kauajuno
kauajuno

Reputation: 45

What's wrong with that if-else structure?

So, I'm a beginner programmer and I'm trying to do an exercise where i have to create a program that catches the price and the source code of a product, and through the source code the program will show from where the product came from. So here's the list of the source codes:

If the source code inserted by the user don't match with any of this list, there will be a message saying that the code inserted has no correspondent in the list. So, I did the exercise and there's the final code:

int main() {
    float price;
    int code;
    char check1, check2;

    printf("Hello there! Insert the price of your product: ");
    scanf("%f", &price);
    printf("\nAlright, now enter the source code of the product: ");
    scanf("%d", &code);

    switch(code) {
    case 1:
        printf("Your product came from the South");
        break;

    case 2:
        printf("Your product came from the North");
        break;

    case 3:
        printf("Your product came from the East");
        break;

    case 4:
        printf("Your product came from the West");
        break;

    default:
        if(code == 5 || code == 6 || (code >= 25 && code <= 30)) {
            printf("Your product came from the Northeast");
        } else {
            if(code >= 7 && code <= 9) {
                printf("Your product came from the Southeast");
            } else {
                if(code >= 10 && code <= 20) {
                    printf("Your product came from the Midwest");
                } else {
                    printf("Sorry, this code doesn't match with any correspondence");
                    return 0;
                }
            }
        }
        break;
    }

    printf(" and it costs %0.2f\n", price);
}

But I also wanted to do an if-else structure before the program checks if there is any correspondence to give the user the opportunity of correcting something that he inserted wrong or something like that. So I did this:

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

int main() {
    float price;
    int code;
    char check1, check2;

    printf("Hello there! Insert the price of your product: ");
    scanf("%f", &price);
    printf("\nAlright, now enter the source code of the product: ");
    scanf("%d", &code);

    printf(
        "So, the price is %0.2f and the code is %d, right? Press c to continue, r "
        "to reset and any other key to quit: \n",
        price, code);
    scanf("%s", &check1);

    if(check1 == 'c') {
        printf("Let's continue then!\n");
    } else {
        if(check1 == 'r') {
            printf("\n");
            main();
        } else {
            return 0;
        }
    }

    switch(code) {
    case 1:
        printf("Your product came from the South");
        break;

    case 2:
        printf("Your product came from the North");
        break;

    case 3:
        printf("Your product came from the East");
        break;

    case 4:
        printf("Your product came from the West");
        break;

    default:
        if(code == 5 || code == 6 || (code >= 25 && code <= 30)) {
            printf("Your product came from the Northeast");
        } else {
            if(code >= 7 && code <= 9) {
                printf("Your product came from the Southeast");
            } else {
                if(code >= 10 && code <= 20) {
                    printf("Your product came from the Midwest");
                } else {
                    printf("Sorry, this code doesn't match with any correspondence");
                    return 0;
                }
            }
        }
        break;
    }

    printf(" and it costs %0.2f\n", price);
}

The problem is: with this code, the output is always "Sorry, this code doesn't match with any correspondence", no matter what are the input values. Why does it happen? How can I fix it?

(Also, sorry 4 the bad English, I'm still learning)

Upvotes: 3

Views: 99

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

In

scanf("%s", &check1);

you can not use the string format specifier ("%s") to scan a char, switch to

scanf("%c", &check1);

or better yet

scanf(" %c", &check1); // The leading whitespace will consume the previous newline

Upvotes: 3

Related Questions