Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <cmath>
#include <iomanip>
#include <conio.h>   //getche() icin
#include <cstring>   //stringler
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
/////////////////////////////////////////////////////

/////////////////////////////////////////////////////
int main(int argc, char** argv) {
	
	char username[20],password[20];
	char id[]="Username";
	char pw[]="Password";
	
	cout<<"\n\n  Welcome :)\n";
	
	for(int a=2;a>=0;a--){
		cout<<"\n\nYour Username: ";
		gets(username);
		cout<<"Your Password: ";
		gets(password);
		
		if(strcmp(id,username)==0 && (pw,password)==0){
			cout<<"Success!";
			cout<<"\n\Connecting...";
			break;
		}
		else{
			cout<<"\nWrong entry";
		}
		if(a!=0)
		cout<<endl<<a<<" tries left";
		else
		cout<<endl<<"Your account is blocked for 1 day";
	}
}


somehow if(strcmp(id,username)==0 && (pw,password)==0) doesn't be true even if id and password is correct. I'm probably using strcmp() wrong but i want to know where did i get wrong. Help?

What I have tried:

I tried a different version of this code and it worked but i really didn't get what did i do wrong on this one.
Posted
Updated 1-Apr-22 6:33am
Comments
jeron1 1-Apr-22 12:14pm    
"&& (pw,password)==0"

What is "(pw,password)" supposed to do? Do you want a second string compare? If so, do a second string compare;
if(strcmp(id,username)==0 && strcmp(pw,password)==0)
Member 15587265 1-Apr-22 13:19pm    
I actually wrote it if(strcmp(id,username)==0 && strcmp(pw,password)==0) like that but somehow i copied the code wrong. I was reworking the code to make it english ,since english is not my native language, i must forgot strcmp() on second one but it actually worked fine when i added it to the second one. I probably accidentally fixed it when i was reworking it lol.

1 solution

C++
if(strcmp(id,username)==0 && (pw,password)==0){

The expression (pw,password)==0 is invalid. The code should be:
C++
if(strcmp(id,username)==0 && strcmp(pw,password)==0){
 
Share this answer
 
Comments
k5054 1-Apr-22 13:06pm    
"the expression (pw,password) == 0 is invalid"
I think maybe you meant to say the expression is incorrect, not invalid, as it compiles just fine. If you turn on /Wall, the compiler will warn you that the expression before the comma has no effect. (Although that turns on a lot of other things you probably don't want, so use sparingly...)

The OP has inadvertently invoked the comma operator, see https://en.cppreference.com/w/cpp/language/operator_other#:~:text=function%20called%0A7.000000-,Built%2Din%20comma%20operator,-The%20comma%20operator
In brief (pw,password) == 0 evaluates pw (the address of the pw variable), throws that away, then evaluates password (the address of the passwword variable), and then uses that value as the left-hand-side of the comparison operation. Since password in on the stack, its address will never be zero (NULL), so the second comparison will always fail.
Richard MacCutchan 2-Apr-22 4:08am    
You are correct, of course. I just did not think an explanation of the comma operator was worth my, or the OP's, time.
k5054 2-Apr-22 10:52am    
I thought it worth while to explain why the compiler accepted the code. I'm sure we've all bumped into this from time to time. And will probably do so again. Fortunately, compiler warnings and debuggers make finding the brain-fades much easier these days!
Richard MacCutchan 2-Apr-22 11:06am    
As you can see from the OP's comment, it was a simple typo.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900