Hi. I made this program so i could verify if a certain molecule is an Hydroxide (if it ends in "OH") or not (it´s not for fun, it's for school). I believe the code should be correct, but for some reason it gives me wrong when it should be right, and right when it should be wrong. What is the problem here?
#include <iostream>
#include <cstring>
usingnamespace std;
bool isHydroxide(char compound[])
{
char term[] = "OH";
int size_str = strlen(compound);
char str2[] = {compound[size_str - 2], compound[size_str - 1],'\0' };
if (strcmp(str2, term))
returntrue;
elsereturnfalse;
}
int main()
{
char str[10];
cout << "Insert a molecule: ";
cin >> str;
if (isHydroxide(str))
cout << "It's an Hydroxide!" << endl;
else
cout << "It's not an Hydroxide! :( " << endl;
return 0;
}
When comparing the two c-strings it should give me 0 if they are equal but i debbuged the code and it gave me a value greater than 0. Why? Any Help would be great!
Thank You :)