I'm definitely a beginner and desperately need some help. I'm studying for an exam and trying the practice problem in the book. I keep getting an error with a void fctn, the code is below:
void readdata(int num[], int &n)
{
cout << "Enter the number of marks> ";
cin >> n;
if (n > 0 && n <= SIZE)
cout << "There are " << n << " marks" << endl;
else {
cout << "Invalid number of marks" << endl;
exit(1);
}
for (int count = 0; count < n; count++) {
cout << "Enter a mark> ";
cin >> num[count];
cout << num[count] << endl;
}
return;
}
This is the error msg:
E:\Users\...\Practice Programs\20111104_Prog7.cpp||In function 'void readdata(int*, int&)'
E:\Users\...\Practice Programs\20111104_Prog7.cpp|41|error: 'exit' was not declared in this scope|
What am I doing wrong? I'm litterally copying code from the book and didn't make any mistakes in that sense. Also, since I'm very new to programming, I've done all that "my" programming allows me to do. Not sure if this helps, but I'm using Code Blocks as my compiler.
I don't understand how you could studying for an exam, meaning you must have read a few chapters in a C++ book, did some of the problems the teacher gives you, and not know how to write a function. For one, you declared the function as void, but you have a return. Two, exit() is not C++ command. You probably meant return 1;, but again, the function is void, meaning it returns nothing. I'll let others tell you more..
The C exit() function is declared in stdlib.h, so it's likely you're missing
1 2 3 4
...
<cstdlib>
...
usingnamespace std;
but I never use exit(). I would return a code, eventually returning it from main (see PS).
How deep is your function?
Andy
PS I do come across exit() is C++ code, from people who also code C. But it is not a good idea. See: "return statement vs exit() in main()" http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main
Note that calling exit() deeper in the call stack would just compound issues. The only valid use might be to bail out of an app when something's gone horribly wrong.
fun2code & andywestken, thanks for the help. The return stmt worked and I also checked the link.
whitenite1, you're a moron. I shouldn't entertain you but I will. This ch. that I was reviewing introduced arrays. All the fctns in the prog, there are 5, worked except this one. This is an entry lvl C++ programming class with 520 pages of concepts that unless you're Rainman, no one's going to recall... hence me seeking some help from people with more experience. The fact of the matter is I'm making up an incomplete from last semester because my 19 yr old brother was diagnosed with leukemia. So thanks for being a d-bag, I needed a break from studying. And, no, I'm not doing assigned problems from my professor, I'm going over stuff on my own.
You other two, thanks again. I'm honestly enjoying the whole learning process, but it gets hard when reviewing a concept and the book has the wrong code. I'm probably going to continue learning this language on my own b/c I'm having a good time. My other bro actually writes in a bunch of other languages, all pretty similar but not C++; and it would give us something more in common. I just can't call him at 4 in the morning to ask for some help.
I give you my humblest apology for the answer I gave you, and hope you'll forgive me. If not, well, I probably deserved it. I will refrain from making *snap ( *meaning: stupid ) assumptions in the future.
Not a problem, you seem not to be the only one here that made poor assumptions; I too apologize for my comments. This last year (Halloween 2010 to be exact) has been more than a toll on everyone. Just like everything that people study, the more you learn about a subject, the more critical one can get. By that I mean that the more I study C++, the more I realize that this book selected for this class is horrible. There are so many bugs in the sample programming that it's crazy that the professor still uses this text, then again, who am I to question his choices. Point being is that my patience in general and especially with this book has waned greatly.
As a side note, it was last semester that my brother received a bone marrow transplant (the single most critical moment in all of his treatments) and he's doing much better. We're all still in a holding pattern because a simple cold, something that you or I may never notice, could have detrimental effects on him.
Don't worry, we're cool. Thank you for your 2nd post. Again, sorry for getting snappy; there's no way you could have known the background of this whole situation.
@stormtrooper
Thank you, but knowing the background or not, I still should have not answered that way. When someone is having a problem that, for one reason or another, they cannot get to the bottom of, the last thing they need is misunderstanding. I, too, have seen misprints in programming books, and had to improvise to get the correct results.
I'm glad to hear your brother is doing better, and pray that he keeps on improving.
Again, thanks for the understanding and forgiveness.