-2

I've been writing a program where you give a buyer company ID and product ID to a program and it'll give you the price based on company exclusive discounts and a few other discounts.

However, every time I test it (with the same inputs), I get a completely different answer, usually hundreds of times larger than my intended answer. I'm still new to C and programming so a lot of the methods I've been using in this program I've never done before.

#include <stdio.h>

char compID[30];
double discount;
char prodID[30];
int prodAmount = 0;
double productPrice;
double tax;
double totalprice = 0;



int main() {

    char cont[2];

    printf("What is your company ID? ");
    scanf("%s", compID);

    if (strcmp(compID, "BFSC") == 0) {

        discount = 1;
        tax = 1.1;

    }

    do {

        int itemPrice;

        printf("What is the idea of the product you'd like to purchase? ");
        scanf("%s", prodID);

        if (strcmp(prodID, "FENG") == 0) {

            productPrice = 12124.50;

        }

        printf("How many? ");
        scanf("%d", &prodAmount);


        printf("Do you want to purchase another item? (y/n) ");
        scanf("%s", &cont);
    } while (cont == 'y' || cont == 'Y');

    totalprice = prodAmount*productPrice*tax*discount;
    printf("%d", &totalprice);


}

If i put BFSC as the company ID and FENG as the product ID, the answers i'm getting are 1 million plus, rather than 13336.95.

  • 7
    1) `printf("%d", &totalprice);` --> `printf("%f\n", totalprice);` – BLUEPIXY Mar 29 '17 at 11:14
  • after every `scanf` use `printf` to print the value of variable which have been read via `scanf`. Now check if the values are as expected or match what you provided. – sameerkn Mar 29 '17 at 11:15
  • 1
    2) `scanf("%s", &cont); } while (cont == 'y' || cont == 'Y');` --> `scanf("%s", cont); } while (*cont == 'y' || *cont == 'Y');` – BLUEPIXY Mar 29 '17 at 11:15
  • `while (cont[0] == 'y' || cont[0] == 'Y');` instead of `while (cont == 'y' || cont == 'Y');` – sameerkn Mar 29 '17 at 11:18
  • 3) Put `#include ` – BLUEPIXY Mar 29 '17 at 11:19
  • These are all utterly common beginner mistakes that everyone learning C does. They are all addressed in chapter 1 of your beginner-level C programming book. Please read chapter 1. Then enable compiler warnings. – Lundin Mar 29 '17 at 12:54
  • [How to read / parse input in C? The FAQ](http://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq). – Lundin Mar 29 '17 at 12:55

1 Answers1

2

I have reviewed your code its just format specifier mistakes and there are some typecast problems you are calculating totalprice and but printing its address printf("%d", &totalprice);

and where totalprice = prodAmount(int type) * productPrice(double) * tax(double) * discount(double)

here if you will not do any type cast then it would always give some garbage data as output or integer value. please try below code this might help you

#include <stdio.h>
char compID[30];
double discount;
char prodID[30];
float prodAmount = 0;
float productPrice;
float tax;
float totalprice;
int main() {

    char cont = 0;

    printf("What is your company ID? ");
    scanf("%s",compID);

    if (strcmp(compID, "BFSC") == 0) {

            discount = 1;
            tax = 1.1;

    }

    do {

            int itemPrice;

            printf("What is the idea of the product you'd like to purchase? ");
            scanf("%s", prodID);

            if (strcmp(prodID, "FENG") == 0) {

                    productPrice = 12124.50;

            }

            printf("How many? ");
            scanf("%f",&prodAmount);


            printf("Do you want to purchase another item? (y/n) ");
            scanf(" %c", &cont);
    } while (cont == 'y' || cont == 'Y');

    totalprice = ((prodAmount * productPrice) * tax )* discount;

    printf("%0.02f\n", totalprice);


}
Mukul
  • 114
  • 1
  • 11
  • Did you run this code? I suspect that this will give problems: `scanf("%c ", &cont);`. There is a newline left in the input stream, and in any event, trailing whitespace in a `scanf()` format specifier is almost always wrong. – ad absurdum Mar 29 '17 at 13:44
  • So my main issues was the fact I was using doubles rather than floats and that my formula for totalprice was wrong? – Lachlan Langmead Mar 29 '17 at 16:10
  • @Lachlan Langmead your logic was right but you have mix the integer double operation try to avoid different data types operations but if you want to do such operations then you need to do typecast . – Mukul Mar 30 '17 at 05:09
  • @David Bowling yes i have run this code and i am able to get correct results and here space in scanf("%c ", &cont); having a reason ,suppose you wont put space in it it will consider direct output because "Enter" is also a character so when we press enter it will consider as input and produce the results . try it once and let me know if you face any problem :) – Mukul Mar 30 '17 at 05:12
  • Well, yes I have already tried it and it does not quite work. The previous call to `scanf()` to get `prodAmount` leaves a newline behind in the input stream. This newline is picked up by the `%c` in the next `scanf()`, so no matter what you select the loop is exited. If you fix this problem, then the problem with the trailing space in the format string `"%c "` will become apparent. – ad absurdum Mar 30 '17 at 05:19
  • 1
    Hi Devid i found my problem in the code i was leaving the '\n' on stdin: form last scanf and yes if i put space before %c (corrected in my code above) then it solves the problem thnaks :) – Mukul Mar 30 '17 at 05:53