Как сравнить элементы структуры
Не работает проверка:
(authorization.UserName == ptr->UserName && authorization.Password == ptr->Password)
хоть и все данные верны (проверял через дебагер)
#pragma once
#include <iostream>
using namespace std;
#include <conio.h>
struct Person {
char UserName[30]{};
char Password[30]{};
};
char* InputUserName() {
char* username = new char[30] {};
cout << "UserName: ";
cin >> username;
return username;
}
char* InputPassword() {
char* password = new char[30] {};
cout << "Password: ";
cin >> password;
return password;
}
Person CreatePerson(const char* password, const char* username) {
if (username == nullptr || password == nullptr) {
throw "Name or surname can not be empty!";
}
Person person = Person();
strcpy_s(person.UserName, username);
strcpy_s(person.Password, password);
return person;
}
Person registration;
Person authorization;
Person* ptr;
void Registration();
void Authorization() {
int choice = -1;
do
{
system("cls");
cout << "\t\t\t\t_______________________Authorization__________________________" << endl;
Person authorization = CreatePerson(InputPassword(), InputUserName());
if (authorization.UserName == ptr->UserName && authorization.Password == ptr->Password) {
system("cls");
cout << "\t\t\t\t___________________Authorization Successfully___________________" << endl;
cout << "\t\t\t\tEnter '0' to continue: ";
cin >> choice;
if (choice == 0) {
while (EndFunc) {
system("cls");
cout << "\t\t\t\t_____________________Restaurant__________________________" << "\n\n";
PrintMenu(MainMenu, menuLength);
SelectedOption();
_getch();
}
}
}
else {
system("cls");
cout << "\t\t\t\t______________Incorrect password or username!__________________" << endl;
cout << "\t\t\t\t1. Repeat\n\t\t\t\t2. Registration\n\t\t\t\t3. Exit " << endl;
cout << "\t\t\t\tEnter: ";
cin >> choice;
if (choice == 1) {
system("cls");
Authorization();
}
else if (choice == 2) {
system("cls");
Registration();
}
else if (choice == 3) {
system("cls");
SetConsoleTextAttribute(console, 0x09);
cout << "\t\t\t\tThank you for using this program!\n\n\n";
SetConsoleTextAttribute(console, 0x0c);
EndFunc = false;
break;
}
}
} while (choice == 3);
}
void Registration() {
bool match = 0;
system("cls");
cout << "\t\t\t\t_________________________Registration_________________________" << endl;
Person registration = CreatePerson(InputPassword(), InputUserName());
ptr = ®istration;
match = 1;
system("cls");
cout << "\t\t\t\t______________________Registered Successfully______________________" << endl;
cout << "\t\t\t\tEntar '0' for Authorization: ";
bool choice = 0;
cin >> choice;
if (choice == 0)
system("cls");
Authorization();
}
Источник: Stack Overflow на русском