944,205 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1967
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 26th, 2007
0

Noobie help :P

Expand Post »
Hi, first post :cheesy:

I'm currently a complete noob to c++ programming and am currently reading the book "Beginning C++ Game Programming" written by Michael Dawson. I had the problem where i complied and ran the following source code:

  1. #include <iostream>
  2.  
  3. int main()
  4.  
  5. {
  6. std::cout << "Game Over!";
  7. std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
  8. return 0;
  9. }

This would pop up with a window and it would then very quickly dissapear, in the book it says that this might happen and says to use the following code just before the
C++ Syntax (Toggle Plain Text)
  1. return 0;
bit:

  1. std::cin.ignore(std::cin.rdbuf()->in_avail() + 1 )

This then ofcourse worked and i have to press enter to close the window that pops up (you'll see why i have written this later).

I then moved a tiny bit further into the book and used the following code:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int score;
  8. double distance;
  9. char playAgain;
  10. short lives, aliensKilled;
  11.  
  12. score = 0;
  13. distance = 1200.76;
  14. playAgain = 'y';
  15. lives = 3;
  16. aliensKilled = 10;
  17.  
  18. double engineTemp = 6572.89;
  19. cout << "\nscore: " << score << endl;
  20. cout << "distance: " << distance << endl;
  21. cout << "playAgain: " << playAgain << endl;
  22. cout << "lives: " << lives << endl;
  23. cout << "aliensKilled: "<< aliensKilled << endl;
  24. cout << "engineTemp: " << engineTemp << endl;
  25.  
  26. int fuel;
  27. cout << "\nHow much fuel? ";
  28. cin >> fuel;
  29. cout << "fuel: " << fuel << endl;
  30.  
  31. typedef unsigned short int ushort;
  32. ushort bonus = 10;
  33. cout << "\nbonus: " << bonus << endl;
  34. return 0;
  35. }

The problem with this one is that at the red highlighted bit the user is meant to input a number into the window and press enter. I have read on this forum that you can use the following code to get around the problem:

  1. std::cin.ignore(std::cin.rdbuf()->in_avail() + 2 )

Lo and behold it works aswell.

Now onto the real question :rolleyes:

The two following bits of source:
  1. std::cin.ignore(std::cin.rdbuf()->in_avail() + 1 )
  2. std::cin.ignore(std::cin.rdbuf()->in_avail() + 2 )

I have no idea what the things in them are for and how the hell it is solving the problem for me, as i have read (again on this forum) it isn't exactly good practice to wack in code and just remember not to mess up again in the future. So i was therefore wandering if you lot could tell me what the things in this mean and do so that they solve the problem so that i can use the code with actually knowing what it is doing. Considering I'm a complete noob and won't understand any real techincal jargon yet (read the book for about 3 hours now and only 20 pages in :lol: ) can you explain it in a way that even George Bush could understand. Cheers :cheesy:

IDE = Dev-C++ LINKY
Last edited by ikix; Feb 26th, 2007 at 7:01 pm. Reason: QUOTE and CODE tags are not the same thing -- and format your code properly please
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ikix is offline Offline
2 posts
since Feb 2007
Feb 26th, 2007
0

Re: Noobie help :P

It's a convoluted way of clearing the input buffer of characters left after the previous read. When you read a number usingcin, the characters after the number are left in the buffer for the nect cin to read. That basically means the ENTER is still there, and when you attempt to read a string, the first thing it sees is ENTER, so it's happy. That convoluted cin.ignore() clears the buffer.

It's a bandaid that's necessary because cin is a relatively misbehaved command. It's ultimately better to read a full line using getline() and then parse the line into it's components.
Moderator
Reputation Points: 3280
Solved Threads: 897
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Feb 26th, 2007
0

Re: Noobie help :P

Click to Expand / Collapse  Quote originally posted by ikix ...
Hi, first post :cheesy:

