Help with a C++ Login Program

Started by
18 comments, last by augy 19 years, 8 months ago
No: user is of type "char *", which means *user is of type char. *user is the first letter of the string, or 'M'.
Advertisement
oh yeah damn those strings confuse me so lol
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
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.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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...

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;}
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
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
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...
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.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
Polite note...

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
______________________________________________________________________________________________________
[AirBash.com]
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>

This topic is closed to new replies.

Advertisement