generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: [C++]

Previous Thread :: Next Thread 
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
29 Jun 2012 09:49 AM
Does any one know why cin.get(); doesn't always work alone?
Like sometimes you need to add another one to work, but a tutorial said to use:
cin.clear();
cin.ignore(255, '\n');
cin.get();

Why..... how? explainzors please Dx
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
29 Jun 2012 09:50 AM
Oh and btw I'll be gone for the weekend incase you won't recieve a reply within 2 days.
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
29 Jun 2012 09:59 AM
That awkward moment when there are views, but no replies o.o
Report Abuse
LocalChum is not online. LocalChum
Joined: 04 Mar 2011
Total Posts: 6906
29 Jun 2012 10:04 AM
I had this problem once.

I was too lazy to use Google to solve my problem, so I used getch instead. :3
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
29 Jun 2012 10:07 AM
°д°
Report Abuse
AnnWhethers is not online. AnnWhethers
Joined: 26 Jun 2012
Total Posts: 128
29 Jun 2012 10:08 AM
...cin.get()?

Why not just use getline(cin,var)?
Report Abuse
su8 is not online. su8
Joined: 06 Mar 2009
Total Posts: 6334
29 Jun 2012 10:10 AM
But then you need to spend memory on the var :P
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
29 Jun 2012 10:10 AM
@ann: I mean to keep open the console.
Report Abuse
AnnWhethers is not online. AnnWhethers
Joined: 26 Jun 2012
Total Posts: 128
29 Jun 2012 10:12 AM
@TheMycro

If you want to keep the console open then include "conio.h" at the top and use the "getch()" function. This is used for getting a keypress, meaning it will pause the console until any key is pressed.
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
29 Jun 2012 10:14 AM
But then I need to include another library and that consumes moar memory...?
Report Abuse
AnnWhethers is not online. AnnWhethers
Joined: 26 Jun 2012
Total Posts: 128
29 Jun 2012 10:14 AM
...no it doesn't... Who's been telling you C++?
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
29 Jun 2012 10:17 AM
Nah I just tought that.c:
Report Abuse
eyeontheprizeREBOOT is not online. eyeontheprizeREBOOT
Joined: 30 Jan 2011
Total Posts: 1555
29 Jun 2012 12:07 PM
Including libraries will only make your exe negligibly larger.
Report Abuse
myrkos is not online. myrkos
Joined: 06 Sep 2010
Total Posts: 8072
29 Jun 2012 12:22 PM
Why use nonstandard conio.h when you can get the equivalent (getchar) from cstdio?
Report Abuse
Multidimensional is not online. Multidimensional
Joined: 05 Apr 2012
Total Posts: 866
29 Jun 2012 01:03 PM
I'll write up a little example.

int num;
std::cin >> num;

This would require you to use std::cin.get() twice. Why? Because std::cin is buffered input, meaning you will have to hit enter before anything happens to the data typed. Once you do hit enter the value entered for the integer num is stored in num and then a newline character ('\n') is stored in the buffer. The first std::cin.get() would extract that newline from the buffer and the second one would pause it. That's its job. But if you only use one it's going to extract the newline then exit the program (or follow on with the rest of the code).
Report Abuse
Megamatrixdude is not online. Megamatrixdude
Joined: 06 Dec 2009
Total Posts: 10939
29 Jun 2012 01:13 PM
I remember once using conio.h and getch...

That was when I modded a keyboard to be a quick launch control on a key press.
Report Abuse
Multidimensional is not online. Multidimensional
Joined: 05 Apr 2012
Total Posts: 866
29 Jun 2012 01:15 PM
I'm using it right now for a 'WPM' program I'm making. <3
Although I probably shouldn't. I've heard there are others ways around it.
Report Abuse
pokelover980 is not online. pokelover980
Joined: 12 Jun 2009
Total Posts: 4141
29 Jun 2012 01:29 PM
"Why use nonstandard conio.h when you can get the equivalent (getchar) from cstdio?"

Indeed. And why use getchar when C++ gives you std::cin.get from iostream? The only time I can think of using getchar over std::cin.get is when you want your program to exit on any keypress since getchar immediately returns as soon as a key is pressed while std::cin.get waits until enter is pressed since it gets from the input stream buffer or something like that, which isn't populated until the enter key is pressed.
Report Abuse
NVI is not online. NVI
Joined: 11 Jan 2009
Total Posts: 4744
29 Jun 2012 01:46 PM
Aw, Multidimensional beat me. But yeah, buffered input.

Anyway, potato
Report Abuse
popinman322 is not online. popinman322
Joined: 04 Mar 2009
Total Posts: 5184
29 Jun 2012 01:49 PM
PDCurses is the way to go.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
29 Jun 2012 02:44 PM
Well, to get to the bottom of it, here's what happens when you do a call to cin.get():

-Let's assume that you have a blank starting state state.
-First consider: what exactly does cin.get() do? It returns one character from cin's input buffer.
-So, when you call it, it gets on... wait, there are no characters in the buffer, we said we're starting from a blank state, so cin now issues a request to the OS for input, and execution blocks until that input gets back.
-Now, say you type "a"... an "a" will appear in the console but execution will not continue, the method that C++'s cin is using to get the input only returns once a newline has been entered. So, you hit enter, and execution continues.
-Now there is the "a" in cin's buffer, which cin.get() can remove from the buffer and return to you, all good?
-You might think so, but WAIT, you got your "a" back, but there is _also_ the newline character, that you entered in cin's buffer.
-So what if you call cin.get again? This time it won't ask you for any input, it still has a newline character in it's buffer, so it will happily return that to you right away, without asking you for more input.


Now that you understand that, you can get an idea of what the code you are asking about is for. In order to force cin.get() to actually ask the user for input you have to make sure that there isn't old data still in cin's buffer from previous input, which cin.get() would get instead otherwise.


And that's all good but the code you posted is WRONG! Don't use it!


std::cin.clear();
This part is fine. What this does is set cin's "error state" to "no error". If there was an error (reaching the end of the stream is an error, so it will often be in an "error" state) in a previous input then the cin.get will silently fail, and not read any input at all, from the buffer or otherwise, just returning 0 or something like that.

std::cin.ignore(255, '\n');
This part is WRONG! What the intent of this statement is to ignore the next characters coming through the stream, either 255 characters, or up until the first newline, whichever comes first.
But there is a flaw with this, ignore does not only consider input in the buffer, so if there was NOT a '\n' character in the buffer when this bit of the code runs, cin will actually ASK THE USER FOR MORE INPUT in order to complete the ignore call. So this is not the function that you actually want there.

The correct call would be:
std:cin.sync();
Which will do exactly what was intended, it will clear cin's buffer so that that next call will definitely have to do an actual request to the user for input, rather than just reading from the buffer.

So what you really want is:
std::cin.clear();
std::cin.sync();
std::cin.get();
std::cin.clear();
std::cin.sync();

Which will ensure that you both ask the user for input exactly once, and that the cin is left in a ready-state for the next code to call it.
Report Abuse
Multidimensional is not online. Multidimensional
Joined: 05 Apr 2012
Total Posts: 866
29 Jun 2012 03:21 PM
Stravant always has the best explanations.

Stravant, what type of music do you listen to?
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
01 Jul 2012 12:05 PM
Thanks all for the explanation.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image