I'm currently a complete noob to c++ programming and am currently reading the book "Beginning C++ Game Programming" written by Michael Dawson. I had the problem where i complied and ran the following source code:

  1. #include <iostream>
  2.  
  3. int main()
  4.  
  5. {
  6. std::cout << "Game Over!";
  7. std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
  8. return 0;
  9. }

This would pop up with a window and it would then very quickly dissapear, in the book it says that this might happen and says to use the following code just before the
C++ Syntax (Toggle Plain Text)
  1. return 0;
bit:

  1. std::cin.ignore(std::cin.rdbuf()->in_avail() + 1 )

This then ofcourse worked and i have to press enter to close the window that pops up (you'll see why i have written this later).

I then moved a tiny bit further into the book and used the following code:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int score;
  8. double distance;
  9. char playAgain;
  10. short lives, aliensKilled;
  11.  
  12. score = 0;
  13. distance = 1200.76;
  14. playAgain = 'y';
  15. lives = 3;
  16. aliensKilled = 10;
  17.  
  18. double engineTemp = 6572.89;
  19. cout << "\nscore: " << score << endl;
  20. cout << "distance: " << distance << endl;
  21. cout << "playAgain: " << playAgain << endl;
  22. cout << "lives: " << lives << endl;
  23. cout << "aliensKilled: "<< aliensKilled << endl;
  24. cout << "engineTemp: " << engineTemp << endl;
  25.  
  26. int fuel;
  27. cout << "\nHow much fuel? ";
  28. cin >> fuel;
  29. cout << "fuel: " << fuel << endl;
  30.  
  31. typedef unsigned short int ushort;
  32. ushort bonus = 10;
  33. cout << "\nbonus: " << bonus << endl;
  34. return 0;
  35. }

The problem with this one is that at the red highlighted bit the user is meant to input a number into the window and press enter. I have read on this forum that you can use the following code to get around the problem:

  1. std::cin.ignore(std::cin.rdbuf()->in_avail() + 2 )

Lo and behold it works aswell.

Now onto the real question :rolleyes:

The two following bits of source:
  1. std::cin.ignore(std::cin.rdbuf()->in_avail() + 1 )
  2. std::cin.ignore(std::cin.rdbuf()->in_avail() + 2 )

I have no idea what the things in them are for and how the hell it is solving the problem for me, as i have read (again on this forum) it isn't exactly good practice to wack in code and just remember not to mess up again in the future. So i was therefore wandering if you lot could tell me what the things in this mean and do so that they solve the problem so that i can use the code with actually knowing what it is doing. Considering I'm a complete noob and won't understand any real techincal jargon yet (read the book for about 3 hours now and only 20 pages in :lol: ) can you explain it in a way that even George Bush could understand. Cheers :cheesy:

IDE = Dev-C++ LINKY
The "std::" portion of a given statement says to the compiler, "The following calls (i.e cin.ignore(...)) will be called with the definitions defined under the "std" namespace. If you include two files containing two different classes, perhaps used for two different purposes, and it contains class names that are the same, how is the compiler suppose to know what call is to what class? Namespace solve this problem by having libraries declared under a unique namespace. In effect, two libraries can contain classes with the same name, as long as they're under different namespaces, basically.

Also, you don't have to use the "std::" all the time to indicate a particular namespace, you could simply insert this:

C++ Syntax (Toggle Plain Text)
  1. using namespace std;

after your "#includes ..." then you could just delete all statements preceeding "std::".

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 26th, 2007 at 7:21 pm.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 26th, 2007
0

Re: Noobie help :P

yes i just do

using namespace std

then im like

cout << "Hello";
cin >> variablex;
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005
Feb 26th, 2007
0

Re: Noobie help :P

Click to Expand / Collapse  Quote originally posted by jbennet ...
yes i just do

using namespace std

then im like

cout << "Hello";
cin >> variablex;
Then you haven't run into the scoping issues associated with that yet.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 26th, 2007
0

Re: Noobie help :P

