Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I don\'t understand why this code is not comparing if the char are the same or n

ID: 3765653 • Letter: I

Question

I don't understand why this code is not comparing if the char are the same or not, and when it enters the loop, the if is always is true so it return false. I've modified it many times but it's still doing the same. I want to know, why if(license[i]!=license[i]); is always true.

#include <iostream>
#include <cstdlib>
#include <string>
#include <time.h>
#include "HashTable.h"

using namespace std;

struct Autoexpreso{
string name;
char license[6];
float balance;
bool operator == (const Autoexpreso &comp);
};

int main()
{
srand(time(0));
Autoexpreso num;
if(num==num)
cout << "ok";
else
cout << "no";
}

bool Autoexpreso::operator==(const Autoexpreso &comp)
{
for(int i=0; i<6; i++)
{
cout << license[i] << " igual? " << comp.license[i] << endl;
if(license[i]!=license[i]);
{
return false;
}
}
return true;
}

Explanation / Answer

you have placed ; semicolon at the end of if condition so it is always returning false pl. correct it to

if(license[i]!=license[i])
{
return false;
}