Jump to content
  • Advertisement
Sign in to follow this  

Help with a C++ Login Program

This topic is 5019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK so I am brand new at C++ and I've been working on a little LOGIN program. I think the issue is with my if/else if loops because the prog will compile, but when it runs, it goes through each sub-function instead of obeying my loop. Any help is appreciated. here is the source:

#include <iostream.h>

main()
{
    char user [100];    //100 char buffer for user name input
    char pass [100];    //100 char buffer for pwd input

    cout << "LOGIN: ";
    cin >> user;

        if (user == "Meat") //if user types "Meat"
        {
        cout << "Password: "  //ask for a pwd
        cin >>pass;  //assign input to the "pass" variable
        }
        
        else if (user != "Meat")
        {
        cout << "Invalid User!\n"; //other wise say no and try again
        cout << "LOGIN: ";
        cin >> user;  //assign input to the "user" variable
        }    
        
        if (pass == "password")  //if the correct pwd is entered
        {
        cout << "Login Ok\nGo: ";  //then say Ok & display a prompt
        }    

        else if(pass != "password")
        {
        cout << "Invalid Password!\nPassword: "; //otherwise try again
        cin >> pass;  //assign input to the "pass" variable
        }    

return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
Well the first thing I can think of is that you can't compare character arrays like that, you must use strcmp() or use a std (or other) string class.

so password == "password" would be strcmp(password, "password")==0

Share this post


Link to post
Share on other sites
You cannot compare C strings with ==. Use strcmp() instead. Or C++ strings.

And don't use iostream.h, it's bad - use iostream instead.

Share this post


Link to post
Share on other sites
The use of "==" to compare strings is incorrect,since what you're really doing is comparing the pointers,not their contents.Use the strcmp function.

-EDIT:Dammit!Beaten by just a few seconds!

Share this post


Link to post
Share on other sites
this:

user == "Meat"


isn't going to work, it doesn't compare each character. You can use the function strcmp, or make learning easier by using real c++ strings (you wont to get into the habbit of using this over C-style strings at some point so nows a good time) that allow you to do the above code e.g


#include <string>
#include <iostream>

int main() {

std::string user, password;

std::cout << "LOGIN: ";
std::cin >> user;

if (user == "Meat") { //if user types "Meat"

std::cout << "Password: " //ask for a pwd
std::cin >> password; //assign input to the "pass" variable
} else if(user != "Meat") {
std::cout << "Invalid User!\n"; //other wise say no and try again
std::cout << "LOGIN: ";
std::cin >> user; //assign input to the "user" variable
}

if (password == "password") { //if the correct pwd is entered
std::cout << "Login Ok\nGo: "; //then say Ok & display a prompt
} else if(password != "password") {

std::cout << "Invalid Password!\nPassword: "; //otherwise try again
std::cin >> password; //assign input to the "pass" variable
}

return 0;
}







also note that the headers iostream.h is deprecated so use the one without the ".h" on the end

[Edited by - snk_kid on August 22, 2004 11:51:10 AM]

Share this post


Link to post
Share on other sites
Thanks I'll play around with that. I dont understand what the "std::cout" is... I thought it was just "cout >> x" Also, I had a version where I used strcmp, but I must have had something different messed up because that version wouldnt compile. Thanks to everyone for the help though!

-augy

Share this post


Link to post
Share on other sites
Quote:
Original post by augy
Thanks I'll play around with that. I dont understand what the "std::cout" is...


They're the same thing. Unless you're using namespace std, you have to specify where you're getting cout from, which is within the std:: namespace. (A namespace is a way of keeping your functions from overlapping with other people's who might have used the same names)

Share this post


Link to post
Share on other sites
One thing I noticed about your program is if the user enters "pork" for the username, he gets taken to the invalid username block of code. But, if he THEN enters the correct username "Meat", your program ignores that. Sort of like "You better get it right the first time". Use some sort of a loop.

Share this post


Link to post
Share on other sites
You mention using a loop, yet there are none present in the code that you provided. Each if and else statement will be executed IN THE ORDER THAT THEY ARE WRITTEN. In order for the program to work as you expected it to, you will need to insert a couple of while or do while loops in the appropriate places.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
The use of "==" to compare strings is incorrect,since what you're really doing is comparing the pointers,not their contents.Use the strcmp function.

-EDIT:Dammit!Beaten by just a few seconds!


So what if you did (*user == "Meat") would that work?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!