Doubt about code reading in C

Asked

Viewed 36 times

0

I have a question about a code I made in C, the statement is as follows:

"Write a C algorithm that reads the Price of a commodity and displays the price on the reset screen of 3%. The user will choose the Option: (A)accretion or (D)scope for the 3% adjustment. Now Do the same exercise, however; reading the readjustment (in %) of the user."

The first part I managed to solve, but in the second part (when I have to define if the user chose the value of the percentage or the program would follow normally as stated) I’m having problems, because when I run, the program asks if I want to choose the value of the discount asking me to type 'YES' or 'NOT' if I don’t want to, only as soon as I type YES or NOT, it asks if I want to add or remove and then the program ends with Return 0, that is, he does not enter the if and nor in the Switchs, I would like to clarify this doubt, thank you!

Follow the code:

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

int main()
{
    float productPrice, reajustedPrice, percentageValue;
    char switchChoose, operationChoose;

    printf("if you want to add the percentage value type YES if not type NOT\n");
    scanf("%c", &operationChoose);

    if (operationChoose == 'YES')
    {
        printf("Enter percentage value\n");
        scanf("%f", &percentageValue);

        printf("A to Addition\nD to Descount\n");
        scanf("%c", &switchChoose);

        switch (switchChoose)
        {
        case 'A':
            printf("Entering case A...\n");
            printf("Enter product price:\n");
            scanf("%f", &productPrice);

            reajustedPrice = productPrice + (productPrice * (percentageValue / 100));
            printf("Total price: %.2f", reajustedPrice);
            break;

        case 'D':
            printf("Entering case D...\n");
            printf("Enter product price:\n");
            scanf("%f", &productPrice);

            reajustedPrice = productPrice - (productPrice * (percentageValue / 100));
            printf("Total price: %.2f", reajustedPrice);
            break;
        }
    }
    else
    {
        printf("A to Addition\nD to Descount\n");
        scanf("%c", &switchChoose);

        switch (switchChoose)
        {
        case 'A':
            printf("Entering case A...\n");
            printf("Enter product price:\n");
            scanf("%f", &productPrice);

            reajustedPrice = productPrice + (productPrice * 0.03);
            printf("Total price: %.2f", reajustedPrice);
            break;

        case 'D':
            printf("Entering case D...\n");
            printf("Enter product price:\n");
            scanf("%f", &productPrice);

            reajustedPrice = productPrice - (productPrice * 0.03);
            printf("Total price: %.2f", reajustedPrice);
            break;
        }
    }
    return 0;
}
  • Please avoid Pedro put code link in question.

  • Note that you ask to inform YES or NOT but read a single character (not 3). In C this: (operationChoose == 'YES') is invalid, the ' character delimits a single character and not a string. To compare strings you have to use the strcmp function of <string. h> and delimit the constant strings with " and not '.

  • could you give me an example of what the code would look like doing what you explained to me? use #include <string. h>, and then?? obg!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.