|
-
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?
-
Dec 27th, 2002, 01:33 PM
#2
Frenzied Member
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.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Dec 27th, 2002, 01:47 PM
#3
Thread Starter
Frenzied Member
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?
-
Dec 27th, 2002, 01:49 PM
#4
Frenzied Member
Right once the function sumSequence is done it returns to where it left off in main
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Dec 27th, 2002, 02:04 PM
#5
Thread Starter
Frenzied Member
Ok, now my eyes are starting to open. I am used to Batch files.
-
Dec 27th, 2002, 06:56 PM
#6
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).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 28th, 2002, 12:36 AM
#7
Thread Starter
Frenzied Member
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).
-
Dec 28th, 2002, 09:56 AM
#8
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 28th, 2002, 05:17 PM
#9
Thread Starter
Frenzied Member
I think I will stick with C++ for now. Thank you for all your help.
-
Dec 29th, 2002, 04:17 AM
#10
You were welcome and you will be in the future too.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 29th, 2002, 10:40 PM
#11
Thread Starter
Frenzied Member
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
|