Multi-character warning while get a char from keyboard in c - c

According to the operation entered from the keyboard, I want to do 5 operations with the switch structure, but gives an error. I tried also getchar & putchar functions...
int main()
{
char proc;
int firstNum,secondNum,result;
printf("* Multiplication\n/ Division\n+ Add \n- Minus\n%c Mode", '%');
printf("\nEnter the first number: ");
scanf("%d",&firstNum);
printf("\nEnter the second number: ");
scanf("%d",&secondNum);
printf("\nEnter the process: ");
scanf("%c",&proc);
switch(proc) {
case '*':
result=firstNum*secondNum;
printf ('%d',result);
break;
case '/':
result=firstNum/secondNum;
printf ('%d',result);
break;
case '+':
result=firstNum+secondNum;
printf ('%d',result);
break;
case '-':
result=firstNum-secondNum;
printf ('%d',result);
break;
case '%':
result=firstNum%secondNum;
printf ('%d',result);
break;
default:
printf('Warning!');
break;
}
warning: multi-character character constant [-Wmultichar]
warning: passing argument 1 of ‘printf’ makes pointer from integer
without a cast [-Wint-conversion]

For starters use
scanf(" %c",&proc);
^^^
(see the blank before the character &) instead of
scanf("%c",&proc);
And use double quotes to specify string literals in statements like this
printf ( "%d",result);
^^^^
or this
printf("Warning!");
^^^ ^^^
And you forgot one closing brace in the end of the program.

Related

Basic C - Switch cases duplication

I am working on a project, where you answer a char, and if it is not one of the 4 answers, it tells you to try again. If it is not the correct one of the 4, it stops running. Here is a snippit.
#include <stdio.h>
#include <stdbool.h>
bool switchCheck = true;
char answer;
printf("A. English\nB. French\nC. Spanish\nD. German\n");
do{
scanf("%c\n", &answer);
switch (answer){
case 'C':
printf("Very nice ");
break;
case 'B':
case 'A':
case 'D':
printf("Sorry! Incorrect. Code Ends\n");
switchCheck =false;
break;
default:
printf("Try Again\n");
}
}while(switchCheck==true);
For some reason, when I input A, B, or D, it first prints the result for default, and if I do it again immediately afterward, it gives me the right input. Any help?
Thanks!
You are using scanf like
scanf("%c\n", &answer);
Instead use
scanf( " %c", &answer );
See the blank before the conversion specifier %c. It allows to skip white spaces.

Why is the default case matched in this switch statement?

I've got a question about switch statements.
Here is my code:
#include<stdio.h>
int main()
{
float a=0.0f;
float b=0.0f;
char operation=0;
printf("Enter expression:");
scanf("%f %c %f",&a,&operation,&b);
switch(operation)
{
case '+':
printf("=%.2f\n",a+b);
break;
case '-':
printf("=%.2f\n",a-b);
break;
case '*':
printf("=%.2f\n",a*b);
break;
case '/':
if(b==0)
printf("\ndivision by zero error.\n");
else
printf("=%.2f\n",a/b);
break;
case '%':
if(b==0)
printf("\ndivision by zero error.\n");
else
printf("=%d\n",(int)a%(int)b);
break;
default:
printf("invalid operation\n");
break;
}
return 0;
}
And this is result about two different input, one right, one wrong.
Why, when I enter two letters instead of two numbers, does it go into the default case?
a+b won't match the format string of your scanf since it expects floats not chars (like a or b), therefore scanf does not do anything.
scanf returns the number of items it was able to read which will be 0 in this case. Checking its return value is not a bad idea.
And since operation is initialized to 0 the default case will execute.
scanf("%f %c %f",&a, &operation, &b);
So, when you enter a+b:
'a' is not a float
scanf fails (you can check this by looking at its return value)
operation is still with its default value which is 0
Inside the switch statement, none of the cases('+', '-', '*', '/', '%') get matched because char operation = 0;
Therefore, the default block is executed.
Because you need to check the return value of scanf
// scanf("%f %c%f", &a, &operation, &b);
if (scanf("%f %c%f", &a, &operation, &b) != 3) {
fprintf(stderr, "Unable to convert input!\n");
exit(EXIT_FAILURE);
}

Why this switch case goes to the default case?

The code is supposed to check for a vowel input. If found, it would print "Vowel" and if there is none it should print "consonent". But the compiler is going to the default case regardless of the input and I can't find where the mistake is. Please Help.
Here's my code:
#include<stdio.h>
void main()
{
char ch;
printf("Insert a Char \n");
scanf("%d", &ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("Vowel");
break;
default:
printf("Consonent");
}
}
The conversion specifier %d in this call
scanf("%d", &ch);
is invalid. It tries to read a number. So entering a letter like 'A' results in input error.
The compiler can issue a warning or even an error reporting that for example
format ‘%d’ expects argument of type ‘int *’, but argument 2 has type
‘char *’
Because using an invalid format can result in undefined behavior.
Here is a demonstrative program
#include <stdio.h>
int main(void)
{
char s[] = "ABCD";
puts( s );
scanf( "%d", ( int * )s );
puts( s );
return 0;
}
If for example even to enter a valid ASCII code as for example 65 of the letter 'A' then the program output might look like
ABCD
A
That is the memory occupied by the array was overwritten.
Instead use the following call.(if you want to skip white spaces)
scanf( " %c", &ch );
or the following call
scanf( "%c", &ch );
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
First thing %d accepts an integer, not a character so correct this and run then place break after each case ends if you don't place break it will run all case till the end which is the default.
Example:-
char ch;
printf("Insert a Char \n");
scanf("%c", &ch);
switch(ch)
{
case 'a':printf("Vowel");
break;
case 'e':printf("Vowel");
break;
case 'i':printf("Vowel");
break;
case 'o':printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
default:
printf("Consonent");
}
Hope it will help.
Happy coding :-)
you should use "%c" as the format. when you use "%d", the value is 0 (because no integers have been detected in the input string). Refer to https://en.cppreference.com/w/c/io/fscanf#Example.
This issue, and void main() instead of int main(), should produce a compiler warning.
Thanks Everyone. I appreciate the help!
I should have noticed the data type though.
include
void main()
{
char ch;
printf("Insert a Char \n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
}

Code fails at case '0' - I don't understand why

Here is a minimal form of the code emphasizing the issue I have.
Code fails at "exit"(case '0') - program simply crashes. I suspect it is related to the while loop.
The issue occurs no matter what character i choose for the exit case (instead of '0').
#include <stdio.h>
void main()
{
int run=1;
char menu_option;
while (run==1)
{
printf("Choose case:\n");
scanf ("%s", &menu_option);
switch (menu_option) {
case '1':
printf("1");
break;
case '2':
printf("2");
break;
case '0':
run=0;
break;
default:
printf("Wrong input, try again\n");
}
}
}
menu_option is not a string, so %s is the wrong format specifier. You need %c, prefixed with a space to prevent whitespace (including newline) being interpreted as valid character input.
scanf (" %c", &menu_option);

A simple “printing” calculator

When I'm typing in a digit I see
Type in a digit 1
Type in an operator ERROR: Unknown operator!
accumulator = 0.000000
Type in a digit
Why step - printf("Type in an operator ") is skipped and is replaced by - default:
printf ("ERROR: Unknown operator!\n");
break;
Thanks for the help in advance!
// Program to produce a simple printing calculator
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
double accumulator = 0.0, number; // The accumulator shall be 0 at startup
char operator;
bool isCalculating = true; // Set flag indicating that calculations are ongoing
printf("You can use 4 operator for arithmetic + - / *\n");
printf("To set accumulator to some number use operator S or s\n");
printf("To exit from this program use operator E or e\n");
printf ("Begin Calculations\n");
while (isCalculating) // The loop ends when operator is = 'E'
{
printf("Type in a digit ");
scanf ("%lf", &number); // Get input from the user.
printf("Type in an operator ");
scanf ("%c", &operator);
// The conditions and their associated calculations
switch (operator)
{
case '+':
accumulator += number;
break;
case '-':
accumulator -= number;
break;
case '*':
accumulator *= number;
break;
case '/':
if (number == 0)
printf ("ERROR: Division by 0 is not allowed!");
else
accumulator /= number;
break;
case 'S':
case 's':
accumulator = number;
break;
case 'E':
case 'e':
isCalculating = false;
break;
default:
printf ("ERROR: Unknown operator!\n");
break;
}
printf ("accumulator = %f\n", accumulator);
}
printf ("End of Calculations");
return 0;
}
scanf for a char consumes the newline characters. So the scanned char is "linefeed" instead of the one you're expecting.
I replaced:
scanf ("%c", &operator);
by
scanf ("%*c%c", &operator);
(consuming linefeed before the operator without assigning it using %*c format)
and your code worked fine.

Resources