If else returning wrong

I need to do this log in program in Turbo. But whether I type the right password or not it always show the statement of else. Pls help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
char p[20],u[20];
cout<<"pass: \n";
cin>>p;
cout<<"user: \n";
cin>>u;

if(p=="word" && u=="name")
{cout<<"HI";}
else
{cout<<"HEY";}
getch();
}
Last edited on
p=="word" && u=="name"

p is a pointer. It stores a memory address.

"word" is some char values in memory. At a different memory address.

p=="word" says if (the memory that p is pointing to is the same as the memory hold the characters "word"

You're not comparing letters. You're comparing memory addresses.

Use string. Don't use char arrays. If Turbo-C++ even supports actual C++ strings? They were standardised in 1998, twenty years ago.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream.h>
#include <string>
#include<conio.h>
int main()
{

string p, u;
cout<<"pass: \n";
cin>>p;
cout<<"user: \n";
cin>>u;

if(p=="word" && u=="name")
{cout<<"HI";}
else
{cout<<"HEY";}
getch();
}


I won't waste time telling you not to use Turbo-C++ and that you're wasting your time using it and wasting your time trying to learn how to write C++ from 30 years ago. I expect you already know it's a waste of your time but you're stuck with it. I just hope that when you're done learning with it, you're able to find a time machine to take you back to 1990 so you can use your C++.

Last edited on
My school... is the the one to blame for . They expect us to use Turbo... thanks for explanation
If Turbo-C++ doesn't support strings, you can use the function strcmp to do what you want:

1
2
3
4
if(strcmp(p, "word") == 0  &&
   strcmp(u, "name") == 0)
{
  // success 
Topic archived. No new replies allowed.