PDA

View Full Version : help with C++


x-scythe
06-24-2004, 07:59 PM
been here like a year now and decided i want to learn C++ so i can help out more.
ive been practicing a few things since some of it seems to be the same as PERL. i ran into an error tho...
what i want this simple program to do is ask people to type in a username and then i want it to ask them for a password. people who put the wrong pass in get this message "Incorrect Pass. Good-Bye." and people who put the right one get this message "You have accessed my information. You are lame!" For some reason tho, every time you put in your username you get the message you would get for a correct password instead of the "Enter Password" prompt.
heres is the code

//basic pass

#include <iostream.h>
#include <stdio.h>

int main()
{
//enter username
int name;
cout << "Enter Username:";
cin >> name;

//enter pass
int pass;
pass = 5;
cout << "Enter Password:";
cin >> pass;

if(pass != 5)
{
cout << "Incorrect Password. Good-bye.";
}
if(pass == 5)
{
cout << "You have accessed my information! You are lame!";
}

return;
}


thats it...any help would be appreciated greatly.

also if anyone could respond with what exactly the beginning "int main()" function does would also be appreciated hehe...i have no clue what its for. i know it has something to do with declarations maybe?

Virus11
06-24-2004, 08:42 PM
Weird, I spent like 20 miniutes and I still couldn't figure it out. Me and you are probally around the same c++ skill level :-(

mangoo
06-24-2004, 08:43 PM
#include <iostream.h>
#include <stdio.h>

int main()
{
//enter username
char name[32];
cout << "Enter Username:";
cin >> name;

//enter pass
int pass = 0;
cout << "Enter Password:";
cin >> pass;

if(pass != 5)
{
cout << endl << "Incorrect Password. Good-bye." << endl;
}
else if(pass == 5)
{
cout << endl << "You have accessed my information! You are lame!" << endl;
}

return 0;
}


Made a couple changes here.

1. Main problem you were seeing is because "name" was an integer and you were inserting text. So it immediately dropped to the if statements.

2. Changed "return;" to "return 0;"

3. Initialized "pass" to 0.

:D

*EDIT* Added a little spacing to the code. *EDIT*

*EDIT* Made the second if statement an else if, not a big deal in this size program, but it's good habit to get in to. *EDIT*

Scorpious2k
06-25-2004, 01:13 AM
Might want to change pass to a char too ....

x-scythe
06-25-2004, 05:16 AM
thanks for the help.
i dont understand tho what the [32] means after name....
thanks again

mangoo
06-25-2004, 06:23 AM
Scorp: Yes, but he had everything setup for the password to == 5, so I just left that alone :D .

x-scythe: the [32] designates the size of "name". So in this case, "name" is 32 characters long.