So I am writing a simple while loop program that calculates a discount for a number of shirts a person enters into the program. For the most part it works, however I am getting a strange value if I enter in the same value multiple times:
Input value: 4 Price of shirts are $12.00 and total value is $48.00
Input value: 4 Price of shirts are $9.00 and total value is $36.00
Input value: 4 Price of shirts are $12.00 and total value is $48.00
Input value: 4 Price of shirts are $9.00 and total value is $36.00
#include<iostream>
#include<iomanip>
#include<math.h>
usingnamespace std;
int main()
{
int number;
double total_price, new_price;
constdouble shirt_price = 12.00;
cout << "Number of Shirts Discount.\n";
cout << "5 to 10 shirts: 10% Discount.\n";
cout << "11 to 20 shirts: 15% Discount.\n";
cout << "21 to 30 shirts: 20% Discount.\n";
cout << "31 or more shirts: 25% Discount.\n";
cout << "\nHow many shirts would you like? ";
cin >> number;
cout << showpoint << setprecision(4);
while (number >= 0) { //For all 0 or positive values
if (number <= 4) { //Price for 0 to 4 shirts
new_price = shirt_price;
total_price = new_price * number;
cout << "The cost per shirt is $12 and the total cost is $ " << total_price << endl;
cout << "\nHow many shirts would you like? ";
cin >> number; }
elseif (number = 5 && number <= 10) { //Price for 5-10 shirts
new_price = shirt_price - (shirt_price * .1);
total_price = new_price * number;
cout << "The cost per shirt is $ "<< new_price << " and the total cost is $ " << total_price << endl;
cout << "\nHow many shirts would you like? ";
cin >> number; }
elseif (number >= 11 && number <= 20) { //Price for 11-20 shirts
new_price = shirt_price - (shirt_price * .15);
total_price = new_price * number;
cout << "The cost per shirt is $ " << new_price << " and the total cost is $ " << total_price << endl;
cout << "\nHow many shirts would you like? ";
cin >> number; }
elseif (number >=21 && number <= 30) { //Price for 21-30 shirts
new_price = shirt_price - (shirt_price * .2);
total_price = new_price * number;
cout << "The cost per shirt is $ " << new_price << " and the total cost is $ " << total_price << endl;
cout << "\nHow many shirts would you like? ";
cin >> number; }
else (number >= 31); { //Price for 31+ shirts
new_price = shirt_price - (shirt_price * .25);
total_price = new_price * number;
cout << "The cost per shirt is $ " << new_price << " and the total cost is $ " << total_price << endl;
cout << "\nHow many shirts would you like? ";
cin >> number; }
}
cout << "\nInvalid number. Please run program again.\n"; //End program if negative value is entered.
system("pause");
return 0;
}
This is my first coding class and I am doing online classes. Any and all help and teaching is greatly appreciated!