Thread: Why doesn't this cin.get() pause the program?

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    Why doesn't this cin.get() pause the program?

    I have been reading that for portatbility reasons it's best not to use system("pause") to pause the program to view the ouput. But for this program cin.get() doesn't pause the program. Any ideas why? BTW, this program is a simple program to generate passwords based upon the number of characters the user wants it to be.

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <time.h>
    int main()
    {
        int passwordLength = 0;
        std::string password;// = "";
        time_t seconds;
        time(&seconds);
        srand((unsigned int) seconds);
         
        std::cout<<"Enter the desired length for the password: ";
        std::cin>>passwordLength;
        
        for(int i = 0; i < passwordLength; i++)
        {
            int num = rand()%75 + 48;
            if(num >= 48 && num <= 122) //only want certain characters in pthe password
            {
                char c = (char)num;
                password += c; 
            }
            else
                i--;
        }
        std::cout<<"Password is: " <<password<<std::endl;
        std::cin.get();
        system("pause");
        
    }
    If I comment out the system("pause") it doesn't pause.
    Last edited by Loctan; 06-24-2006 at 03:01 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    cin.get() doesn't pause for input. it returns a char from the input buffer. only if that buffer is empty it waits for input before it retuns.
    In your case there is still the '\n' from the previous input in the buffer. get() returns that.
    Kurt

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I probably wouldn't use rand to generate my passwords for me. There has to be something better. Rand takes it's seed, and returns numbers in a discernable pattern. So many of the passwords you generate won't be that different from each other. Hmm...

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I think if you call cin.ignore(); after you take the input it will work as you intend. Correct me if I'm wrong but I think that ignores any chars left in the buffer.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Thanks for the help guys. Using the cin.ignore() before the cin.get() works fine.

    I was just using this program to mess around with generating random numbers, and I thought it would be interesting to see what kind of passwords it generates.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Correct me if I'm wrong but I think that ignores any chars left in the buffer.
    Actually, cin.ignore() ignores a single character in the buffer. In this case it works because it ignores the leftover newline which is the only character in the buffer if the user enters valid data. You could also use two cin.get() calls.

    If you want to ignore any characters left in the buffer, you would #include <limits> and use std::cin.ignore(std::numeric_limits<streamsize>::m ax(), '\n'). This has the extra benefit of ignoring all the characters if the user enters extra stuff after the input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  2. Why won't my program pause?
    By ladysniper in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2006, 03:22 AM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. How do I make my program pause
    By quiksilver9531 in forum C++ Programming
    Replies: 9
    Last Post: 01-23-2002, 07:38 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM