cin.get(); problem

This is a discussion on cin.get(); problem within the C++ Programming forums, part of the General Programming Boards category; Greetings, Noob here. I've recently started trying to learn C++. It would also be my first language to learn, so, ...

  1. #1
    Registered User J.P.'s Avatar
    Join Date
    Dec 2006
    Posts
    2

    cin.get(); problem

    Greetings,

    Noob here. I've recently started trying to learn C++. It would also be my first language to learn, so, I'm a major noob. Bare with me.

    In this code I wrote, testing out the things the first two lessons on this site taught me, the program just ends without me pressing enter. Cin.get(); is supposed to have it where you press enter to end the program right? So what am I doing wrong?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a;
    
    label:
    		cout<<"Password: ";
    		cin>> a;
    		if ( a == 8759 ) {
    			cout<<"Access granted.\n";
    		}
    		else {
    			cout<<"Your password entry is incorrect. Try again.\n\n";
    			goto label;
    		}
    		cin.get();
    }
    I'm writing the code in Visual C++ 2005 Express Edition, and compiling it in the command line compiler that comes with it.

    Any help would be appreciated

    -J.P.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    ew... you have gotos...

    try adding a second cin.get();
    I'm sure theres a better way but at least that works

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    It would be a good thing *not* to use goto in such simple cases. As for the issue, you want to add cin.ignore(1000, '\n') before it. Why ? Because it will flush all the junk that the first call to cin left in buffers so you can call cin.get() properly.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    When you use operator>> to read in a value (in this case the password into a), the user types the value and hits enter. The enter leaves a newline character in the input stream that is not read. When your code gets to the cin.get(), it gets that newline instead of waiting for the user to hit enter again.

    The solution is to use cin.ignore(), either just before the cin.get() or after any calls to cin >>.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Bare with me.
    J.P., I don't know what you've heard, but we're not that kind of board...
    There is a difference between tedious and difficult.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    goto reminds me of the ol' dos days (still had a brain back then).

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a;
    
    loop:  
            cout<<" \n";
    		cout<<"Password: ";
    		cin>> a;
    		if ( a == 8759 ) {
    			cout<<"Access granted.\n";
    			cout<<"Key to exit \n";
    			cin.get();
    		}
    		else {
    			cout<<"Your password entry is incorrect. Try again.\n";
    			goto loop;
    		}
    		
    		cin.get();
    }

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    >>It would be a good thing *not* to use goto in such simple cases.

    Its never a good thing to use gotos

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  8. #8
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Don't you need to add the
    Code:
     return 0;
    ?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a;
    
    loop:  
            cout<<" \n";
    		cout<<"Password: ";
    		cin>> a;
    		if ( a == 8759 ) {
    			cout<<"Access granted.\n";
    			cout<<"Key to exit \n";
    			cin.get();
    		}
    		else {
    			cout<<"Your password entry is incorrect. Try again.\n";
    			goto loop;
    		}
    		
    		cin.get();
                                    return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    Try this, it is the easiest way i have done...but goto is a bad idea...
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	int a;
    label:  
           	cout<<"Password: ";
    		cin>> a;
    		if ( a == 8759 ) 
            {
    			cout<<"Access granted.\n";
    			cin.get();
    		}
    		else 
            {
    			cout<<"Your password entry is incorrect. Try again.\n";
    			goto label;
    		}
    		cin.get();
                    return 0;                                                    //return 0; should be there when int main() is used!
    }

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> Don't you need to add the return 0?
    No. You do not need the return 0 for main. The main function must specify a return type of int, but it is special in that you don't have to explicitly return something. If the main function (and only the main function) finishes without a return value, 0 is automatically returned. Some people prefer to add it for consistency, others prefer to leave it out unless they are using the return value for some specific purpose. It doesn't really matter.

    BTW, a while loop (or a do while loop) is the alternative in this case instead of the goto, in case you actually wanted to change it to some thing better.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    He says he has recently started learning C++ so its better not to talk about the while loop now...

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by alokrsx
    He says he has recently started learning C++ so its better not to talk about the while loop now...
    it is better to talk about while loop now - before he is regular with goto approach

    better start learning right from the beginning than to switch later
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    Damn...
    I forgot he used goto statement there.
    Ooops!

  14. #14
    Registered User J.P.'s Avatar
    Join Date
    Dec 2006
    Posts
    2
    Thanks for the help! It helps when I don't have to run the program in the command line just to see the correct outcome. :-P

    BTW, I got rid of the goto.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a;
    
    		cout<<"Password: ";
    		cin>> a;
    		while (a != 8759) {
    			cout<<"Your password entry is incorrect. Try again.\n \n";
    			cout<<"Password: ";
    			cin>> a;
    		}
    		if (a == 8759){
    			cout<<"Access granted.\n";
    		}
    		cin.ignore(1000, '\n');
    		cin.get();
    }
    -J.P.

  15. #15
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    >>if (a == 8759){

    you dont need that. it will only go there if a is equal to 8759 because of the while loop

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

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

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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