Thread: Multiple users and passwords

  1. #1
    Registered User
    Join Date
    Jun 2016
    Location
    Copenhagen, Denmark
    Posts
    2

    Multiple users and passwords

    Hey everyone,

    I'm slowly working my way through "Jumping into C++" but can't seem to get passed this practice problem of creating multiple users and passwords (this is at p. 75 in the book, practice problem number 4). Right now I'm stuck trying to make it so two users, each with their own seperate password can log in, but only "user_1" and "password_1" seems to work.

    Could anyone please tell me what I'm doing wrong here?

    Thanks.


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string user_1;
        string user_2;
        string password_1;
        string password_2;
        cout << "Enter username " << endl;
        getline (cin, user_1, '\n' ) || (cin, user_2, '\n');
        cout << "Enter password" << endl;
        getline (cin, password_1, '\n' ) || (cin, password_2, '\n');
        if (user_1 == "diddy" && password_1 == "kiddy")
        {
            cout << "Welcome diddy! " << endl;
        }
        else if (user_2 == "daddy" && password_2 == "kaddy")
        {
            cout << "Welcome daddy! " << endl;
        }
        else
        {
            cout << "Wrong! Imposter!! " << endl;
        }
    }

  2. #2
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    well user_2 and password_2 never get any values. You only read on user_1 and password_1 variables(Getline function call). You have to compare user_1 to "daddy" and password_1 to "kaddy" too.

  3. #3
    Guest
    Guest
    Code:
    getline (cin, user_1, '\n' ) || (cin, user_2, '\n');
    Lines 11 and 13 are wrong, and I'm not sure what you were trying there. Consider that you don't need two variables to hold the user name, it's an either/or situation, not both. Increase your compiler warning level, it should have notified you about the offending lines.

  4. #4
    Registered User
    Join Date
    Jun 2016
    Location
    Copenhagen, Denmark
    Posts
    2
    Great. I'll give that a try. Thanks to both of you.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    getline is a function. You can only call functions by typing its name and then a paran. The part after the "||" will never call getline (because you aren't doing a function call), hence user_2, etc, won't ever get a value.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. default browser and multiple users
    By Waldo2k2 in forum Tech Board
    Replies: 6
    Last Post: 10-05-2003, 12:03 PM
  2. Passwords and what not.
    By w00tsoft 2 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-29-2003, 10:42 AM
  3. I know, I know...(passwords[****])
    By Sebastiani in forum Windows Programming
    Replies: 1
    Last Post: 03-29-2002, 01:49 PM
  4. Too... Many... Passwords...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 12-14-2001, 07:29 PM
  5. Passwords
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-21-2001, 12:53 PM

Tags for this Thread