Jump to content

Easy c++ question


Recommended Posts

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 :wall: 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

Link to comment
Share on other sites

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 by Urza9814
Link to comment
Share on other sites

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  :wall:  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

 

 

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 by jfsgr8
Link to comment
Share on other sites

I think Urza is spot on. Change the line to:

 

char strLogin;

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 by tyme
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...