| Engineer1608 (2) | |
|
I am a beginner and would like to ask a question My Code: ///////////////////////// #include<iostream.h> #include<conio.h> void main() { clrscr(); cout<<"Enter The Password\n"; char a[10]; cin>>a; char b[]="rain"; if(a==b) cout<<"\nRIGHT"; else cout<<"WRONG"; getch(); } /////////////// When I enter "rain" it shows "WRONG". Thanks For Help | |
|
|
|
| NwN (770) | |||
|
Hi there, You are trying to compare c-strings, which are basically arrays of type char.This can't be done just using a==b, because the name of an array actually holds the memory address to the first element of the array.So in your case, you're actually asking "is the address of array a the same as that of array b?", which is off course not the case. The recommended solution is to use std::string, which will allow you to do such comparison:
Hope that helps. All the best, NwN | |||
|
|
|||