How to compare two "chars"

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;
}
You can't compare char arrays with ==
You need to use strcmp or even better stricmp.
http://www.cplusplus.com/reference/cstring/strcmp/
http://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/rtref/stricmp.htm
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)...
Thanks!
Welcome :)
Topic archived. No new replies allowed.