-2

I'm a student(newbie) and everything I've learned, I just applied here but string isn't clearly explained by our instructor. So I'm having problem with this code it doesn't accept the if else if and jumps on else. I'm using Turbo C++. I'm wondering what is wrong with here, since I'm totally newbie with these. Thank you in advance

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{

clrscr();

char user[50],pass[50];

cout<< "Username: ";
gets(user);
cout<< "Password: ";
gets(pass);

if (user=="user" && pass=="pass"){
cout<< "ACCESS GRANTED";
}else if (user=="user"){
cout<< "Wrong Password";
}else if (pass=="pass"){
cout<< "Wrong Username";
}else
cout<< "Wrong Username and Password";

getch();

}
Kosue
  • 11
  • 1

2 Answers2

0

I can't find it, but this is clearly a duplicate and has nothing to do with the turbo C++ compiler. In C, you cannot compare strings that way. You need to do if (strcmp(user,"user") == 0) (strcmp returns 0 if the two strings are equal, otherwise it returns a positive or negative to indicate which is alphabetically "first")

Foon
  • 6,148
  • 11
  • 40
  • 42
0

In short: you cannot compare char[] using operator ==.

You should replace == with strcmp

Galimov Albert
  • 7,269
  • 1
  • 24
  • 50