Home > OS >  Switch function directly goes to default. What's the issue?
Switch function directly goes to default. What's the issue?

Time:10-04

I am new to C and am writing a simple code of converting temperatures. The code is still incomplete but still should give me some output

#include<stdio.h>
void main()
{
    float temp;
    char choice;
    printf("\n 1. Celcius to Farenhite\n 2. Farenhite to Celcius\n What do you want to convert from? : ");
    scanf("%c", &choice);
    switch (choice)
    {
    case 1:
        printf("Enter temperature in Celcius: ", temp );
        scanf("%f", &temp);
        break;
    case 2:
    printf("Enter temperature in Farenhite: ", temp);
    scanf("%f", &temp);
    break;
    default:
    printf("Invalid Choice");
        break;
    }

}   

When I run this it asks "what do you want to convert from?" and shows the options. But when I enter 1 or 2, it directly prints and shows "Invalid Choice". Pls tell me what's wrong :(

CodePudding user response:

1 is 'int' and not a char.
1 and '1' are different.

This is the edited code

#include<stdio.h>
void main()
{
    float temp;
    char choice;
    printf("\n 1. Celcius to Farenhite\n 2. Farenhite to Celcius\n What do you want to convert from? : ");
    scanf("%c", &choice);
    switch (choice)
    {
        case '1':
            printf("Enter temperature in Celcius: ", temp );
            scanf("%f", &temp);
            break;
        case '2':
            printf("Enter temperature in Farenhite: ", temp);
            scanf("%f", &temp);
            break;
        default:
            printf("Invalid Choice");
            break;
    }

}   

CodePudding user response:

The number one is not the same as the digit "1". You are entering the character "1" and your switch statement is checking for the number one.

The number one is how many hearts I have. It can be written with the digit '1' but any number of other ways.

The digit '1' is a character. It is sometimes use to represent the number one but can also be used for other things. A variable of type char can hold the digit 1 since digits are characters.

Your switch statement is checking for the number one. You want to check for the character 1.

CodePudding user response:

'1' is a character, whereas 1 is an integer. Your switch statement can't find a case to match with, hence it goes to default.

Is that the complete code? You're not converting anything here, you're only asking the user for the temperature and storing it in a variable.

OT: Indent properly, your code is messy and difficult to read. And do not use scanf for taking input. You may want to check out this link http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html.

CodePudding user response:

This is a common misunderstanding for novices and occasional typo for experienced programmers(*).

1 means "the integer value one" (it is the integer literal for the value of 1).

Assuming your on an ASCII compatible platform(**) that is the character with ASCII code 1 (the non-printing character called Start of Header - SOH).

The ASCII Code for the character 1 is actually 49. But you should write:

case '1':

Because those apostrophes identify the 1 as the character representing 1 not the numeric value of 1.

The compiler will interpret '1' as actually meaning character of 1 meaning the (codepoint) value of 49.

In C char has a deeply dual role as a numeric type and as the smallest unit of text and accidental mixing of those roles causes endless confusion.

Arguably char has 3 roles. The smallest arithmetic type, the smallest unit of text (a character) and the unit of addressable memory (byte).

There are historical reasons why those roles are conflated in C.

(*) But I can't find a good answer for it even though there should be thousands of times this has been asked.

(**) C does not specify a character set but if you working on a platform that isn't ASCII compatible you will know about it. Unless you've found something in your Grandparent's basement and they're sadly not around to explain it. This footnote exists for the likely comments to the effect that C doesn't specify a character set.

CodePudding user response:

Your program reads a byte from stdin and compares that to the values 1 and 2. It is unlikely the user can type these byte values as they correspond to control characters CtrlA and CtrlB1. The byte values representing the digits 1 and 2 typed by the user are noted in C as '1' and '2'. These are called character constants and are int values for the encoding of the corresponding characters.

Furthermore, you should:

  • indent your code more consistently.
  • define main with a return type int
  • test the return value of scanf()
  • fix the spelling of Fahrenheit, named after German physicist Daniel Gabriel Fahrenheit and Celsius, named after Swedish astronomer Anders Celsius.

1: From a unix terminal, one can actually enter these byte values by hitting CtrlV CtrlA and CtrlV CtrlB, respectively, followed by Enter.

Here is a modified version:

#include <stdio.h>

int main() {
    float temp;
    char choice;

    printf(" 1. Celsius to Fahrenheit\n"
           " 2. Fahrenheit to Celsius\n"
           " What do you want to convert from? ");

    if (scanf(" %c", &choice) != 1)
        return 1;

    switch (choice) {

      case '1':
        printf("Enter temperature in Celsius: ", temp);
        if (scanf("%f", &temp) != 1)
            return 1;
        printf("%g degrees Celsius is %g degrees Fahrenheit\n",
               temp, 32   temp * 9 / 5);
        break;

      case '2':
        printf("Enter temperature in Fahrenheit: ", temp);
        if (scanf("%f", &temp) != 1)
            return 1;
        printf("%g degrees Fahrenheit is %g degrees Celsius\n",
               temp, (temp - 32) * 5 / 9);
        break;

      default:
        printf("Invalid choice\n");
        break;
    }
    return 0;
} 
  •  Tags:  
  • c
  • Related