Home > Software design >  Using function in switch statement in C [closed]
Using function in switch statement in C [closed]

Time:10-03

Hey I'm new to C just starting the course a few weeks ago. I'm trying to code simple calculator with add and minus so I tried using the switch statement but it doesn't work and the console keep showing default switch instead. Here is the code that I wrote.

#include <stdio.h>
#include <math.h>
int add(void);
int minus(void);
int result;

int add()
{
int num1, num2;
printf("Enter number 1 : ");
scanf("%d", &num1);
printf("Enter number 2 : ");
scanf("%d", &num2);
result = num1   num2;
return result;
}

int minus( )
int num1, num2;
printf("Enter number 1 : ");
scanf("%d", &num1);
printf("Enter number 2 : ");
scanf("%d", &num2);
result = num1 - num2;
return result;
}

int main( )
{
int number;

 printf("Enter OP number: ");
 scanf("d", &number);
 switch (number)
 {
     case 1 : add( );
     printf("The sum of 2 numbers is %d \n",result);
     break;

      case 2 : minus( ); 
     printf("The result of 2 numbers is %d \n",result);
     break;

     default:
     printf("Wrong...");
 }

 return 0;
}

CodePudding user response:

First of all when you define a function before main you don't need to declare it again at the top of your source file anymore since the computer reads your program from top to bottom line by line. Good practice is to define them after the main function or put them in separate files in larger programs and then declare them before main.

Second, If you want to use an external variable, declare it in your functions with the keyword extern for clarity, readability and to make clean program. But I don't think it's necessary to use an external variable here. Use an internal variable since the functions do not need to communicate with each other.

Third, your d in scanf("d", &number); needs a %. Your program is not reading any input so it's just going to default.

CodePudding user response:

First, scanf("d", &number) is just wrong. You need to give it a format specifier: scanf("%d", &number) (Note the %).

Second, scanf() is bad for reading input from the user. You should not use it in your programs. fgets() is a better alternative.

Third, avoid global variables as much as you can.

Fourth, function return values have a meaning, so always make sure to check them. See scanf() manual here.


Taking into account all of the above, here is a rewrite of your code:

1- Define a function that reads an integer from the user:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

// Returns false if it encouters an input error or the input is not an integer
bool input_int(int *i)
{
    char input[100]; // Max line size is 100. You can make it larger
    if (!fgets(input, sizeof input, stdin)) // Read one line
        return false;

    // fgets reads the last \n, so replace it with \0
    input[strcspn(input, "\n")] = '\0';

    if (sscanf(input, "%d", i) != 1) // Look for an integer
        return false;

    return true;
}

2- Since the above function can fail, you can define another function that forces the user to enter an integer:

void request_number(const char *msg, int *number)
{
    do {
        printf("%s: ", msg);
    } while(!input_int(number)); // Loop until the user enters an integer
}

It would be nice if you print a menu to guide the user through your program:

int main(void)
{
    printf("1:\tAdd two numbers\n");
    printf("2:\tSubtract two numbers\n");
    
    int choice;
    request_number("Enter OP number", &choice);

    int num1, num2;
    
    switch (choice) {
    case 1:
        request_number("Enter number 1", &num1);
        request_number("Enter number 2", &num2);
        printf("%d   %d = %d\n", num1, num2, num1 num2);
        break;
    case 2:
        request_number("Enter number 1", &num1);
        request_number("Enter number 2", &num2);
        printf("%d - %d = %d\n", num1, num2, num1-num2);
        break;
    default:
        printf("Wrong choice\n");
        break;
    }   
}

CodePudding user response:

There is a typo in this call of scanf

 scanf("d", &number);
       ^^^ 

Instead write

 scanf("%d", &number);
        ^^^

Also there i no reason to use the global variable result. Rewrite the functions add and minus like

int add( void )
{
    int num1 = 0, num2 = 0;

    printf("Enter number 1 : ");
    scanf("%d", &num1);
  
    printf("Enter number 2 : ");
    scanf("%d", &num2);

    return num1   num2;
}

int minus( void  )
{
    int num1 = 0, num2 = 0;

    printf("Enter number 1 : ");
    scanf("%d", &num1);

    printf("Enter number 2 : ");
    scanf("%d", &num2);

    return num1 - num2;
}

And in the switch block write

 case 1 :
 printf("The sum of 2 numbers is %d \n", add() );
 break;

 case 2 :  
 printf("The result of 2 numbers is %d \n",minus() );
 break;
  • Related