Thread: noob help on simple program

  1. #1
    Registered User
    Join Date
    Dec 2007

    noob help on simple program

    ive just started learning c++, im still in high school so i dont do it constantly, and im using the bloodshed compiler. I wrote this program and its not working quite as i had hoped. Here is the code.
    #include <iostream>
    using namespace std;
    int main()
        cout<<"Enter Password: ";
        string password;
        cin >> password;
        getline(cin, password ,'\n');
        if (password=="lala") {
             cout<<"Access Granted";
             _sleep ( 3000 );
        else {
             cout<<"Access Denied\n";
             cout<<"Closing Program\n";
    when running the program it will ask you for a password and when u enter "lala" the correct password it says access denied, then closing program, then it closes as if it was wrong. I looked over the code plenty of times and cant quite figure out the problem. pls help.

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    I'd rather you put those brackets at the same level as the ifs. Makes it easier to read.
    And what's the probem? Have you considered why you first use cin to read password and then later use getline to read into the same variable once again?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
        cin >> password;
        getline(cin, password ,'\n');
    This is wrong, and I'll explain why.

    Input to your program is buffered. When you type in your password, it might look like this:

    'l', 'a', 'l', 'a', '\n'....
    The first line you have will read until it hits '\n' in this case. Reading strings with cin, however, means that the '\n' char is left in the input buffer. That means the input buffer will look like this:

    getline() comes around and tries to read now. It reads into the same location you had cin read to. It reads this as an empty string and the output buffer is now empty because getline(), unlike cin, will eat up the '\n' char and discard it.

    Your variable password is now an empty string, not "lala".

    Solution: Get rid of the first line.

  4. #4
    Registered User
    Join Date
    Dec 2007
    thank you for the help that solved my problem. Thanks for the explanation that makes a lot more sense. Really quick to that was only like 10 mins from my original post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM