Write your question here.
Hi guys! I'm kind of a newbie on C++ and I'm stuck on this problem. It's not that it's not compiling but for some reason it's not doing what I imagined it to do. On Cases 2-5 it's not computing "totalCost = totalPrice - totalDct" it's not calculating the discounts if you buy this certain amount of units. When I run it it only shows the full price without discounts. Could you guys somehow help me?
#include <iostream>
#include <iomanip>
usingnamespace std;
int main(){
int units, CASE;
cout << "\n1 Unit of software costs $99\n\n";
cout << "There is a 20 percent discount if you buy 10-19 units, 30 percent for 20-49 units,\n";
cout << "40 percent for 50-99 units, and a 50 percent discount for 100 and above units" << endl;
cout << "How many units would you like to buy? ";
cin >> units;
cout << endl;
while(units < 1){
cout << "INVALID AMOUNT" << "\nPlease enter how many you would like to buy ";
cin >> units;
cout << endl;
}
if(units >= 1)
CASE = 1;
elseif(units >= 10)
CASE = 2;
elseif(units >= 20)
CASE = 3;
elseif(units >= 50)
CASE = 4;
elseif(units >= 100)
CASE = 5;
constdouble price = 99;
double totalPrice, totalCost, totalDct;
switch(CASE){
case 1:
totalPrice = units * price;
totalCost = totalPrice;
cout << "The total cost you would be paying is " << totalCost << endl;
break;
case 2:
totalPrice = units * price;
totalDct = totalPrice * 0.2;
totalCost = totalPrice - totalDct;
cout << "The total cost you would be paying is " << totalCost << endl;
break;
case 3:
totalPrice = units * price;
totalDct = totalPrice * 0.3;
totalCost = totalPrice - totalDct;
cout << "The total cost you would be paying is " << totalCost << endl;
break;
case 4:
totalPrice = units * price;
totalDct = totalPrice * 0.4;
totalCost = totalPrice - totalDct;
cout << "The total cost you would be paying is " << totalCost << endl;
break;
case 5:
totalPrice = units * price;
totalDct = totalPrice * 0.5;
totalCost = totalPrice - totalDct;
cout << "The total cost you would be paying is " << totalCost << endl;
break;
}
return 0;
}
! I did not know that, my program works perfectly now.
Also is there a way to assign a specific number range to a case statement? e.gcase >= 10
would this work? or is what I did the best possible way to assign a range of numbers to a case statement?