ATiGuy
March 17th, 2006, 03:12 PM
I don't get it. Don't the lines accumulator = 0; and while (accumulator != 0); contradict themselves? I don't see how the program exits after I enter in a negative number the second time. I understand how it repeats the loop, but I don't see how it ever gets out of it. Why does enteiring a negative number twice remove it from the loop completely? When entering it only once only restarts the loop? I don't see how the code ever gets to cout << "The total for this sequence is " This is my first time ever with C++ or any real language for the matter.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// the outer loop
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 sequences
int accumulator;
do
{
// start entering the next sequence
// of numbers
accumulator = 0;
cout << "Start the next sequence\n";
// loop forever
for(;;)
{
// fetch another number
int value = 0;
cout << "Enter next number: ";
cin >> value;
// if it's negative...
if (value < 0)
{
// ...then exit
break;
}
// ...otherwise add the number to the
// accumulator
accumulator = accumulator + value;
}
// output the accumulated result...
cout << "The total for this sequence is "
<< accumulator
<< endl << endl;
// ...and start over with a new sequence
// if the accumulated sequence was not zero
} while (accumulator != 0);
// we're about to quit
cout << "Thank you" << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
// the outer loop
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 sequences
int accumulator;
do
{
// start entering the next sequence
// of numbers
accumulator = 0;
cout << "Start the next sequence\n";
// loop forever
for(;;)
{
// fetch another number
int value = 0;
cout << "Enter next number: ";
cin >> value;
// if it's negative...
if (value < 0)
{
// ...then exit
break;
}
// ...otherwise add the number to the
// accumulator
accumulator = accumulator + value;
}
// output the accumulated result...
cout << "The total for this sequence is "
<< accumulator
<< endl << endl;
// ...and start over with a new sequence
// if the accumulated sequence was not zero
} while (accumulator != 0);
// we're about to quit
cout << "Thank you" << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}