Home > database >  How can you use a function and switch in the same c program?
How can you use a function and switch in the same c program?

Time:12-03

This is my code the only thing I can do is quit. There are two functions one where it calculates Fahrenheit to Celsius and the other does the opposite and there is an array that stores the data as well. Then I added the switch at the end. I understand each concept individually but don't know how to bring them together.

#include <stdlib.h>
#define SIZE 1


//C = (5 / 9) * (F - 32) and F = (9 / 5) * C   32
// calculate f to c
int getValue() {
    int result;

    printf("Input degree: ");
    scanf_s("%i", &result);

    return result;
}
int F (int input[], int size) {
    int result = 0;
    int a;
    for (a = 0; a = size; a  ) {
        if (input[a] = result);
        result = (9 / 5) * input[a]   32;
    }
    return result;
}
int C (int input[], int size) {
    int result = 0;
    int a;
    for (a = 0; a = size; a  ) {
        if (input[a] = result);
        result = (5 / 9) * (input[a] - 32);
    }
    return result;
}
main() {
    int choice;
    int input[SIZE];
    int F, C;
    int a;

    for (a = 0; a < SIZE; a  ) {
        input[a] = getValue();

        F = (input, SIZE);
        C = (input, SIZE);

    }


    do {
        printf("Welcome to the Main Menu\n");
        printf("1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius\n");
        printf("2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit\n");
        printf("3. Quit.\n");
        scanf_s("%i", &choice);

        
    } while (choice != 3);

    switch (choice) {
    case 1:

        printf("%i degree C\n", F);
        break;
    case 2:
    
        printf(" % i degree F\n", C);
        break;
    case 3:
        printf("You have chosen option 3 you are now able to quit\n");

        break;
    }
    
    system("pause");
}```

CodePudding user response:

Beside the errors pointed in the answer by @Mehmet Aslan, you should move the switch block into the while loop, so it gets executed each time the user selects an option. As it is now, it will only be run once, when the user has chosen the option "3"

CodePudding user response:

Since this looks like homework here is constructive feedbacks. Hopefully you ll manage.

Hints: Look into pointers

int getValue();
void F(int *output, int *input, int size);
void C(int *output, int *input, int size)
{
    for(int i = 0; i < size;   i)
    {
        output[i] = (5 / 9) * (input[i]- 32);
    }

}
#include <stdlib.h>
#define SIZE 1


//C = (5 / 9) * (F - 32) and F = (9 / 5) * C   32
// calculate f to c
int getValue() {
    int result;

    printf("Input degree: ");
    scanf_s("%i", &result);

    return result;
}
int F (int input[], int size) {
    int result = 0;
    int a;
    for (a = 0; a = size; a  ) {
        if (input[a] = result);
        result = (9 / 5) * input[a]   32;
    }
    return result;
}
int C (int input[], int size) {
    int result = 0;
    int a;
    for (a = 0; a = size; a  ) {
        if (input[a] = result);
        result = (5 / 9) * (input[a] - 32);
    }
    return result;
}
main() {
    int choice;
    int input[SIZE];
    int F, C;
    int a;

    for (a = 0; a < SIZE; a  ) {
        input[a] = getValue();

        F = (input, SIZE); <<---compile time error, not a function call
        C = (input, SIZE); <<---compile time error, not a function call

    }

    // F(output, input, SIZE) <<--- it should be here, so you could calculate after collect inputs

    do {
        printf("Welcome to the Main Menu\n");
        printf("1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius\n");
        printf("2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit\n");
        printf("3. Quit.\n");
        scanf_s("%i", &choice);

        
    } while (choice != 3);

    switch (choice) {
    case 1:

        printf("%i degree C\n", F); <<---F compile time error, not a function call
        break;
    case 2:
    
        printf(" % i degree F\n", C); <<---C compile time error, not a function call
        break;
    case 3:
        printf("You have chosen option 3 you are now able to quit\n");

        break;
    }
    
    system("pause");
}
  • Related