SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 25th, 2008, 11:25 AM
crofalcon crofalcon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 11 crofalcon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 40 m 31 sec
Reputation Power: 0
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.

Reply With Quote
  #17  
Old April 25th, 2008, 01:51 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,062 sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 9 h 53 m 2 sec
Reputation Power: 1441
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).

Reply With Quote
  #18  
Old May 4th, 2008, 11:21 PM
crofalcon crofalcon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 11 crofalcon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 40 m 31 sec
Reputation Power: 0
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

Reply With Quote
  #19  
Old May 5th, 2008, 12:04 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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.

Reply With Quote
  #20  
Old May 6th, 2008, 04:49 AM
crofalcon crofalcon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 11 crofalcon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 40 m 31 sec
Reputation Power: 0
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

Reply With Quote
  #21  
Old May 6th, 2008, 07:26 AM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Stubborn ol' L'User
Click here for more information.
 
Join Date: Jun 2005
Posts: 3,062 sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 7th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 9 h 53 m 2 sec
Reputation Power: 1441
Quote:
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, that is expecting an int, it will see the "abc" and fail. So another better way would be to try and clear the junk out of the input buffer, such as:

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.

Reply With Quote
  #22  
Old May 6th, 2008, 10:40 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
Quote:
Originally Posted by sizablegrin
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.

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > End of file signal


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway