warning in char data type

hello everyone ,
I have a warning massage from the compiler when i enter a multi-character in char :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,total;
    char ch,var;
    total=0;
    cin>>n;
    for(int a=1;a<=n;a++)
    {
        cin>>ch>>var;
        if((ch=='++')||(var=='++'))
        {
            total++;
        }
        else if((ch=='-')||(var=='-'))
        {
            total--;
        }
    }
    cout<<total;
    return 0;
}

what should i do to fix this ??
closed account (SECMoG1T)
Consider using a string if possible , reason is if multiple characters are being read into char then most of them will be leftover in your input buffer. Btw exactly what warning did you receive.
really i don't know how to use strings i wish if there is another way . this is the warning
warning: multi-character character constant [-Wmultichar]|
closed account (SECMoG1T)
Okay i'll give you a clue

In you code the error sprout from comparing a char to a string like literal
if((ch=='++')||(var=='++'))
The above line is in error because a char variable can only hold a single char and a char literal can not hold more than one chat ie. '++'/*invalid char litetal*/ '+','+'/*two valid char litetals */

'++' //means you want to use either an array of chars i.e. const char* or std::string

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
31
32
 
 #include<iostream> 
 #include<string> // prefer std::string to const char*

  using namespace std; 

  int main()
    {
       int n,total=0; 

       string ch,var, incr ("++"), decr ("-"); 
       /// prompt the user that you expect some inputs
       cin>>n; 

       for(int a=1;a<=n;a++)
          {
              cin>>ch>>var; 
                if((ch==incr)||(var==incr)) 
                  {
                     total++;
                  } 

               else if((ch==decr)||(var==decr)) 
                 { 
                     total--;
                  } 
          } 

         cout<<total;
          return 0;

     }


you can read more about strings here
http://www.cplusplus.com/reference/string/string/
http://en.cppreference.com/w/cpp/string
http://www.cprogramming.com/tutorial/string.html
Last edited on
(ch=='++')||(var=='++')

You have 2 characters between the ''.
Multicharacter literals (something like '++') are not characters. They are integers. Implementation defined integers to boot, so you cannot really say anything aside that it is some integer. It is C leftover, and almost always used incidentally and is undesireable and sign of mistake, that is why it emits a warning. Just like trigraphs.

It is simple: you cannot store several characters in data type serving to store single characters.
closed account (SECMoG1T)
Haha I too had no idea of this
Multicharacter literals (something like '++') are not characters. They are integers.Implementation defined integers to boot, 
that av not heard about wow, so @Miinippa are they of any use or are they applicable in a program and what would they be used for , coz it's like they dark syde outweigh their advantages, how are they integers haha ?? Thank you..
how are they integers
http://coliru.stacked-crooked.com/a/dec059414d3dd633

what would they be used for
Like trigraphs they are almost useless and superseded by other language features.
Sometimes you might use them as internal constants (poor man enumerations):
1
2
3
4
5
int code = 'succ';
if (something)
    code = 'err';
//...
if(code = 'err')
closed account (SECMoG1T)
Well that was amazing though i dint get a thing right there hehe , btw I could see their typeid ... maybe one of these day i'll check 'em out and (trigraphs too) Btw just for adventure hehe .
Thanks @Miinipaa , th was gr8
Last edited on
Topic archived. No new replies allowed.