cin.get() twice for effect

This is a discussion on cin.get() twice for effect within the C++ Programming forums, part of the General Programming Boards category; I am havin a prob, i need to use Code: cin.get(); twice in the following code to get the effect, ...

  1. #1
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29

    Angry cin.get() twice for effect

    I am havin a prob, i need to use
    Code:
    cin.get();
    twice in the following code to get the effect, if i do not it just continues without waiting to press enter...i added //ERROR HERE at the part in question
    Code:
    #include<conio>
    #include<iostream>
    using namespace std;
    
    extern string Name, Dwelling;
    extern int Health, MaxHealth, Gold, GoldBank, Exp;
    
    void FLHome()
    {
    	while(true)
    	{
    		clrscr();
    		cout<<"Welcome to your home "<<Name<<", and your current dwelling is a "<<Dwelling<<"!\n";
    		cout<<"Your health is "<<Health<<"/"<<MaxHealth<<", and you have "<<Exp<<" experience points\n";
    		cout<<"You have "<<Gold<<" gold coins in your pocet, and "<<GoldBank<<" Gold coins in the bank!\n";
    		cout<<"Select what you want to do:\n";
    		cout<<"c: Game credits.\ns: Vilage Square\n";
    		char Choice;
    		cin>>Choice;
    		if(Choice=='c')
    		{
    			clrscr();
    			FLCredits();
    			clrscr();
    			continue;
    		}
    		if(Choice=='s')
    		{
    			break;
    		}
    		else
    		{
    			cout<<"Invalid choice! Press ENTER\n";
    			cin.get();       //ERROR COMES IN HERE
    			cin.get();
    			continue;
    		}
    	}
    }

    any ideas and help would be appreciated
    Feel welcome to visit my web/wapsite at FantasticWap anytime.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    It's not an error at all.

    The reason cin.get() pauses the program is because when the cin buffer is empty, it waits for input in order to resolve. You called the the input operator ( >> ) and that left a newline in the stream. The first cin.get() takes that character. The second one waits for input.

    You can keep it as two cin.get() functions. Or after your last ( >> ) statement you can use a cin.ignore() statement.
    Sent from my iPadŽ

  3. #3
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    ahhhh, i new i was forgetting something.
    i usually do the whole cin.ignore line. is either of the two better to use?
    Feel welcome to visit my web/wapsite at FantasticWap anytime.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Not as far as I know.

    If I had to guess, I would say ignore() is quicker than get() since it doesn't require any sort of assignment, but I could be wrong.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Speed is not a consideration here (especially since you wait for user input immediately afterwards). Use whichever is clearer. I would consider ignore() to be clearer, since you are ignoring that extra character, but it really doesn't matter.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You have included conio, use one of its functions to get a key from user. <conio.h> has getch() for example. It is not good for portablity (conio), but because you included conio you can use it.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Why would you use getch() over cin.get()?

    Anyway, since you mention it, I hadn't noticed your use of " <conio> ". That's not a library. The .h extension was only removed from standard C++ libraries. <conio.h> is not one.
    Sent from my iPadŽ

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Using getch() is better because then there is no need for ignore and user can press any key to continue not only ENTER.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    so i need to use <conio.h> as <conio> is not valid? Why then does my compiler (Borland 5.5) does not give an error?
    Feel welcome to visit my web/wapsite at FantasticWap anytime.

  10. #10
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    your question isn't clear... you get an error when you use #include <conio.h>? or is it when you try to use <conio>? the first one should work, the second one shouldn't. and check out dev-cpp, it's a nice IDE that seems to follow the standards pretty well as far as i know
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    First, search for that file. Or if your IDE allows you, right click on it and open it.
    Second, why you included that when you didn't need it?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Nothing is impossible! 74466's Avatar
    Join Date
    Jan 2006
    Location
    Bloemfontein South-Africa
    Posts
    29
    Quote Originally Posted by linucksrox
    your question isn't clear... you get an error when you use #include <conio.h>? or is it when you try to use <conio>? the first one should work, the second one shouldn't. and check out dev-cpp, it's a nice IDE that seems to follow the standards pretty well as far as i know
    i do not get an error when using <conio> and i am going to need it later on
    Feel welcome to visit my web/wapsite at FantasticWap anytime.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Could be that Borland 5.5 allows #include <conio>, but you should use #include <conio.h>, if you decide to use conio at all (since it is not very portable).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    You need it for what?
    Anyway if you used one of it's functions, you'r free to use its other ones. But if you didn't, don't include that.
    Here people know how to replace conio functions with standard and portable ones.
    Last edited by siavoshkc; 03-02-2006 at 09:10 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    He is using clrscn() which is from conio.h. It's in my opinion the best way to clear the screen regardless of the fact that conio.h isn't portable.
    Sent from my iPadŽ

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 09:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 05:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 11:44 AM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 07:51 AM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21