If Conditional Issue
May 29, 2012 at 5:38am UTC
I'm having an issue where I try to execute arguments when a specific condition is met, and ONLY a specific condition. The program is a shopping list assistant. The problem is it applies the inputted quantity to every item. For instance, if I were to order 2 pants, then I would get a receipt that says $375 because it added 2 of EVERYTHING, not 2 pants. I don't want that. I want the inputted quantity to be applied to the number of the item inputted only.
It's the if conditionals, I believe. I removed
if (item == 2)
and so on leaving only
if (item ==1)
left, which gave me a PROPER output. If the if conditionals are the problem, how do I fix that? If not, how I fix whatever the problem is?
Here is my code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#include <iostream>
using namespace std;
int choice;
int process = 1;
float total = 0;
//Giving variables to the properties of the items
//Stock
int pants_quant = 11;
int tshirt_quant = 4;
int jacket_quant = 5;
int sweater_quant = 22;
int jeans_quant = 9;
int boots_quant = 28;
//Stock in the shopping cart
int shop_pants_quant = 0;
int shop_tshirt_quant = 0;
int shop_jacket_quant = 0;
int shop_sweater_quant = 0;
int shop_jeans_quant = 0;
int shop_boots_quant = 0;
//Prices
float pants_price = 49.99;
float tshirt_price = 8.83;
float jacket_price = 29.99;
float sweater_price = 22.5;
float jeans_price = 37.0;
float boots_price = 39.19;
//Prices in Shopping Cart
float shop_jeans_price = 0;
float shop_boots_price = 0;
float shop_sweater_price = 0;
float shop_jacket_price = 0;
float shop_tshirt_price = 0;
float shop_pants_price = 0;
int main()
{
while (process == 1)
{
cout<<"\n\n1. Print available items" ;
cout<<"\n2. Add item to cart" ;
cout<<"\n3. Print receipt and quit" ;
cout<<"\nPlease enter your choice: " ;
cin>>choice;
if (choice != 1 && choice != 2 && choice != 3)
{
cout<<"\nERROR! Insufficient quantity available. Please try again." ;
}
if (choice == 1)
{
cout<<"\n\nDescription Quantity Price" ;
cout<<"\n 1) Pants " <<pants_quant << " left $" << pants_price;
cout<<"\n 2) T-Shirt " <<tshirt_quant << " left $" << tshirt_price << " ON SALE! reg. $10.15" ;
cout<<"\n 3) Jacket " <<jacket_quant << " left $" << jacket_price;
cout<<"\n 4) Sweater " <<sweater_quant<< " left $" << sweater_price;
cout<<"\n 5) Jeans " <<jeans_quant << " left $" << jeans_price;
cout<<"\n 6) Boots " <<boots_quant << " left $" << boots_price << " ON SALE! reg. $41.25" ;
}
if (choice == 2)
{
int item;
int quant;
cout<<"\n\nPlease enter the item to add: " ;
cin>>item;
cout<<"\n\nPlease enter the quantity to add: " ;
cin>>quant;
if (item == 1)
pants_quant -= quant;
shop_pants_quant += quant;
shop_pants_price = shop_pants_quant * pants_price;
if (item == 2)
tshirt_quant -= quant;
shop_tshirt_quant += quant;
shop_tshirt_price = shop_tshirt_quant * tshirt_price;
if (item == 3)
jacket_quant -= quant;
shop_jacket_quant += quant;
shop_jacket_price = shop_jacket_quant * jacket_price;
if (item == 4)
sweater_quant -= quant;
shop_sweater_quant += quant;
shop_sweater_price = shop_sweater_quant * sweater_price;
if (item == 5)
jeans_quant -= quant;
shop_jeans_quant += quant;
shop_jeans_price = shop_jeans_quant * jeans_price;
if (item == 6)
boots_quant -= quant;
shop_boots_quant += quant;
shop_boots_price = shop_boots_quant * boots_price;
}
if (choice == 3)
{
float total = shop_jeans_price + shop_boots_price + shop_sweater_price + shop_jacket_price + shop_tshirt_price + shop_pants_price;
cout<<"\n\nRECEPT:\n" ;
cout<<shop_pants_quant << " x Pants " << pants_price << " ea.\n" ;
cout<<shop_tshirt_quant << " x T-Shirt " << tshirt_price << " ea.\n" ;
cout<<shop_jacket_quant << " x Jacket " << jacket_price << " ea.\n" ;
cout<<shop_sweater_quant << " x Sweater " << sweater_price << " ea.\n" ;
cout<<shop_jeans_quant << " x Jeans " << jeans_price << " ea.\n" ;
cout<<shop_boots_quant << " x Boots " << boots_price << " ea.\n" ;
cout<<"\n---------------------------------------------------------------------------" ;
cout<<"\n$" << total;
cin>>total;
process = 0;
}
}
return 0;
}
May 29, 2012 at 5:54am UTC
You are missing braces.
Your code with [correct] indentation, to hopefully show you your mistake:
1 2 3 4 5 6 7 8 9 10 11 12 13
if (item == 1)
pants_quant -= quant;
shop_pants_quant += quant;
shop_pants_price = shop_pants_quant * pants_price;
if (item == 2)
tshirt_quant -= quant;
shop_tshirt_quant += quant;
shop_tshirt_price = shop_tshirt_quant * tshirt_price;
if (item == 3)
jacket_quant -= quant;
shop_jacket_quant += quant;
shop_jacket_price = shop_jacket_quant * jacket_price;
// ... etc...
Jun 13, 2012 at 2:13am UTC
It was fixed a while ago and I didn't have time to thank you until now.
Not sure why I forgot those, haha. Probably way too used to Python. Thank you a lot!
Topic archived. No new replies allowed.