currently going through C++ for dummies and found problems with some things so i wouldnt be surprised if some of the book is typod. reading cautiously though it
anyways, im in chapter 6 creating functions
he doesnt explain very well void displayExplanation (void)and displayExplanation ()and sumSequence ()
could someone explain these cause i dont understand them. he keeps refering to main() and there is no main () in his testfuntion code. It doesn't make sense
this is his code for explaining these
// FunctionDemo - demonstrate the use of functions
// by breaking the inner loop of the
// NestedDemo program off into its own
// function
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace std;
// displayExplanation - prompt the user as to the rules
// of the game
void displayExplanation(void)
{
cout << "This program sums multiple series\n"
<< "of numbers. Terminate each sequence\n"
<< "by entering a negative number.\n"
<< "Terminate the series by entering an\n"
<< "empty sequence.\n"
<< endl;
return;
}
// sumSequence - add a sequence of numbers entered from
// the keyboard until the user enters a
// negative number.
// return - the summation of numbers entered
int sumSequence(void)
{
// loop forever
int accumulator = 0;
for(;;)
{
// fetch another number
int nValue = 0;
cout << "Enter next number: ";
cin >> nValue;
// if it's negative...
if (nValue < 0)
{
// ...then exit from the loop
break;
}
// ...otherwise add the number to the
// accumulator
accumulator += nValue;
}
// return the accumulated value
return accumulator;
}
int main(int nNumberofArgs, char* pszArgs[])
{
// display prompt to the user
displayExplanation();
// accumulate sequences of numbers...
for(;;)
{
// sum a sequence of numbers entered from
// the keyboard
cout << "Enter next sequence" << endl;
int accumulatedValue = sumSequence();
// terminate the loop if sumSequence() returns
// a zero
if (accumulatedValue == 0)
{
break;
}
// now output the accumulated result
cout << "The total is "
<< accumulatedValue
<< "\n"
<< endl;
}
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;
}
plus sorry in advance for the long code...copy and pasted his code and he put empty code line in between everything lol
The main() function is always called first. And the first thing main() encounters in this program is displayExplanation() on line 113. It passes no variables to displayExplanation() on account of theres is nothing between the parentheses. So if you look at the function decleration on line 23, it is declared void displayExplanation(void). The void before displayExplanation(void) means it returns nothing and the void in the parentheses means it takes no variables. That void is not necesarry.
You'll learn about functions taking variables soon but don't worry about it for now. Learn how the control is passed through the program first.
Now displayExplanation just outputs some text and then goes back to the main function. The return on line 39 is not necesarry. When this function reaches the end, the only place for it to will be back to main().
The for(;;) on line 61 of main() just keeps an infinite loop going until you break out of it. So on line 129, int accumulatedValue is declared and set equal to sumSequence(), which is a function that also takes no variables. But if you look at the function decleration on line 53, it returns an int.
Now sumSequence() starts another infinite loop until a negative number is read in, line 77, and it breaks out of the loop. It will return whatever the accumulated value is, line 101. This value is stored by main() into accumulatedValue, which is then displayed, lines 149-155.
So that's kind of how functions work. Main() is always called first and when it encounters a function, denoted by a name and (), it performs that function and eventually returns to main or exits. I also read this book and I found that as I got to the more advanced stuff it became hard to follow.
Hope that helps, good luck!
ok now i understand where control is jumping to and from, but is he trying to teach at this point to learn where control is jumping to and from or he is trying to teach something that i am not understanding?
another question...isn't there an easier was to put this program in sequential order to not have control jump around so much(mostly to easily understand) if his goal was to teach to follow where control was going then it all makes sense.
my other question is...isn't the idea to not have control jump around so much? doesn't that make a program run slower than it could?
thanks a lot, nice explanation
lol oh yeah another question, shouldn't this run, i copied and pasted it from his cd, but it gives and error code : undefined reference to 'main'