Greetings, new member and user here, taking up non-web programming for the first time in .... well, I used Fortran and Cobal if that tells you.
I'm running NetBeans 6.9 and g++ in Ubuntu 10.10 on a 64 bit machine.
Tried working my way through C++ For Absolute Beginners, that was good until about 1/2 way through, then not so much (doesn't explain enough about what's actually happening.) Now working through Sam's Teach Yourself C++ and that seems pretty good, then I'm going to go through Stroustrup's 3rd edition.
Here's the issue:
Sam's 7th chapter is on functions. I thought I had a good handle on that already, but am trying to be thorough, so going through everything I can get my hands on. Anyway, the tutorial has me write a program to divide two floats and return the result using functions. There are 6 functions and the main. 5 of the functions work fine (initialize, get dividend, get divisor, error handling and pause) but the divide function and call are throwing errors and I can't figure out why (I'm sure it's obvious, but not to me)
and then in main, within a try, it fetches the dividend and divisor from their individual functions and then the calls the Divide function:
cout << Divide(Dividend,Divisor) << endl;
my IDE is returning two errors- too few arguments in the Divide declaration and "error: at this point in file" at the call
As I understand it, "theDivisor" and "theDividend" are local to the Divide function, and should accept "Dividend" and "Divisor" respectively as the arguments sent to the function which should then perform the division operation and return the resulting float to be output to the screen, yes?
I'm missing something here and can't figure it out. A nudge in the right direction would be greatly appreciated.
>Do you declare the function Divide at some other part of the code?
no, just the above listed function. Mind you, I copied it straight out of the book, so I'm presuming the book is right, which may very well be a bad assumption.
>And where do you declare/define Dividend and Divisor?
Each has their own separate function, neither of which is throwing errors during debug. Here's Dividend, divisor is nearly identical:
float Divide(const float theDividend,const float theDivisor) // Takes unmodifiable arguments, returns float
{
return (theDividend/theDivisor); // Returns the result of the calculation
}
int HandleNotANumberError(void) // Returns the error code
{
cerr <<
"Input error - input may not have been a number."
<<endl;
cin.clear(); // Clear the error state from the stream
// Eat the bad input so we can pause the program
char BadInput[5];
cin >> BadInput;
return 1; // An error occurred
}
void PauseForUserAcknowledgement(void)
{
// Note: You must type something before the Enter key
char StopCharacter;
cout << endl << "Press a key and "Enter": ";
cin >> StopCharacter;
}
int main(int argc, char* argv[])
{
Initialize(); // Call the function
Aside from a few typos (you have to escape the quotes around Enter invoid PauseForUserAcknowledgement(void)) the problem is cout << Divide(Dividend/Divisor) << endl;
should be cout << Divide(Dividend, Divisor) << endl;
I'm embarrassed I didn't see that. I think, in my math teacher mind, I was doing the division operation in both places, and the obvious idea that only the function should be doing it whizzed right past my grey matter. Thanks so much for the help Naraku9333. It works as planned now.
If it's not too much of a bother, what typos did you see?
nara, I don't think that's a typo. For some reason, escape characters behave weird in this forum, e.g. you see " , but I actually wrote \", for which I have to write \\" to show it as it is in the code.