Home > Blockchain >  issue with switch statement c language
issue with switch statement c language

Time:09-27

So this is the code I'm working on. All is good except that I need to match the case with the counter so I can manage to display the desired output "you are in your tens / teens". I tried many different approaches but I couldn't get it right. However, I think the problem is within the condition. Any tips? Note: I'm just a beginner who just started programming in C this semester!

#include <stdio.h>

int main()
{
    int YoB, CY, Age;
    unsigned int counter = 0;
    printf("Please enter your year of birth");
    scanf_s("%d", &YoB);
    printf("Please enter the current year");
    scanf_s("%d", &CY);
    Age = CY - YoB;
    printf("Entered year of birth %d\n", YoB);
    printf("Entered current year %d\n", CY);
    printf("You are %d years old\n \n", Age);
    if (Age > 18) {
        puts("You are an adult\n");
        if (Age < 18)
            puts("You are a minor\n");
    }
    if(Age<=100){
        while (  counter <= 10);
    }
    switch (counter) {
    case 0:
        puts("You are less than 10\n");
        break;
    case 1:
        puts("you are in your tens / teens\n");
        break;
    case 2:
        puts("You are in your twenties\n");
        break;
    case 3:
        puts("You are in your thirties\n");
        break;
    case 4:
        puts("You are in your fourties\n");
        break;
    case 5:
        puts("You are in your fifties\n");
        break;
    case 6:
        puts("You are in your sixties\n");
        break;
    case 7:
        puts("You are in your seventies\n");
        break;
    case 8:
        puts("You are in your eighties\n");
        break;
    case 9:
        puts("You are in your nineties\n");
        break;
    case 10:
        puts("You are a 100 !!\n");
        break;
    default:
        puts("invalid age!");
        break;
    }
    return 0;
}

CodePudding user response:

Answer to your problem

You have a simple logic issue in your "counter-setter-loop" (I don't know how I should call this).

if(Age<=100){
     while (  counter <= 10);
}

you're incrementing counter to 11 not 10. Suppose counter is 10, it'll iterate again, since the condition <= 10 is still true. As a result you'd need to change this part to:

if(Age<=100){
     while (  counter < 10);
}

Code review

Anyway, when I saw your code, some code improvements went through my head. I hope that's fine for you, if not, you can skip this.

  1. adding \n to printf (little UI improvement)

In my opinion it looks a little bit better, if you create a little prompt and create a new line for the input:

    printf("Please enter your year of birth\n>> ");
    scanf_s("%d", &YoB);

    printf("Please enter the current year\n>> ");
    scanf_s("%d", &CY);
  1. Logic issue:

There's a logic issue in this part:

    if (Age > 18) {
        puts("You are an adult\n");
        if (Age < 18)
            puts("You are a minor\n");
    }

Assuming Age is < 18. I think that you'd expect to have the output You are a minor. But this will never happen because in order to get to the if (Age < 18) condition, you'd need to enter the Age > 18 condition. So I think you mean it like that:

// Also keep in mind that Age can be "< 0" if the user used a "bad" input.
if (Age < 0) {
    puts("You are not born yet.");
} else if (Age > 18) {
    puts("You are an adult\n");
} else {
    puts("You are a minor\n");
}
  1. Set counter a value instead of using a loop

You can change this part:

    if (Age <= 100){
        while (  counter <= 10);
    }

to this:

    if (Age <= 100) {
        counter = 10;
    }

although I don't really understand how you can enter the other case-arms since counter seems to be only 0 or 10 due to the condition above. I think you rather mean something like this:

if (Age <= 100) {
    counter = Age / 10;
}

Also please use variable-names which start with a lowercase and not with a uppercase since uppercase Names are rather used for structs, enums or constants etc.

To sum it up, I'd do it as follows:

#include <stdio.h>

int main()
{
    int birth_year, current_year, age;
    unsigned int tenths = 0;

    printf("Please enter your year of birth\n>> ");
    scanf_s("%d", &birth_year);

    printf("Please enter the current year\n>> ");
    scanf_s("%d", &current_year);

    age = current_year - birth_year;

    printf("Entered year of birth %d\n", birth_year);
    printf("Entered current year %d\n", current_year);
    printf("You are %d years old\n \n", age);

    if (age < 0) {
        puts("You aren't born yet.");
    } else if (age >= 18) {
        puts("You are an adult.\n");
    } else {
        puts("You are a minor.\n");
    }

    if(age <= 100){
        tenths = age % 10;
    }

    switch (tenths) {
        case 0:
            puts("You are less than 10\n");
            break;
        case 1:
            puts("you are in your tens / teens\n");
            break;
        case 2:
            puts("You are in your twenties\n");
            break;
        case 3:
            puts("You are in your thirties\n");
            break;
        case 4:
            puts("You are in your fourties\n");
            break;
        case 5:
            puts("You are in your fifties\n");
            break;
        case 6:
            puts("You are in your sixties\n");
            break;
        case 7:
            puts("You are in your seventies\n");
            break;
        case 8:
            puts("You are in your eighties\n");
            break;
        case 9:
            puts("You are in your nineties\n");
            break;
        case 10:
            puts("You are a 100 !!\n");
            break;
        default:
            puts("invalid age!");
            break;
    }
    return 0;
}
  • Related