| | |
Noobie help :P
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 2
Reputation:
Solved Threads: 0
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:
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 bit:
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:
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:
Lo and behold it works aswell.
Now onto the real question :rolleyes:
The two following bits of source:
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
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:
c Syntax (Toggle Plain Text)
#include <iostream> int main() { std::cout << "Game Over!"; std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); return 0; }
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)
return 0;
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int score; double distance; char playAgain; short lives, aliensKilled; score = 0; distance = 1200.76; playAgain = 'y'; lives = 3; aliensKilled = 10; double engineTemp = 6572.89; cout << "\nscore: " << score << endl; cout << "distance: " << distance << endl; cout << "playAgain: " << playAgain << endl; cout << "lives: " << lives << endl; cout << "aliensKilled: "<< aliensKilled << endl; cout << "engineTemp: " << engineTemp << endl; int fuel; cout << "\nHow much fuel? "; cin >> fuel; cout << "fuel: " << fuel << endl; typedef unsigned short int ushort; ushort bonus = 10; cout << "\nbonus: " << bonus << endl; return 0; }
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:
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1 ) 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
It's a convoluted way of clearing the input buffer of characters left after the previous read. When you read a number using
It's a bandaid that's necessary because
cin, 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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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:
c Syntax (Toggle Plain Text)
#include <iostream> int main() { std::cout << "Game Over!"; std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); return 0; }
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 thebit:C++ Syntax (Toggle Plain Text)
return 0;
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int score; double distance; char playAgain; short lives, aliensKilled; score = 0; distance = 1200.76; playAgain = 'y'; lives = 3; aliensKilled = 10; double engineTemp = 6572.89; cout << "\nscore: " << score << endl; cout << "distance: " << distance << endl; cout << "playAgain: " << playAgain << endl; cout << "lives: " << lives << endl; cout << "aliensKilled: "<< aliensKilled << endl; cout << "engineTemp: " << engineTemp << endl; int fuel; cout << "\nHow much fuel? "; cin >> fuel; cout << "fuel: " << fuel << endl; typedef unsigned short int ushort; ushort bonus = 10; cout << "\nbonus: " << bonus << endl; return 0; }
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:
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1 ) 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
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)
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.
•
•
•
•
yes i just do
using namespace std
then im like
cout << "Hello";
cin >> variablex;
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.
>So, next time you call "cin >> variable", it will reach the left over ENTER chacter and continue, not allowing any input.
Not quite.
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.
All my posts may be freely redistributed under the terms of the MIT license.
Consider this simple program:
My output:
Notice all the extra returns I hit?
By contrast, consider this modification:
My output:
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> int main() { std::string firstName; std::string lastName; std::cout << "Please enter your first and last name" << std::endl; std::cin >> firstName >> lastName; std::cout << "Hello " << lastName << ", " << firstName << std::endl; return 0; }
C++ Syntax (Toggle Plain Text)
Please enter your first and last name Joe Programmer Hello Programmer, Joe
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)
#include <iostream> #include <string> int main() { std::string firstName; std::string lastName; std::cout << "Please enter your first name" << std::endl; std::cin >> firstName; std::cout << "Please enter your last name" << std::endl; std::cin >> lastName; std::cout << "Hello " << lastName << ", " << firstName << std::endl; return 0; }
C++ Syntax (Toggle Plain Text)
Please enter your first name Joe Programmer Please enter your last name Hello Programmer, Joe
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
>So, next time you call "cin >> variable", it will reach the left over ENTER chacter and continue, not allowing any input.
Not quite.cinactually 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 mixingcin >>andgetline.
LamaBot
![]() |
Similar Threads
- Noobie (Visual Basic 4 / 5 / 6)
- knoppix or else (Getting Started and Choosing a Distro)
- Mandrake 9.2 Screen Res problem (Window and Desktop Managers)
- i don't know how to install linux (*nix Software)
- Tutorials for Linux (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Encryptor
- Next Thread: Help with very simple class...
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






