neocytrix Posted October 18, 2004 Report Share Posted October 18, 2004 Hello all, I am trying to get back into C++ so I am doing little scripts in my spare time and I can't figure out why this won't work. #include <iostream> char strUser[10]; char strPass[10]; char strProgPass[10]; char strLogin[10]; using namespace std; int main() { cout<<"Hello, please choose a user name "<<endl; cin >> strUser; cout<<endl<<"Hello "<< strUser<<". Please choose a password"<<endl; cin >> strPass; cout<<endl<<"The user name you have entered is "<<strUser<<endl; cout<<endl<<"The password you have entered is "<<strPass<<endl; cout<<endl<<"Log in as "<<strUser<<"? (y,n)"<<endl; cin >> strLogin; if (strLogin == "y" ){ cout<<endl<<"Enter password: "<<endl; } return 0; } The thing is, when you type your user name/ password and hit "y" the program just ends, it doesn't even display "Enter password: " before it ends. Im stumped Also, if someone is feeling like showing me something, how would I use a 'while' statement to make it so the user could keep trying to enter their password? -neocytrix Quote Link to comment Share on other sites More sharing options...
Urza9814 Posted October 18, 2004 Report Share Posted October 18, 2004 (edited) hmm...case sensitive perhaps? Are you running it from konsole or something? if not it's most likely that it just closes too fast...other than that I got no ideas... *compiles it* hmm...it doesn't...that's weird... well, I got nothing...and I don't have time to mess with it right now... edit: I had a few extra minutes...I think I found your problem. strLogin is a 10 char array...and you are comparing it to one character...maybe... Edited October 18, 2004 by Urza9814 Quote Link to comment Share on other sites More sharing options...
pmpatrick Posted October 19, 2004 Report Share Posted October 19, 2004 I think Urza is spot on. Change the line to: char strLogin; Quote Link to comment Share on other sites More sharing options...
neocytrix Posted October 19, 2004 Author Report Share Posted October 19, 2004 Okay thanks :-D I'll take that the problem are solved and mark the thread solved - Artificial Intelligence Quote Link to comment Share on other sites More sharing options...
jfsgr8 Posted October 22, 2004 Report Share Posted October 22, 2004 (edited) Hello all, I am trying to get back into C++ so I am doing little scripts in my spare time and I can't figure out why this won't work. #include <iostream> char strUser[10]; char strPass[10]; char strProgPass[10]; char strLogin[10]; using namespace std; int main() { cout<<"Hello, please choose a user name "<<endl; cin >> strUser; cout<<endl<<"Hello "<< strUser<<". Please choose a password"<<endl; cin >> strPass; cout<<endl<<"The user name you have entered is "<<strUser<<endl; cout<<endl<<"The password you have entered is "<<strPass<<endl; cout<<endl<<"Log in as "<<strUser<<"? (y,n)"<<endl; cin >> strLogin; if (strLogin == "y" ){ cout<<endl<<"Enter password: "<<endl; } return 0; } The thing is, when you type your user name/ password and hit "y" the program just ends, it doesn't even display "Enter password: " before it ends. Im stumped Also, if someone is feeling like showing me something, how would I use a 'while' statement to make it so the user could keep trying to enter their password? -neocytrix <{POST_SNAPBACK}> Some recomendations: 1) Instead of using C style arrays to contain your user and password, why not use the STL string class? It's so much easier. It will also make you a better C++ programmer. Most C++ "gurus", not the least of which is Bjarne Stroustrup (the creator of C++), emphatically tell people to try to use C++ with the STL, and fully utilize C++'s powerful libraries, rather than using C++ "as a better C", writing C style code. In other words, make coding easier on yourself, and only use lower level C stuff (char arrays, pointers to char, bit operators, etc) if you absolutely have to. Go for the higher level construcst that the STL provides. Use the string class in your program. 2) The program terminates immediately because nothing happens after entering the user and passord. You have the (proper) 'return 0;' at the end of the main event. So that's it for the program. If you want it to continue, perhaps you could put the user and password entry into a loop, and it only exits the loop until the user enters 'end' or 'q', or something like that. This would provide some continuity to the program. 3) In the 'if' statement, include both uppercase and lower case y, using the || (logical 'OR' opporator, just to make it irrelevant whether or not the user enters upper or lower case. Edited October 22, 2004 by jfsgr8 Quote Link to comment Share on other sites More sharing options...
tyme Posted October 25, 2004 Report Share Posted October 25, 2004 (edited) I think Urza is spot on. Change the line to: char strLogin; <{POST_SNAPBACK}> Urza is spot on :) you're comparing a string to a single char. the array actually contains an endline character after the y, and so your if statement returns as false every time - because it doesn't contain the endline. another option, if you want to keep the array (though it's not really needed), would be to write your if statement this way: if (strLogin[1] == "y" ){ cout<<endl<<"Enter password: "<<endl; } however if you are only looking to have 1 letter input then there's no need for an array. note that once it prints out "Enter password: " the program will end and drop you back to console. But I'm sure you knew that ;) Edited October 25, 2004 by tyme Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.