-
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?
-
int main is the starting function of your program and any program not using MFC, AFX or Window API. Thats why it started there. It jumps up because of accumulatedValue = sumSequence(); sumSequence() call the sumSequence function which is above main. Hope this helps some.
I have never looked at the dummies book for C, but it doesn't seem like its giving you a good foundation to start with. You might want to try a different book. I like the Beginning Visual C++ by Ivor Horton the best. If you search around I bet you could find more recommend books and sites on the forum.
-
But then it jumps down below I guess because it got the input from the loops and then jumps back down there to finish. Is that right? C++ is that smart?
-
Right once the function sumSequence is done it returns to where it left off in main
-
Ok, now my eyes are starting to open. I am used to Batch files.
-
ouch.
Do you know the call batch instruction, which lets you execute another batch file and then continue the current where you left? Like
Code:
dir /a /o
call some.bat
cd \
Functions work just like that, only that they also have parameters and a return value (you can pass information to them and get information back).
-
I didn't know about that batch command. But I am glad I am learning C++. Or maybe I'll choose C#. . .can't decide yet.
Hope everything pays off marvolously (spell check please).
-
If you already started out with C++ then learn it. C# syntax is (with a few exceptions) a subset of C++ syntax, so once you know C++ learning C# is very easy.
-
I think I will stick with C++ for now. Thank you for all your help.
-
You were welcome and you will be in the future too.
-