can someone tell me whats wrong with my code?

Started by
14 comments, last by fireblade 19 years, 8 months ago
Tell me your problem exactly, as I'm having a hard time replicating it...

Which line is causing the error? And what exactly is the error...?
Advertisement
I think I know where your problem is, but it may be more helpful for you to find it, but here is a hint or two to help you out:

Input from the keyboard does not actually go directly to your program, it is buffered. The same goes for output from your program to the screen.

A common and simple way to find where your program is actually terminating is to use something like:

cout << "I made it this far!" << endl;

the endl here is important. Because of the buffer issue I mentioned above, if the program crashes before the buffer is flushed to screen you will never see your line, even though you wrote it. the endle will put a newline and flush the buffer.

Move this line around, following your flow of control. I would suggest starting it after your loop, and see if you get what you expect.

[edit] BTW: cin >> will read up to the newline(or space), but will not read the newline, and this is the purpose the ignore Matt Apple suggested. remember that ignore will ignore upto and including the newline(or the number of characters specified).
Okay, I found the problem. The only thing is I dont know why the program wouldn't register the cin.getline(response,256). It seemed like it skipped it, probably cause there is some space or something im assuming, and when it encountered the if statement, the program terminated. Also, can someone explain what exactly the ignore() meathod of cin does, as my book did not cover this. Thanks.

int WhatKindOfStudent(){        cout << "Enter student type  (1 = English, 2 = Math, 3 = Science): ";    cin.ignore(1,'\n'); //added this to fix cin.getline    cin.getline(response,256);        if (strlen(response) == 0) {        cout << "You must select a Student Type";        exit(1);    }cout << "I made it this far" << endl;    return atoi(response);}
As explained in my previous post, input is buffered. cin.ignore will ignore the specified number of bytes up to a specific character. In your case:

cin.ignore(1, '\n');


ignore one character upto and including the newline. if you replaces the one with 5, it would still only read up to the newline, and not beyond.

Now, before you get to the get line you do a cin like this:

cin >> moreGradesToCalculate;


This skips leading white space and stops at the first whitespace after that, in your case the newline, but does not remove it from the buffer. What this means is that the newline is the next thing on the buffer and the next character to be read in.

This does not effect using the stream operator (>>) because it will be ignored. cin.getline, however does not ignore it.

cin.getline(response,256);


will read up to 256 character up to the newline, ignoring it. Without the ignore the next character to be read is a newline, therefore it stops there, and your string is length zero. So, you enter your if.

On another note, cin.getline can actually take a third parameter, which is the character to read to, if you don't like the newline:

cin.getline(responce,256, ';'); //for example 
Okay, thanks again for all ur help, im sure ill be needing it again sometime as I learn more and more c++. Thanks again.
Quote:Original post by Anonymous Poster
The string "yes" has 3 letters in it.
How many times do you go through the loop?

step through the loop your self, instruction by instruction like you were the computer. . . no cheating here, or you could miss your solution.


The good old rule, if you want to copmletely understand it do it on foot.

This topic is closed to new replies.

Advertisement