Error: expected primary-expression before 'else'

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?

Here is where I am so far....

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
140
141
142
143
144
145
146
147
148
149

// 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>

using namespace 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;

else if (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;

else if (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;

else if (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;

else if (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;

else if (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


THANKS IN ADVANCE!!!!!!!!
Last edited on
Hi Josh,

The error is because of your if/else syntax.

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 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.
Last edited on
iHutch105 (125) Feb 17, 2012 at 8:51am
Hi Josh,

The error is because of your if/else syntax.

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!!!!!!!!!!
Last edited on
Please mark the problem as solved
Topic archived. No new replies allowed.