|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#16
|
|||
|
|||
|
No i'm not running from the CMD, altho it wouldnt be a problem for me.
Anyways, It's supposed to give a result after that loop, and its not showing. It happened before to me, that the result would be shown for half a second but the window closed after, but i've got like 2 x cin.get(); before the return statement, which i've used so far to keep the window open in the other programs, so I'm guessing the app just crashes before finishing the code. I'm not on my laptop atm so i dont have the code, anyways i'll do 2 things when i get back, and thats to try and run the file from the CMD; and second, i'll post a bigger part of the code here, if that might help, or even the whole code, its a short one really since i'm still a beginner. |
|
#17
|
||||
|
||||
|
If you break cin, which you almost certainly are, then the cin.get () won't pause. It has to be repaired, first. Again, read about what you use.
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread). |
|
#18
|
|||
|
|||
|
Sorry for being away for quite some time.
Straight to the point: I'm not sure where/what exactly is "breaking" cin, as you said, and what to do to repair it. I'm really getting confused here, since as I said, i've only begun doing c++ (with the book tutorials) and i'm getting so much replies here with what to do, that aren't even nearly as simple as how far I got in c++. Thanks in advance again |
|
#19
|
||||
|
||||
|
Ok let me give you a rundown about how input/output works in C and C++. Unfortunately most books don't explain how it works and neither do most college courses either, which is a pity because proper I/O handling is really important in real world programming.
The first thing to realize is that in most cases, the input and output are buffered. What this means is that, if you do something like this: Code:
int a, b; cin >> a; cin >> b; and if you enter data like this: Code:
23<enter> 42<enter> or if you enter data like this: Code:
23 42<enter> the data will be read in the same way. Why is this, you ask?? This is where buffering comes in. When you enter something via your keyboard, the input goes into a buffer maintained by the OS. C or C++ then read from the buffer and only read what they need to read and leave the rest behind for the next read operation. So if you enter data like this: Code:
23 42 69<enter> this will work too. C++ will read 23 for the first variable (a), and 42 for the second variable (b), and leave the 69 behind in the buffer for the next input operation. So this is how C and C++ input operations work -- they first check the input buffer to see if there are any characters that can be read. If there is anything valid in the buffer, the program reads it. If there is nothing in the buffer, it pauses and waits for the user to enter something. If you haven't grasped the concept above, read it until you do and write a small program to test it out. The next thing to understand is what happens if you enter something that the program was not expecting. For instance: Code:
int a; cin >> a; Now if you enter "abc" for input, your program will see the "abc" in the buffer and stop reading immediately, since it is expecting some digits, not alphabet characters. It will instead leave the "abc" in the buffer for the next read operation. This is where things get interesting. For instance, if you were reading in a loop: Code:
while(1) {
int a;
cin >> a;
if (a == -1)
break;
// Do something here with a
}
Now on the surface, this code looks like it should work. It reads numbers until the user enters a -1, in which case it breaks out of the loop. However you can probably guess what the problem with this code is. Let's say the user enters "abc". Now the cin >> a line will fail at the letter 'a' since the code is expecting digits. So what does it do then?? It leaves the "abc" behind in the buffer for the next read operation. It then loops around and comes back to read input again. Guess what?? cin is marked in a state of failure and will not read until you clear it first. Hence the input will fail again and the loop continues indefinitely. This means your program will never stop! So how to fix it?? For one thing, both C and C++ input/output functions return values to indicate whether the last read operation was successful or not. Which brings up what sizeablegrin is talking about above -- learn to use your I/O functions properly by reading the documentation on how they work. In C++'s case, you can use the .good() or .fail() member function to check if things went ok or not. Code:
while (1) {
int a = 0;
cin >> a;
if (!cin.good())
break;
if (a == -1)
break;
}
In this case, the code checks if it could successfully read an int in or not and breaks out of the loop if there was an error in reading. Beginner C and C++ programmers are often never taught to check their I/O operations for failures, which is a *bad thing*, because they repeat the same mistakes in their production quality code. Of course, in the above code, while you successfully break out of the while loop, you have still left the "abc" behind in the buffer and if you have another input operation further down, it will fail as well, since the cin object is now in a state of failure. So another better way would be to try and clear the junk out of the input buffer, such as: Code:
while (1) {
int a = 0;
cin >> a;
if (cin.fail()) {
cin.clear();
string dummy;
getline(cin, dummy);
break;
}
if (cin.eof())
break;
if (a == -1)
break;
}
In this case, we clear the error flags and then read the invalid characters into a string variable and this will clear the input buffer, since string variables can read characters such as "abc". Note that since I'm using .fail() instead of .good() in the example above, I also need to check for .eof() separately. Also note I'm not checking if getline() succeeded or not . That is left as an exercise for you to solve.Another way to handle this would be to read the data into a string variable first and then use a stringstream object to read ints, floats etc. from the string. We have examples of this in our commonly asked questions thread on this forum. This is probably the best way there is and you should look into it once you get a bit more comfortable with coding. [edit]Fixed with sizeablegrin's recommendation below[/edit]
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month Last edited by Scorpions4ever : May 6th, 2008 at 11:24 AM. |
|
#20
|
|||
|
|||
|
Wow, thanks for the long and descriptive answer, that answered many questions, now I just need to get that into my code working properly, I'll reply if I still remain stuck xD
Thanks again :P |
|
#21
|
||||
|
||||
|
Quote:
It's actually even worse. The stream is now in a fail condition. It won't attempt to read at the next (or any subsequent) input operation even if the next value matches the type. You'll have to clear the error first. |
|
#22
|
||||
|
||||
|
Quote:
Good point: Code:
if (cin.fail()) {
cin.clear();
getline(cin, dummy);
break;
}
Checking if the getline() succeeded is left as an exercise to the reader ![]() [edit] Fixed the code above with your recommendations. Thanks[/edit] Last edited by Scorpions4ever : May 6th, 2008 at 11:17 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > End of file signal |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|