cin.get problem

This is a discussion on cin.get problem within the C++ Programming forums, part of the General Programming Boards category; hi, in code below cin.get() doesn't work correctly (maybe I'm mistaken) Code: #include <iostream> #include <string> using namespace std; void ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    Question cin.get problem

    hi,

    in code below cin.get() doesn't work correctly (maybe I'm mistaken)
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void getPassword(void);
    bool Login(string, string);
    
    int main
    {
        string uname;
        string pass;
        int cmd;
    
        while (true)
        {
            cout << "Select your choose: " << endl
                 << "  1. Login" << endl
                 << "  2. What evere!" << endl
                 << "  3. Exit" << endl;
            cin >> cmd;
    
            switch (cmd)
            {
                case 1:
                    cin.ignore();
    
                    cout << "Enter username: ";
                    getline(cin, username);
                    cout << "Enter Passwoed: ";
                    password = getPassword();
    
                    if (!Login(username, password))
                    {
                        cout << "> Username or Password was wrong!" << endl
                                << "> Press Enter to continue" << endl;
                        cin.get();            // problem is here: I should press Enter twice!!?
                        cout << endl;         // and what ever cout here will be ignored
                        break;
                    }
                break;
    
                case 2:
                    // do something
                break;
    
                case 3:
                    return 0;
                break;
            }
        }
    }
    first time I press Enter, cursor will come at beginning of line. and next time, new line will overwrite on what would output in next line

    I have tried different solution I found over internet but cannot solve this.


    thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > password = getPassword();
    I see
    string pass;
    but no password.

    Also, where is getPassword() implemented?
    Does it leave the input stream in a nice clean state, like getline() does?

    Or does it leave random garbage on the stream, leaving you to press return a few times?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    this is getPassword function, and problem is exactly because of it.
    How should I solve it?
    Code:
    string getPassword()
    {
    	string result;
    
    	// Set the console mode to no-echo, not-line-buffered input
    	DWORD count;
    	HANDLE ih = GetStdHandle(STD_INPUT_HANDLE);
    	HANDLE oh = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleMode(ih, ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
    
    	// Get the password string
    	char c;
    	while (ReadConsoleA(ih, &c, 1, &count, NULL) && (c != '\r') && (c != '\n'))
    	{
    		if (c == '\b')
    		{
    			if (result.length())
    			{
    				WriteConsoleA(oh, "\b \b", 3, &count, NULL);
    				result.erase(result.end() -1);
    			}
    		}
    		else
    		{
    			WriteConsoleA(oh, "*", 1, &count, NULL);
    			result.push_back(c);
    		}
    	}
    
    	// Restore the console mode
    	SetConsoleMode(ih, (ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
    	cout << endl << endl << setw(70) << setfill('-') << "-" << endl << endl;
    
    	return result;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > while (ReadConsoleA(ih, &c, 1, &count, NULL) && (c != '\r') && (c != '\n'))
    If you're expecting both \r and \n, then this loop will leave one of them behind.

    Figure out which one comes last, then exit only on that one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  4. cin.get(); problem
    By J.P. in forum C++ Programming
    Replies: 19
    Last Post: 12-14-2006, 07:07 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

Tags for this Thread


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