-2

I'm in a intro to C programming class, and I'm working on a homework assignment. I just can't get to work properly. This is the homework assignment:

When someone buys a small number of widgets, they pay normal price. When someone buys a medium number of widgets, Nick is quite happy, and gives them a discount off of the normal price. When someone buys a large number of widgets, Nick falls in love with them, and gives them an even bigger discount off of the normal price! But his problem is this: sometimes these amounts are lower than his original costs, so he loses money on the sale! For simplicity, we will assume that each widget costs Nick $0.40 each (so he should be selling each of them for a higher cost than this to his customers): #define WIDGET_COST 0.40

To help Nick save his business, please write a program that will prompt him for the price he will sell each widget for, the percent he will discount medium purchases by, and the percent he will discount large purchases by (each as whole numbers of a percent), then will output amessage indicating which of these choices is causing him not to make a profit. The possible issues Nick could have are (output only the first in this list that is true):

  1. Nick, your base price is too low!
  2. Nick, you are discounting medium purchases too much!
  3. Nick, you are discounting large purchases too much!

If there are no issues with his prices, you should just output: All prices look good to me! After outputting the appropriate message from above, don't forget to wish Nick good luck with his new business: Good luck!

This is my code so far:

int main(){

    double base;
    printf("How much are you selling each widget for?\n");
    scanf("%lf", &base);

    int medium;
    printf("How much are you discounting medium orders?\n");
    scanf("%d", &medium);

    int large;
    printf("How much are you discounting large orders?\n");
    scanf("%d", &large);

    double medium_total = base - medium/100 * base;
    double large_total = base - large/100 * base;

    if (base < WIDGET_COST) {
        printf("Nick, your base prices are to low!\n");
    }else if (medium_total < WIDGET_COST){
        printf("Nick, you are discounting medium purchases too much!\n");
    }else if (large_total < WIDGET_COST){
        printf("Nick, you are discounting large purchases too much!\n");
    }else {
        printf("All prices look good to me!\n");
    }

    printf("Good luck!\n");

    return 0;
}

What am I doing wrong?

I believe it might be how I'm structuring the if statements: they won't output properly, either it will say base prices are too low or will say all prices are good, unless I discount over 100 on the purchases.

nbro
  • 12,226
  • 19
  • 85
  • 163
staringblind
  • 7
  • 1
  • 3

2 Answers2

0

Replace:

double medium_total = base - medium/100 * base;

with

double medium_total = base - medium/100.0 * base;

medium is an int which means that medium / 100 is an integer division. By changing 100 to 100.0 a floating point division will be performed.

Same for large_total declaration.

ouah
  • 134,166
  • 14
  • 247
  • 314
  • That was it!!! Thanks so much! I cant believe a simple decimal point was all I needed! Haha, I obviously have a LOT to learn. But thanks again! – staringblind Feb 06 '15 at 19:54
0

In your code, medium being an int, medium/100 is being performed as integer division, producing a 0 for medium < 100. That is why

all prices are good, unless I discount over 100 on the purchases

To avoid, please use a cast,like

double medium_total = base - ((double)medium)/100 * base;

or write 100 as 100.0 to enforce a floating point division.

Similar case in large/100 also.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234