How to store character more than 1 and use it in selection

I don't know how to use selection if the character is more than 1 (ex: code=="B34") since char can only be used for single chara

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

     cout<<"Enter category [B:Baby / K:Kitten / A:Adult] >> ";
     cin>>category;

     if(category=='B')
     {
          strcpy(type,"Baby");
          strcpy(product_name,"Baby Cat 34");
          cout<<"Enter Product Code >> ";
          cin.getline(code,30);
          if(code=="B34")                 \\ here...how to compare it
          {
               cout<<"Enter Weight >> ";
               cin>>weight;
               if(weight==2)
               {
                  price=84;
               }
               else if(weight==4)
               {
                  price=115;
               }
               else
               {
                  cout<<"Product do not exist \n";
                  cout<<"Please try again"<<endl;
               }
          }
  


I use borland C++ old one
You're absolutely correct. The solution is to use a string.
http://www.cplusplus.com/reference/string/string/

Strings are great. Use them if you can.

If you can't use strings (some professors forbid them in early lessons), an array of characters is an option:

1
2
const int MAX_ARRAY_SIZE = 20;
char example[MAX_ARRAY_SIZE];

Arrays in general are important to know and a pain in the butt to use. When possible, use vectors instead of arrays and strings instead of char arrays.
Last edited on
I assume that code is a char array so you need to use strcmp -
http://www.cplusplus.com/reference/cstring/strcmp/
1
2
3
  cin.getline(code,30);
  // if(code=="B34") works only with string but not char arrays
  if (strcmp(code, "B34") == 0) // #include <string.h> 

Topic archived. No new replies allowed.