Noobie help :P

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 2
Reputation: ikix is an unknown quantity at this point 
Solved Threads: 0
ikix ikix is offline Offline
Newbie Poster

Noobie help :P

 
0
  #1
Feb 26th, 2007
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
  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Noobie help :P

 
0
  #2
Feb 26th, 2007
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Noobie help :P

 
0
  #3
Feb 26th, 2007
Originally Posted by ikix View 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
  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,263
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 542
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Noobie help :P

 
0
  #4
Feb 26th, 2007
yes i just do

using namespace std

then im like

cout << "Hello";
cin >> variablex;
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Noobie help :P

 
0
  #5
Feb 26th, 2007
Originally Posted by jbennet View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Noobie help :P

 
0
  #6
Feb 26th, 2007
Originally Posted by jbennet View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Noobie help :P

 
0
  #7
Feb 26th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,263
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 542
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Noobie help :P

 
0
  #8
Feb 26th, 2007
? simple terms please
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Noobie help :P

 
0
  #9
Feb 26th, 2007
Originally Posted by jbennet View Post
? simple terms please
Consider this simple program:
  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:
  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:
  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:
  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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Noobie help :P

 
0
  #10
Feb 26th, 2007
Originally Posted by joeprogrammer View Post
>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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC