Hi, my name is Josh and this is my first time searching for help with my c++ class. I have to write a program that will ask for the product number of an item, the price of one item, and the number of items sold. anything bigger than 3999 is invalid. It should be a loop where if you type (-1) into the input prodict number the loop will stop and tell you the total product summary (the total count of a product, the total money made from those sales, and the number of invalid sales.) I am currently lost and do not know where to begin so any help is good.
My Questions:
1. Why am I getting this error?
2. What is the best type of loop to use for this problem?
3. How do I stop the loop once I get it running properly?
4. How do I add up the total value for a variable? As in how do I keep adding the total value for a variable while the loop is still running so at the end of the loop I can spit out the sum of that variable?
// Items for sale:
// Gizmos - Product number 0-999
// Widgets - Product number 1000-1999
// doohickeys - Product number 2000-2999
// thingamajigs - Product number 3000-3999
// Product number >3999 = Invalid Item
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
float ProdNumb; // Product Number
double PrG; // Product Number for Gizmo
double NG; // Number of items
double PG; // Price of Item
double PrW; // Product Number for Widgets
double NW; // Number of items
double PW; // Price of Item
double PrD; // Product Number for Doohickeys
double ND; // Number of items
double PD; // Price of Item
double PrT; // Product Number for Thingamajigs
double NT; // Number of items
double PT; // Price of Item
double PrI; //Product Number for Invalid (> 3999)
double NI; // Number of items
double PI; // Price of Item
int main ()
{
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
if (ProdNumb >= 0 && ProdNumb <= 999)
ProdNumb == PrG;
cout << "Enter the number of items sold: ";
cin >> NG;
cout << "Enter the price of one of the items sold: ";
cin >> PG;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
elseif (ProdNumb >= 1000 && ProdNumb <= 1999)
ProdNumb == PrW;
cout << "Enter the number of items sold: ";
cin >> NW;
cout << "Enter the price of one of the items sold: ";
cin >> PW;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
elseif (ProdNumb >= 2000 && ProdNumb <= 2999)
ProdNumb == PrD;
cout << "Enter the number of items sold: ";
cin >> ND;
cout << "Enter the price of one of the items sold: ";
cin >> PD;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
elseif (ProdNumb >= 3000 && ProdNumb <= 3999)
ProdNumb == PrT;
cout << "Enter the number of items sold: ";
cin >> NT;
cout << "Enter the price of one of the items sold: ";
cin >> PT;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
elseif (ProdNumb <= -2 && ProdNumb == 0 && ProdNumb >= 4000)
ProdNumb == PrI;
cout << "Enter the number of items sold: ";
cin >> NI;
cout << "Enter the price of one of the items sold: ";
cin >> PI;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
elseif (ProdNumb == -1)
cout << "***** Product Sales Summary *****";
cout << "\n";
cout << "\n";
cout << "Gizmo Count: ";
cout << NG++;
cout << "\n";
cout << "Gizmo Sales Total: ";
cout << (NG++)*(PG++);
cout << "\n";
cout << "\n";
cout << "Widget Count: ";
cout << NW++;
cout << "\n";
cout << "Widget Sales Total: ";
cout << (NW++)*(PW++);
cout << "\n";
cout << "\n";
cout << "Dookickey Count: ";
cout << ND++;
cout << "\n";
cout << "Doohickey Sales Total: ";
cout << (ND++)*(PD++);
cout << "\n";
cout << "\n";
cout << "Thingamajig Count: ";
cout << NT++;
cout << "\n";
cout << "Thingamajig Sales Total: ";
cout << (NT++)*(PT++);
cout << "\n";
cout << "\n";
cout << "Invalid Sales: ";
cout << NI++;
return 0;
}
Sample Output
Enter the product number of the item sold: 1250
Enter the number of items sold: 3
Enter the price for one of the items: 19.99
Enter the product number of the item sold: 2820
Enter the number of items sold: 5
Enter the price for one of the items: 12.50
Enter the product number of the item sold: 3124
Enter the number of items sold: 7
Enter the price for one of the items: 20.30
Enter the product number of the item sold: 987
Enter the number of items sold: 2
Enter the price for one of the items: 10.11
Enter the product number of the item sold: 123
Enter the number of items sold: 36
Enter the price for one of the items: 1.50
Enter the product number of the item sold: 4567
Enter the number of items sold: 9
Enter the price for one of the items: 4.56
Enter the product number of the item sold: 1433
Enter the number of items sold: 20
Enter the price for one of the items: 4.50
Enter the product number of the item sold: 3748
Enter the number of items sold: 1
Enter the price for one of the items: 1.99
Enter the product number of the item sold: 3967
Enter the number of items sold: 5
Enter the price for one of the items: 7.10
Enter the product number of the item sold: 1130
Enter the number of items sold: 30
Enter the price for one of the items: 19.77
Enter the product number of the item sold: 303
Enter the number of items sold: 7
Enter the price for one of the items: 2.00
Enter the product number of the item sold: 530
Enter the number of items sold: 65
Enter the price for one of the items: 0.80
Enter the product number of the item sold: 916
Enter the number of items sold: 130
Enter the price for one of the items: 1.23
Enter the product number of the item sold: 1111
Enter the number of items sold: 75
Enter the price for one of the items: 1.72
Enter the product number of the item sold: 2222
Enter the number of items sold: 7
Enter the price for one of the items: 9.46
Enter the product number of the item sold: 3333
Enter the number of items sold: 4
Enter the price for one of the items: 24.00
Enter the product number of the item sold: 999
Enter the number of items sold: 31
Enter the price for one of the items: 0.90
Enter the product number of the item sold: 7531
Enter the number of items sold: 17
Enter the price for one of the items: 4.10
Enter the product number of the item sold: -1
***** Product Sales Summary *****
Gizmo Count: 271
Gizmo Sales Total: 328.02
Widget Count: 128
Widget Sales Total: 872.07
Doohickey Count: 12
Doohickey Sales Total: 128.72
Thingamajig Count: 17
Thingamajig Sales Total: 275.59
Invalid Sales: 2
You can do this, providing you only have one line of code to execute:
1
2
3
4
if (condition)
// do stuff -
else
// do else stuff
Or this if you have many lines:
1
2
3
4
5
6
7
8
9
10
if (condition)
{
// do stuff
// do more stuff
}
else
{
// do else stuff
// do more else stuff
}
But you can't have this:
1
2
3
4
5
6
if (condition)
// do stuff
// do more stuff here
// and even more stuff
else
// do else stuff
Or, for that matter, this:
1
2
3
4
5
6
7
8
9
10
if (condition)
{
// do stuff
}
// do more stuff here
// and even more stuff
else
{
// do else stuff
}
As for your loop, I'd suggest something along the lines of:
1
2
3
4
while (input != -1)
{
// your code here
}
The loop will cease whenever the input is no longer equal to -1.
To add up a total, create a total variable, then add to it whenever you need to:
1
2
3
4
5
double total = 0;
. . .
total += price; // Add the price to the total
Hope this helps.
THANK YOU FOR THE QUICK RESPONSE!!!!!! as far as the brackets after the if statements, I read in an earlier post that I didn't need them so I took them out; still had the same error. As for the rest.... THANK YOU!!!!!!!!!!