// SquareDemo - demostrate the use of a function // which processes arguments #include #include // square - returns the square of its argument // dVar - the value to be squared // returns - sqare of dVar double square(double dVar) { return dVar * dVar; } int sumSequence(void) { // loop forever int nAccumulator = 0; for(;;) { // fetch another number double dValue = 0; cout << "Enter next number: "; cin >> dValue; // if it's negative... if (dValue < 0) { // ...then exit from the loop break; } // ...otherwise calculate the square int nValue = (int)square(dValue); // now add the square to the // accumulator nAccumulator = nAccumulator + nValue; } // return the accumulated value return nAccumulator; } int main(int nArg, char* pszArgs[]) { cout << "This program sums multiple series\n" << "of numbers. Terminate each sequence\n" << "by entering a negative number.\n" << "Terminate the series by entering two\n" << "negative numbers in a row\n"; // Continue to accumulate numbers... int nAccumulatedValue; do { // sum a sequence of numbers entered from // the keyboard cout << "\nEnter next sequence\n"; nAccumulatedValue = sumSequence(); // now output the accumulated result cout << "\nThe total is " << nAccumulatedValue << "\n"; // ...until the sum returned is 0 } while (nAccumulatedValue != 0); cout << "Program terminating\n"; return 0; }