Reputation: 66
The error is at this line: if (strcmp(answer[i],ans[i]) == 0) Sorry if this is a dumb question, I already looked through most of the old posts but cannot get an answer.
int main()
{
int score=0;
char answer[20]={'T','F','F','T','F','F','T','T','T','T','F','F','T','F','T','F','T','F','T','T'};
char ans[20]={'F','T','F','T','F','F','T','T','F','T','F','F','T','F','T','F','T','F','T','T'};
for(int i=0;i<20;i++)
{
if (strcmp(answer[i],ans[i]) == 0)
{
score++;
cout << "No. " << i+1 << " is correct!" << endl;
}
else
{
cout << "No. " << i+1 << "is false!" << endl;
}
}
cout << "Score: " << score << endl;
return 0;
}
Upvotes: 1
Views: 85
Reputation: 2250
The signature of strcmp
:
int strcmp ( const char * str1, const char * str2 );
It takes char*
arguments and is designed to compare null terminated strings. You pass an argument of the wrong type char
. In addition, the usage logic is violated - strcmp
returns 0 if the contents of both strings are equal, so match checks !strcmp(str1, str2)
. To compare two chars just do
answer[i] == ans[i]
Upvotes: 0
Reputation: 66
I use char instead of string so no need strcmp. Just
if (answer[i] == ans[i])
Thanks! @JohnnyMopp
Upvotes: 2