Sign in to follow this  

Help with a C++ Login Program

This topic is 4859 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
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
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
Guest Anonymous Poster
No: user is of type "char *", which means *user is of type char. *user is the first letter of the string, or 'M'.

Share this post


Link to post
Share on other sites
If you declare all your strings as std::string instead of char* (or char [] as the case may be), then all of your inputs and comparisons will work as is. You must also #include <string> for this to work. The standard C++ library is your friend.

Notice how I didn't say your program would work as you expected because there is still the issue of the non-existant loops.

Share this post


Link to post
Share on other sites
So in addition to my current if/else statements I need to add a couple do/while loops? Because I had traded the if/else for do/whiles and no matter what I entered for a name, I got the invalid login. I guess I should stick to more basic C++ programming until I understand the fundamentals a little bit better...

Share this post


Link to post
Share on other sites
If you need help, don't be afraid to ask. I was just trying to let you get to loops (presumably in the tutorial you are following) before trying to force all the information down on you.

Here's what you want:

#include <iostream>
#include <string>

int main() {
std::string user, password;

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

while (user != "Meat") { //keep trying until user types the right username
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 (user == "exit") return 0; //allow an invalid user to quit the loop
}
//user must == "Meat" at this point

std::cout << "Password: " //ask for a pwd
std::cin >> password; //assign input to the "pass" variable

while (password != "password") { //while the incorrect pwd is entered
std::cout << "Invalid Password!\nPassword: "; //otherwise try again
std::cin >> password; //assign input to the "pass" variable
if (password == "exit") return 0; //again provide a last ditch escape
}
//password must == "password" at this point

std::cout << "Login Ok\nGo: "; //then say Ok & display a prompt
return 0;
}

Share this post


Link to post
Share on other sites
Thanks for the help. C++ is frying my brain. The only programming Ive ever done before is GWBASIC, Visual Basic, and a tiny bit of Perl. So I'm just trying to get involved in C++ and learn. I'll look over the new code and see if I understand it... Eventually I want to have this program check the user and password strings with a list of names/passwords from a seperate file. Also, I am gonna try to use some kind of encrption/decryption method to protect the names/pass. I know I've got a long way to go, but - I figure it will be a good learning process. All I have are 2 online tutorials until the books I ordered get here.

-augy

Share this post


Link to post
Share on other sites
One other thing, kind of off topic here - why is it "int main()" instead of just "main()" ?? I see that a lot, and I also tend to see something like "void main()" or "main (void)"

Just wandering if that makes a difference...

Share this post


Link to post
Share on other sites
Quote:
Original post by augy
One other thing, kind of off topic here - why is it "int main()" instead of just "main()" ?? I see that a lot, and I also tend to see something like "void main()" or "main (void)"

Just wandering if that makes a difference...


the int is a "return type". the type the function will return at its conclusion.
on some C compilers, there is an implied int added to functions without a return type (the missing int in this case). i think there has been a move to take this out however, so adding an int would be a good idea even if your compiler lets you get away with it.
in the case of main, it used to return 0 to DOS to signify normal termination and 1 if it terminated due to an error. i don't think this is the case anymore though.
main(void) == main()
void main(): is allowed, but is also i think being phased out.

the big one that you didn't mention is the commandline arguments. other than that there is almost no practical difference between any of those flavors of main.

Share this post


Link to post
Share on other sites
[u]Polite note...[/u]

It will be a good idea if you learn file handling and store/read passwords and usernames from a file.Oh yes, you will run into encryptions and stuff later on..

Learn filehandling as soon as possible,it's almost as easy as using cout an cin with fstream.It will possibly help you develop,learn in a better way, if you dont hard code everything (becomes a habit), forcing you to program modularly which is better.

There a tut link which might be intresting :) --
Fstream Tutorial

About Learning C++,
C++ has a new standard (from 98,right?).This new standard take into account a lot of new features C++ offers and bases it's style on it.So there is a diffrence from the old (popular?) way of programming, like the #include(s).So tuts,books from diffrent times will look conflicting,might work only after slight modification on new compilers etc.

Actually all the syntax and stuff is the same,but things like namespace, changes things a bit .

My suggestion is that you get a pure C++ programming book which follows the latest standard ,learn and absorb that style instead of confusing yourself.

Book suggestion: C++ Primer, 3rd Edition

Share this post


Link to post
Share on other sites
Thanks Thunder Hawk that code works great. I modified a little bit to suit my personal taste, but the do/while loops are finally cleared up for me. Before I was using a do/while for != and another do/while for ==. SO - Thanks to everyone for the help. The two books I ordered I got off amazon for like 20 bucks (used). On is called the "C++ Black Book: A Comprehensive Guide to C++ Mastery", and the other one is "C++: A Beginner' Guide"

heres my current source:
<quote>

#include <iostream>
#include <string>

int main() {
std::string user, password, exit;

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

while (user != "Meat") //keep trying until user types the right username
{ std::cout << "Invalid User!\n"; //other wise say no and try again
std::cout << "LOGIN: ";
std::cin >> user; //assign input to the "user" variable
}
//user must == "Meat" at this point

std::cout << "Password: "; //ask for a pwd
std::cin >> password; //assign input to the "pass" variable

while (password != "password") //while the incorrect pwd is entered
{ std::cout << "Invalid Password!\nPassword: "; //otherwise try again
std::cin >> password; //assign input to the "pass" variable
}
//password must == "password" at this point

std::cout << "Login Ok\n(Type 'exit' to quit!)\nGo: "; //then say Ok & display a prompt
std::cin >> exit;

while (exit != "exit")
{ std::cout << "Go: ";
std::cin >> exit;
}

return 0;
}

</quote>

Share this post


Link to post
Share on other sites

This topic is 4859 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this