warning in char data type

Dec 3, 2014 at 10:07am
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 ??
Dec 3, 2014 at 10:27am
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.
Dec 3, 2014 at 10:36am
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]|
Dec 3, 2014 at 11:30am
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 Dec 3, 2014 at 11:40am
Dec 3, 2014 at 11:30am
(ch=='++')||(var=='++')

You have 2 characters between the ''.
Dec 3, 2014 at 12:17pm
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.
Dec 3, 2014 at 12:37pm
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..
Dec 3, 2014 at 3:33pm
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')
Dec 3, 2014 at 4:54pm
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 Dec 3, 2014 at 5:03pm
Topic archived. No new replies allowed.