Need help with this program, every time I enter number of shirts to purchase, my
ID: 3848127 • Letter: N
Question
Need help with this program, every time I enter number of shirts to purchase, my output is 0 shirts and $69.
each shirt costs $12
5-10 shirts = 10% off
11-20 shirts = 15% off
21-30 shirts = 20%
31 and up shrts = 25%
#include <iostream>
using namespace std;
int main()
{
double shirts, total_shirt_cost, cost;
cout<<"Shirt Discount Program"<<endl;
cout<<"Please enter the number of shirts you would like to buy? ";
cin>>shirts;
total_shirt_cost = shirts * 12;
cout<<endl;
if ((shirts = 0) )
cost = 0;
else if (shirts > 0 && shirts <= 4)
cost = total_shirt_cost;
else if (shirts >= 5 && shirts <= 10)
cost = total_shirt_cost - (total_shirt_cost*0.1);
else if (shirts >= 11 && shirts <= 20)
cost = total_shirt_cost - (total_shirt_cost*0.15);
else if (shirts >= 21 && shirts <= 30)
cost = total_shirt_cost - (total_shirt_cost*0.2);
else if (shirts >= 31 )
cost = total_shirt_cost - (total_shirt_cost*0.25);
else
cost = 'E';
if (shirts < 0)
cout<<"error, please enter a non-negative integer"<<endl;
cout<<"When you buy "<<shirts<<" shirts, the total cost is: $"<<cost<<endl;
cout<<endl;
return 0;
}
Explanation / Answer
Correct c++ code:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int number_of_shirts = 0;
cout << "Shirt Discount Program!" << endl;
cout << "Enter number of shirts ";
cin >> number_of_shirts;
double cost = 12*number_of_shirts;
double final_cost;
if(number_of_shirts < 0)
{
cout << "Please Enter positive number of shirts! ";
}
else if(number_of_shirts >=1 and number_of_shirts <= 4)
{
final_cost = cost;
cout << "If you buy " << number_of_shirts << " shirts, it will cost you $" << final_cost << endl;
}
else if(number_of_shirts >=5 and number_of_shirts <= 10)
{
final_cost = cost - 0.1*cost;
cout << "If you buy " << number_of_shirts << " shirts, it will cost you $" << final_cost << endl;
}
else if(number_of_shirts >=11 and number_of_shirts <= 20)
{
final_cost = cost - 0.15*cost;
cout << "If you buy " << number_of_shirts << " shirts, it will cost you $" << final_cost << endl;
}
else if(number_of_shirts >=21 and number_of_shirts <= 30)
{
final_cost = cost - 0.20*cost;
cout << "If you buy " << number_of_shirts << " shirts, it will cost you $" << final_cost << endl;
}
else if(number_of_shirts >=31 )
{
final_cost = cost - 0.25*cost;
cout << "If you buy " << number_of_shirts << " shirts, it will cost you $" << final_cost << endl;
}
return 0;
}
Sample Output:
C:UsersAkashDesktopchegg>a
Shirt Discount Program!
Enter number of shirts
4
If you buy 4 shirts, it will cost you $48
C:UsersAkashDesktopchegg>a
Shirt Discount Program!
Enter number of shirts
5
If you buy 5 shirts, it will cost you $54
C:UsersAkashDesktopchegg>a
Shirt Discount Program!
Enter number of shirts
10
If you buy 10 shirts, it will cost you $108
C:UsersAkashDesktopchegg>a
Shirt Discount Program!
Enter number of shirts
20
If you buy 20 shirts, it will cost you $204
C:UsersAkashDesktopchegg>a
Shirt Discount Program!
Enter number of shirts
25
If you buy 25 shirts, it will cost you $240
C:UsersAkashDesktopchegg>a
Shirt Discount Program!
Enter number of shirts
40
If you buy 40 shirts, it will cost you $360