1. // SquareDemo - demostrate the use of a function 2. // which processes arguments 3. 4. #include 5. #include 6. 7. // square - returns the square of its argument 8. // dVar - the value to be squared 9. // returns - sqare of dVar 10. double square(double dVar) 11. { 12. return dVar * dVar; 13. } 14. 15. int sumSequence() 16. { 17. // loop forever 18. int nAccumulator = 0; 19. for(;;) 20. { 21. // fetch another number 22. double dValue = 0; 23. cout << "Enter next number: "; 24. cin >> dValue; 25. 26. // if it's negative... 27. if (dValue < 0) 28. { 29. // ...then exit from the loop 30. break; 31. } 32. 33. // ...otherwise calculate the square 34. int nValue = (int)square(dValue); 35. 36. // now add the square to the 37. // accumulator 38. nAccumulator = nAccumulator + nValue; 39. } 40. 41. // return the accumulated value 42. return nAccumulator; 43. } 44. 45. int main(int nArg, char* nArgs[]) 46. { 47. cout << "This program sums multiple series\n" 48. << "of numbers. Terminate each sequence\n" 49. << "by entering a negative number.\n" 50. << "Terminate the series by entering two\n" 51. << "negative numbers in a row\n"; 52. 53. // Continue to accumulate numbers... 54. int nAccumulatedValue; 55. do 56. { 57. // sum a sequence of numbers entered from 58. // the keyboard 59. cout << "\nEnter next sequence\n"; 60. nAccumulatedValue = sumSequence(); 61. 62. // now output the accumulated result 63. cout << "\nThe total is " 64. << nAccumulatedValue 65. << "\n"; 66. 67. // ...until the sum returned is 0 68. } while (nAccumulatedValue != 0); 69. cout << "Program terminating\n"; 70. return 0; 71. } 72.