Click to Expand / Collapse  Quote originally posted by jbennet ...
yes i just do

using namespace std

then im like

cout << "Hello";
cin >> variablex;
Waltp is right, however, I'm going to try to give a more thourough explaination.

Just think of cin as a way to access a buffer that holds characters until it reaches an ENTER character. The buffer can also hold the ENTER character. As Waltp indicated, the cin buffer sometimes retains an ENTER character because if misbehaviour. So, next time you call "cin >> variable", it will reach the left over ENTER chacter and continue, not allowing any input from the user. There is a read position in the buffer, which indicates a relative reading point. The "rdbuf->in_avail()" member function simply returns the number of characters in the current buffer, excluding the ENTER character. The "ignore" member function uses a number ( the parameter ), which relocates the read position of the input buffer that many number of positions. Now the statement "cin.ignore(cin.rdbuf->in_avail + 1) says, "Place the read position to the end of the input buffer, plus 1 position passing the ENTER character. On the next call to "cin >> variable", it'll start be read for a new input and won't just prompt then continue because of the left over ENTER character.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 26th, 2007 at 7:53 pm.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 26th, 2007
0

Re: Noobie help :P

>So, next time you call "cin >> variable", it will reach the left over ENTER chacter and continue, not allowing any input.
Not quite. cin actually keeps searching/waiting until it finds data. This behaviour is fine until you start using getline() [edit] or until the user types in more data than one variable [/edit], which simply stops when it finds a newline character. That is why the input buffer must be flushed when mixing cin >> and getline.
Last edited by John A; Feb 26th, 2007 at 8:01 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 26th, 2007
0

Re: Noobie help :P

? simple terms please
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,534 posts
since Apr 2005
Feb 26th, 2007
0

Re: Noobie help :P

Click to Expand / Collapse  Quote originally posted by jbennet ...
? simple terms please
Consider this simple program:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main() {
  5.  
  6. std::string firstName;
  7. std::string lastName;
  8. std::cout << "Please enter your first and last name" << std::endl;
  9. std::cin >> firstName >> lastName;
  10. std::cout << "Hello " << lastName << ", " << firstName << std::endl;
  11.  
  12. return 0;
  13. }
My output:
C++ Syntax (Toggle Plain Text)
  1. Please enter your first and last name
  2. Joe
  3.  
  4.  
  5.  
  6.  
  7. Programmer
  8. Hello Programmer, Joe
Notice all the extra returns I hit? cin kept waiting for data, and until then it refused to print the last line.

By contrast, consider this modification:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main() {
  5.  
  6. std::string firstName;
  7. std::string lastName;
  8. std::cout << "Please enter your first name" << std::endl;
  9. std::cin >> firstName;
  10. std::cout << "Please enter your last name" << std::endl;
  11. std::cin >> lastName;
  12. std::cout << "Hello " << lastName << ", " << firstName << std::endl;
  13.  
  14. return 0;
  15. }
My output:
C++ Syntax (Toggle Plain Text)
  1. Please enter your first name
  2. Joe Programmer
  3. Please enter your last name
  4. Hello Programmer, Joe
Notice how the second line was skipped completely? The first input, cin searched for data, and got "Joe". It stopped, because that's all that was needed, leaving "Programmer" in the buffer.

The second cin picked this up and continued, never giving me the chance to enter some input in the second cin statement.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 26th, 2007
0

Re: Noobie help :P

>So, next time you call "cin >> variable", it will reach the left over ENTER chacter and continue, not allowing any input.
Not quite. cin actually keeps searching/waiting until it finds data. This behaviour is fine until you start using getline() [edit] or until the user types in more data than one variable [/edit], which simply stops when it finds a newline character. That is why the input buffer must be flushed when mixing cin >> and getline.
Yeah, I didn't mean to specify that, but was in a rush. Discard that.

LamaBot
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Encryptor
Next Thread in C++ Forum Timeline: Help with very simple class...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC