Jump to content
i was trying to make a simple calculator.
#include <stdio.h>
int main(){
int iNum1, iNum2, Result;
char Operator;
printf("Put the first number: ");
scanf("%d", &iNum1);
printf("Put the second number");
scanf("%d", &iNum2);
printf("what is the operator");
scanf("%c", &Operator);
 
switch (Operator) {
case"+": Result=iNum1+iNum2;
break;
case"-":
if(iNum1>iNum2){
Result=iNum1-iNum2; }
else{
Result=iNum2-iNum1; }
break;
case"*":
Result=iNum2*iNum1;
break;
 
case"/":
if(iNum1>iNum2){
Result=iNum1/iNum2; }
else{ Result=iNum2/iNum1; }
break; }
 
printf("the result is \n", Result); return 0; }
 
when i was trying to compile the code in the terminal in vscode there is a error in the case label. help me how to write the case label.

 

Link to comment
https://linustechtips.com/topic/1400851-problem-in-the-source-code/
Share on other sites

Link to post
Share on other sites

image.png.9e73983a0ca4e6f0177c22b0e5d15602.png

 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 

Your problem is that you're using " instead of '. " = string, ' = char. https://www.tutorialspoint.com/single-quotes-vs-double-quotes-in-c-or-cplusplus

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

First thing, please use the code tags functionality (<> button) as it makes things much easier to read.

 

2nd. Switch statements do not support strings or character arrays for comparisons. So by using double quotes instead of single quotes your specifying the value as a const char array instead of a const char. Basically double quotes creates a string* whereas single quotes create a character.

 

* note that C or C++ do not have a notion of strings like most languages but rather arrays of characters.

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to post
Share on other sites

Your big issue is that you're using " where you should be using '. In C/C++, " are used for strings and arrays of characters, ' are used for characters themselves like you're using in here. If you change it from say 

case"+": Result=iNum1+iNum2;

to 

case'+': Result=iNum1+iNum2;

the errors should go away. 

 

Also, your final line is wrong. It should be 

printf("The result is \n %d", Result); return 0; }

 

Also, as said above, use code tags, they're very useful.

Link to post
Share on other sites

@Souranil21 chakraborty I think everyone has covered the root cause...but there is also something else wrong.

 

While you can use switch without using default: it leads to Result not being set when you choose an invalid operator...which in turn means Result isn't initialized (just has a random memory value).

 

e.g.

int main() {
	switchTestNoDefault('+'); //Returns 7, and prints res: 7
    switchTestNoDefault('b'); //Returns random value of memory...in this case my guess would be 7...as the stack will fall in same location
    
    switchTestDefault('+'); //Returns 7, and prints res: 7
    switchTestDefault('b'); //Returns 0, and prints an error as well though.
    
	return 0;
}

int switchTestNoDefault(char op) {
	int res;
    int a = 4;
    int b = 3;
    switch(op) {
    	case '+':
        res = a + b;
    	break;
    }
    printf("res: %d", res);
    return res;
}

int switchTestDefault(char op) {
	int res;
    int a = 4;
    int b = 3;
    switch(op) {
    	case '+':
        res = a + b;
    	break;
        
        default:
        res = 0;
        printf("Error wrong opp");
        break;
    }
    printf("res: %d", res);
    return res;
}

 

 

Also when dividing using user input information you should always verify.  e.g. if they enter 0 for iNum2 and choose / you will get an error

3735928559 - Beware of the dead beef

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×