if else with else if

So, I know some of the rest of the code is probably wrong, but my current issue is that whenever i compile it returns:

"expected primary-expression before 'else'"
"expected `;' before 'else'"
for all loops containing else if expressions. Is there something I'm missing within the expressions or is some other part of my code messing them up?

Thanks for any help!


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

/*Problem 2--Sutures*/
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
    double BatchNumber, Temperature, Pressure, DwellTime;
    ifstream infile;
    infile.open ("Sutures.txt");
    infile >> BatchNumber >> Temperature >> Pressure >> DwellTime;
    double T, P, D, Total, Rejected, Name;
    cout << "Enter team name(s): " << endl;
    cin >> Name;
    cout << "Enter total number of batches: " << endl;
    cin >> Total;
    T = Total;
    P = Total;
    D = Total;
    Rejected = Total;
    
    while (infile)  {
          
          if (150 <= Temperature <= 170)  {
                 T = T--;
                 Rejected = Rejected--;  
                 }
          if (60 <= Pressure <= 70)  {
                 P = P--;
                else if (150 <= Temperature <= 170)  {
                        Rejected = Rejected;  
                        }
                else    {
                        Rejected = Rejected--;  
                        }
                        }
          if (2 <= DwellTime <= 2.5)  {
                 D = D--;
                else if (150 <= Temperature <= 170)  {
                        Rejected = Rejected;  
                        }
                else if (2 <= DwellTime <= 2.5);  {
                        Rejected = Rejected;  
                        }
                else    {
                        Rejected = Rejected-1;  
                        }  
                        }  
                        }
          
          
    double Tfail, Pfail, Dfail;
    Tfail = (T/Total)*100;
    Pfail = (P/Total)*100;
    Dfail = (D/Total)*100;
    
    cout << "Team Name: " << Name << endl;
    cout << "           Percent Failures     Total    |    Rejected" << endl;
    cout << "Temp       |" << Tfail << "      " << Total << "         " << Rejected << endl;
    cout << "Pressure       |" << Pfail << endl;
    cout << "Dwell Time       |" << Dfail << endl;
    system ("pause");
    return 0;
}
your missing formatting after your if statements.
1
2
3
 if (60 <= Pressure <= 70)  {
                 P = P--;
                     }


and

1
2
3
          if (2 <= DwellTime <= 2.5)  {
                 D = D--;
                     }
Last edited on
This condition is not going to work as intended:
 
if (60 <= Pressure <= 70) 

There are two conditions, each must be expressed in full. The logical and operator && is used to combine the two.
 
if ((60 <= Pressure) &&  (Pressure <= 70)) 


This assignment is wrong.
 
    T = T--;
It should be simply
 
    T--;


Its hard to interpret what the overall logic should be, there are different quantities being tested
Temperature
Pressure
DwellTime
and just what rules should be applied is not clear. Perhaps you have a plain English description of the requirements?

The while loop here
1
2
while (infile)  {
 }
looks like a problem. It will either not execute at all, or will result in an infinite loop which never terminates.

Topic archived. No new replies allowed.