How to compare two "chars"
Sep 5, 2016 at 4:00pm UTC
Hi, I'm trying to compare two chars, one of those entered by the user.. if the person enters "si" it will transform to uppercase and then make the comparison.. unfortunately when I run the code it doesn't take them as similar.
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <stdio.h>
#include <ctype.h>
#include <conio>
#include <iostream>
int main()
{
int i = 0;
char str[5];
cin>>str;
while (str[i])
{
str[i] = toupper(str[i]);
i++;
}
cout<<str;
if (str == "SI" )
{
cout<<"It's equal." ;
}
getch();
return 0;
}
Sep 5, 2016 at 4:12pm UTC
Sep 5, 2016 at 4:14pm UTC
Here's a tweak to your code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
using namespace std;
int main()
{
int i = 0;
char str[5];
cin>>str;
while (str[i])
{
str[i] = toupper(str[i]);
i++;
}
cout<<str<<endl;
if (strcmp("SI" ,str)==0)
{
cout<<"It's equal." ;
}
return 0;
}
PS: It seems like you are using a old compiler (because of conio)...
Sep 5, 2016 at 4:21pm UTC
Thanks!
Sep 5, 2016 at 4:23pm UTC
Welcome :)
Topic archived. No new replies allowed.