//sqauredemo
//demonstrates the use of a function which processes args
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace std;
//square - returns the square of its argument.
//doubleVar - the value to be squared
// returns - square of doubleVar
double square(double doubleVar)
{
return doubleVar * doubleVar;
}
//displayExplanation - prompt user as to the rules of the game.
void displayExplanation(void)
{
cout << "This program sums the square of multiple\n"
<< "series of numbers.\n"
<< "Terminate each sequence by entering a negative number.\n"
<< "Terminate the series by entering a blank space.\n";
cout << endl;
return;
}
//sumSquareSequence - accumulate the square of the number entered into a sequence until the user enters a negative
//number.
double sumSqareSequence(void)
{
double accumulator = 0.0;
for(;;)
{
double dValue = 0;
cout << "Enter next number: ";
cin >> dValue;
if(dValue < 0)
{
//exit the loop
break;
}
double value = square(dValue);
//now add the square to the accumulator
accumulator += value;
}
//return the accumulated value
return accumulator;
}
int main(int nNumberofArgs, char* pszArgs[])
{
displayExplanation();
//continue to accumulate numbers..
for(;;)
{
cout << "Enter next sequence";
cout << endl;
double accumulatedValue = sumSqareSequence();
//terminate if the sequence is zero or negative
if (accumulatedValue <= 0)
{
break;
}
//now output the accumulated result.
cout << "\nThe total of the values squared is: ";
cout << accumulatedValue;
cout << endl;
cout << endl;
}
}