Another Stupid question....

I am a noob in C++, Well just started learning for engineering in 11th and this is my question......??

I am trying to fix this program for simplest login password programme, could u help me out in this.

Note: I am trying to use this functions only so can u stick with it, thx .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
int main()
{using namespace std;
char username[12];
char password[12];
cout<<" Enter Username "<<endl<<endl;
cin>>username;
cout<<endl<<" Enter Password"<<endl<<endl;
cin>>password;

if(username=="raheel123"||username=="dania123")
{
if(username=="raheel123"&&password=="bigben123")
{cout<<" WELCOME    "<<username;
}
else if(username=="dania123"&&password=="smallben123")
{cout<<"WELCOME    "<<username;
}	
else(cout<<" Wrong password, try again  !!");
}
else(cout<<endl<<endl<<" Wrong Username");
return 0;
}



I am getting this output


Enter Username

raheel123

Enter Password

bigben123


Wrong Username
You cannot compare c-strings like that.
Use either std::string to store input (preferred) or use strcmp() function: http://www.cplusplus.com/reference/cstring/strcmp/
The operator == in your code does not do what you expect.

You should use std::string in stead of char arrays.
UMM @MiiNiPaa and @keskiverto

Im sorry but i couldnt understand what u said properly .....
can u please rewrite the programme correctly.. THX
1
2
3
4
5
6
7
#include<iostream>
#include <string>
int main()
{using namespace std;
char std::string username [12] ;
char std::string password [12] ;
//The rest is the same 
username and password are C-strings. C-strings do not support the equality operator ==. Therefore lines 11,13,16 are not valid.

As both MiNiPaa and keskiverto suggested, you should use C++ strings.
http://www.cplusplus.com/reference/string/string/

Change lines 4-5:
1
2
3
4
#include <string> 
...
string username;
string password;
Last edited on
@ MiniiPaa

Thanks a lot, sorry for a nooby question....
Bye

@AbstractAnon

Thanks for helping me again, do appreciate ur support...
BYE
Last edited on
Topic archived. No new replies allowed.