|
-
Dec 27th, 2002, 12:54 PM
#1
Thread Starter
Frenzied Member
The Control of C++
I am trying to learn C++ on my own right now from a book "C++ for dummies". It does not explain some things very well.
So, I am confused about this:
int sumSequence(void)
{
// loop forever
int accumulator = 0;
for(;
{
// fetch another number
int value = 0;
cout << "Enter next number: ";
cin >> value;
// if it's negative...
if (value < 0)
{
// ...then exit from the loop
break;
}
// ...otherwise add the number to the
// accumulator
accumulator= accumulator+ value;
}// break goes here
// return the accumulated value
return accumulator;
}
int main(int arg, 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";
// accumulate sequences of numbers...
int accumulatedValue;
do
{
// sum a sequence of numbers entered from
// the keyboard
cout << "\nEnter next sequence\n";
accumulatedValue = sumSequence();
// now output the accumulated result
cout << "\nThe total is "
<< accumulatedValue
<< "\n";
// ...until the sum returned is 0
} while (accumulatedValue != 0);
cout << "Program terminating\n";
return 0;
}
I ran this little program and found that int main showed up first. But before main finished it jumps up to the beginning and then back down below main. Here:
cout << "\nThe total is "
<< accumulatedValue
<< "\n";
Why?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|