code wont work help please
May 8, 2014 at 2:49pm UTC
whats going wrong im a little rusty with my C++ I know i have a little extra #includes but i enter what i think i may need before i start but my programs not working
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
/*Paul Maywald C++5 project.cpp
*/
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int productNum=0;
int productNum1=0;
int productQty=0;
double total=0.00;
double productPrice=0.00;
cout << "Welcome to the store..." << endl;
cout << "Enter 1 for fruits..." << endl;
cout << "Enter 2 for vegetables..." << endl;
cout << "Enter 3 for meats..." << endl;
cout << "Please enter the product number for which product you would like to buy..." << endl;
cin >> productNum;
while (productNum != 4)
{
if (productNum == 1)
{
cout << "Enter 1 for Tomato..." << productNum1 << endl;
cout << "Enter 2 for Orange..." << productNum1 << endl;
cout << "Enter 3 for Banana..." << productNum1 << endl;
if (productNum1 == 1)
{
cout << "$0.50 per Tomato..." << endl;
cout << "How many Tomato's would you like to purchase..." << productQty << endl;
productPrice = productQty * .5;
}
else if (productNum1 == 2)
{
cout << "$0.70 per Orange..." << endl;
cout << "How many Orange's would you like to purchase..." << productQty << endl;
productPrice = productQty * .7;
}
else if (productNum1 == 3)
{
cout << "$0.80 per Banana..." << endl;
cout << "How many Banana's would you like to purchase..." << productQty << endl;
productPrice = productQty * .8;
}
total = productPrice;
cout << "Your total cost is " << total << "$... come again..." << endl;
}
if (productNum == 2)
{
cout << "Enter 1 for Pickle..." << productNum1 << endl;
cout << "Enter 2 for Lettuce..." << productNum1 << endl;
cout << "Enter 3 for Corn..." << productNum1 << endl;
if (productNum1 == 1)
{
cout << "$0.90 per Pickle..." << endl;
cout << "How many Pickle's would you like to purchase..." << productQty << endl;
productPrice = productQty * .9;
}
else if (productNum1 == 2)
{
cout << "$0.85 per Lettuce head..." << endl;
cout << "How many Lettuce Heads would you like to purchase..." << productQty << endl;
productPrice = productQty * .85;
}
else if (productNum1 == 3)
{
cout << "$0.70 per Corn Cob..." << endl;
cout << "How many Corn Cobs would you like to purchase..." << productQty << endl;
productPrice = productQty * .7;
}
total = productPrice;
cout << "Your total cost is " << total<< "$... come again..." << endl;
}
if (productNum == 3)
{
cout << "Enter 1 for Chicken..." << productNum1 << endl;
cout << "Enter 2 for Pork..." << productNum1 << endl;
cout << "Enter 3 for Steak..." << productNum1 << endl;
if (productNum1 == 1)
{
cout << "$5.00 per lb of Chicken..." << endl;
cout << "How many pounds of Chicken would you like to purchase..." << productQty << endl;
productPrice = productQty * 5;
}
else if (productNum1 == 2)
{
cout << "$7.00 per lb of Pork..." << endl;
cout << "How many pounds of Pork would you like to purchase..." << productQty << endl;
productPrice = productQty * 7;
}
else if (productNum1 = 3)
{
cout << "$10.00 per lb of Steak..." << endl;
cout << "How many pounds of Steak would you like to purchase..." << productQty << endl;
productPrice = productQty * 10;
}
total = productPrice;
cout << "Your total cost is " << total << "$... come again..." << endl;
}
}
system("pause" );
return 0;
}
May 8, 2014 at 3:04pm UTC
1) At the end of your
while (productNum != 4)
, you don't ask user for another input, so it's stuck in an infinite loop (unless you type 4 in the beginning)
2)
1 2 3 4 5
cout << "Enter 1 for Tomato..." << productNum1 << endl;
cout << "Enter 2 for Orange..." << productNum1 << endl;
cout << "Enter 3 for Banana..." << productNum1 << endl;
if (productNum1 == 1)
...
as you can see above, you ask user for input, but no input statement appears
1 2 3
cout << "$5.00 per lb of Chicken..." << endl;
cout << "How many pounds of Chicken would you like to purchase..." << productQty << endl;
productPrice = productQty * 5;
you missed input statement again
Last edited on May 8, 2014 at 3:08pm UTC
May 8, 2014 at 3:18pm UTC
so than how should i change it?
May 8, 2014 at 3:21pm UTC
i deleted the while now if i enter 1 or 2 or 3 it doesnt let me enter anything else here try
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
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int productNum=0;
int productNum1=0;
int productQty=0;
double total=0.00;
double productPrice=0.00;
cout << "Welcome to the store..." << endl;
cout << "Enter 1 for fruits..." << endl;
cout << "Enter 2 for vegetables..." << endl;
cout << "Enter 3 for meats..." << endl;
cout << "Please enter the product number for which product you would like to buy..." << endl;
cin >> productNum;
if (productNum == 1)
{
cout << "Enter 1 for Tomato..." << productNum1 << endl;
cout << "Enter 2 for Orange..." << productNum1 << endl;
cout << "Enter 3 for Banana..." << productNum1 << endl;
if (productNum1 == 1)
{
cout << "$0.50 per Tomato..." << endl;
cout << "How many Tomato's would you like to purchase..." << productQty << endl;
productPrice = productQty * .5;
}
else if (productNum1 == 2)
{
cout << "$0.70 per Orange..." << endl;
cout << "How many Orange's would you like to purchase..." << productQty << endl;
productPrice = productQty * .7;
}
else if (productNum1 == 3)
{
cout << "$0.80 per Banana..." << endl;
cout << "How many Banana's would you like to purchase..." << productQty << endl;
productPrice = productQty * .8;
}
total = productPrice;
cout << "Your total cost is " << total << "$... come again..." << endl;
}
if (productNum == 2)
{
cout << "Enter 1 for Pickle..." << productNum1 << endl;
cout << "Enter 2 for Lettuce..." << productNum1 << endl;
cout << "Enter 3 for Corn..." << productNum1 << endl;
if (productNum1 == 1)
{
cout << "$0.90 per Pickle..." << endl;
cout << "How many Pickle's would you like to purchase..." << productQty << endl;
productPrice = productQty * .9;
}
else if (productNum1 == 2)
{
cout << "$0.85 per Lettuce head..." << endl;
cout << "How many Lettuce Heads would you like to purchase..." << productQty << endl;
productPrice = productQty * .85;
}
else if (productNum1 == 3)
{
cout << "$0.70 per Corn Cob..." << endl;
cout << "How many Corn Cobs would you like to purchase..." << productQty << endl;
productPrice = productQty * .7;
}
total = productPrice;
cout << "Your total cost is " << total<< "$... come again..." << endl;
}
if (productNum == 3)
{
cout << "Enter 1 for Chicken..." << productNum1 << endl;
cout << "Enter 2 for Pork..." << productNum1 << endl;
cout << "Enter 3 for Steak..." << productNum1 << endl;
if (productNum1 == 1)
{
cout << "$5.00 per lb of Chicken..." << endl;
cout << "How many pounds of Chicken would you like to purchase..." << productQty << endl;
productPrice = productQty * 5;
}
else if (productNum1 == 2)
{
cout << "$7.00 per lb of Pork..." << endl;
cout << "How many pounds of Pork would you like to purchase..." << productQty << endl;
productPrice = productQty * 7;
}
else if (productNum1 = 3)
{
cout << "$10.00 per lb of Steak..." << endl;
cout << "How many pounds of Steak would you like to purchase..." << productQty << endl;
productPrice = productQty * 10;
}
total = productPrice;
cout << "Your total cost is " << total << "$... come again..." << endl;
}
system("pause" );
return 0;
}
May 8, 2014 at 3:47pm UTC
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int productNum=0;
int productNum1=0;
int productQty=0;
double total=0.00;
double productPrice=0.00;
cout << "Welcome to the store..." << endl;
cout << "Enter 1 for fruits..." << endl;
cout << "Enter 2 for vegetables..." << endl;
cout << "Enter 3 for meats..." << endl;
cout << "Please enter the product number for which product you would like to buy..." << endl;
cin >> productNum;
while (productNum != 4)
{
if (productNum == 1)
{
cout << "Enter 1 for Tomato..." << productNum1 << endl;
cout << "Enter 2 for Orange..." << productNum1 << endl;
cout << "Enter 3 for Banana..." << productNum1 << endl;
cin >> productNum1;
if (productNum1 == 1)
{
cout << "$0.50 per Tomato..." << endl;
cout << "How many Tomato's would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * .5;
}
else if (productNum1 == 2)
{
cout << "$0.70 per Orange..." << endl;
cout << "How many Orange's would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * .7;
}
else if (productNum1 == 3)
{
cout << "$0.80 per Banana..." << endl;
cout << "How many Banana's would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * .8;
}
total = productPrice;
cout << "Your total cost is " << total << "$... come again..." << endl;
}
else if (productNum == 2)
{
cout << "Enter 1 for Pickle..." << productNum1 << endl;
cout << "Enter 2 for Lettuce..." << productNum1 << endl;
cout << "Enter 3 for Corn..." << productNum1 << endl;
cin >> productNum1;
if (productNum1 == 1)
{
cout << "$0.90 per Pickle..." << endl;
cout << "How many Pickle's would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * .9;
}
else if (productNum1 == 2)
{
cout << "$0.85 per Lettuce head..." << endl;
cout << "How many Lettuce Heads would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * .85;
}
else if (productNum1 == 3)
{
cout << "$0.70 per Corn Cob..." << endl;
cout << "How many Corn Cobs would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * .7;
}
total = productPrice;
cout << "Your total cost is " << total<< "$... come again..." << endl;
}
else if (productNum == 3)
{
cout << "Enter 1 for Chicken..." << productNum1 << endl;
cout << "Enter 2 for Pork..." << productNum1 << endl;
cout << "Enter 3 for Steak..." << productNum1 << endl;
cin >> productNum1;
if (productNum1 == 1)
{
cout << "$5.00 per lb of Chicken..." << endl;
cout << "How many pounds of Chicken would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * 5;
}
else if (productNum1 == 2)
{
cout << "$7.00 per lb of Pork..." << endl;
cout << "How many pounds of Pork would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * 7;
}
else if (productNum1 == 3)
{
cout << "$10.00 per lb of Steak..." << endl;
cout << "How many pounds of Steak would you like to purchase..." << endl;
cin >> productQty;
productPrice = productQty * 10;
}
total = productPrice;
cout << "Your total cost is " << total << "$... come again..." << endl;
}
cout << "Enter 1 for fruits..." << endl;
cout << "Enter 2 for vegetables..." << endl;
cout << "Enter 3 for meats..." << endl;
cout << "Please enter the product number for which product you would like to buy..." << endl;
cin >> productNum;
}
system("pause" );
return 0;
}
check those changes
Last edited on May 8, 2014 at 3:53pm UTC
May 8, 2014 at 3:55pm UTC
i think it's better to use switch
instead of using if
. just IMO
Topic archived. No new replies allowed.