Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I want the user to enter his choice on which base to convert, first. Then the number in other bases is dispalyed. But it only works when the input number is an octal one. I appreciate if someone nice could take a look at the code to see what may be the issue. much thanks.

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

int main()
{
    int a;
    char c;
    printf("Enter a Base:(D,O,H)\n");
    c=getchar();
    printf("Enter a number:\n");
    switch(c)
    {
    case 'D':scanf("%d",&a);
    case 'H':scanf("%x",&a);
    case 'O':scanf("%o",&a);
    break;
    default:printf("Wrong Base!");
}

printf("Octal:%o\tHexadecimal:%X\tDecimal:%d\n",a,a,a);
}


What I have tried:

I searched and saw similar codes but the problem is unsolved yet.
Posted
Updated 18-Feb-23 2:15am

You are missing break statements at the end of your case statements. It should be:
C
switch(c) {
    case 'D' :   scanf("%d", &a); break;
    case 'H' :   scanf("%x", &a); break;
    case 'O' :   scanf("%x", &a); break;
    default  :   printf("Wrong Base!\n");
}

Otherwise, you get what is called a "fall-through", that is if cis 'D', then case 'D' is performed, and then execution "falls through" to case 'H' and so on. More recent compilers (gcc and clang, at least), may issue a warning for this, if told to do so. There are situations where you do want to have fall-through behaviour, so it's sometimes useful to have it. Related is the situation where you have more than one case value for which you want the same action. For example you might have
C
switch(c) {
    case 'd':
    case 'D':
      scanf("%d, &a); break;
   /* etc */
} 
This would allow the user to get the expected action regardless of the case of the letter they entered.

But you might want to consider what to do if the user inputs invalid data, for example what happens if they choose Octal then enter the number 99, or "Hello". Also, consider what happens if the user enters "octal" in response to the prompt for the base.
 
Share this answer
 
The main error in the above code has already been described by K5054. Also the hint how to react if the user enters invalid data is very important in practice. First of all you should define which inputs are allowed. Here negative numbers, invalid numbers or also too large numbers would be a topic.

To keep the program clear, I first suggest to define functions that solve subtasks.Also special data types help to keep the program readable.
C
typedef enum { M_NONE, M_DEC, M_HEX, M_OCT } basetype;

int print_base(basetype bt, char* instr);
basetype get_base();

The main program could then look something like this :
C
int main()
{
    char buf[25];

    for (;;) {
        basetype bt = get_base();
        if (bt == M_NONE)
            puts("illegal base");
        else {
            printf("Enter a number:\n");
            fgets(buf, sizeof(buf) - 1, stdin);
            print_base(bt, buf);
        }
    }
    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900