warning #2055: Excess characters in 'char' character literal ignored.

Started by sannemander, April 24, 2012, 10:30:28 AM

Previous topic - Next topic

sannemander

Hello,

I got a question about this code, I don't understand why I get this warning.

#include<stdio.h>

int main(void)
{

float CurrentVal, NextNum;
char ch;


printf("Enter an expression: ");
scanf("%f", &CurrentVal);

while ((ch = getchar()) != '/n') {

switch(ch){

case '+':
scanf("%f", &NextNum);
CurrentVal += NextNum;
break;

case '-':
scanf("%f", &NextNum);
CurrentVal -= NextNum;
break;

case '*':
scanf("%f", &NextNum);
CurrentVal *= NextNum;
break;

case '/':
scanf("%f", &NextNum);
if (NextNum == 0){
printf("False, cannot divide by 0!/n");
return 1;}
else{
CurrentVal /= NextNum;
break;}

}

}

printf("Value of expression is %.2f/n", CurrentVal);

return 0;

}


now I get warning 2055: Excess characters in 'char' literal ignored.

CommonTater

   '/n'  Should be... '\n'




sannemander

Ah you´re right, I have found the solution to the problem already, now it´s:

#include<stdio.h>

int main(void)
{

float CurrentVal, NextNum;
char ch;


printf("Enter an expression: ");
scanf("%f", &CurrentVal);

do{scanf("%c", &ch);


switch(ch){

case '+':
scanf("%f", &NextNum);
CurrentVal += NextNum;
break;

case '-':
scanf("%f", &NextNum);
CurrentVal -= NextNum;
break;

case '*':
scanf("%f", &NextNum);
CurrentVal *= NextNum;
break;

case '/':
scanf("%f", &NextNum);
if (NextNum == 0){
printf("False, cannot divide by 0!/n");
return 1;}
else{
CurrentVal /= NextNum;
break;}

}

}while (ch != '\n');

printf("Value of expression is %.2f\n", CurrentVal);

return 0;

}



Works perfectly now!