Expected Expression error on an "else if" clause.

I have tried everything I know about fixing "else if" clauses. Can someone provide some direction on what I may have messed up in the "else if" clause or what might have gone array above it?

Thanks in advance!

#include <iostream>
#include<iomanip>

using namespace std;

int main ( ) {

//decimal rules

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);

//variables

char kay = 'y';

float ctemp, ftemp;

cout << "Please enter the Fahrenheit temperature(s). ";
cout << "Press ENTER after entering all the numbers you wish to have converted to Celsius." << endl;
cout << endl;
cout << endl;

//Beginning of Chart

cout << setw(1) << "Fahrenheit" << setw(20) << "Centigrade" << endl;
while(kay == 'y' || kay == 'Y')
{
while(!(cin >> ftemp)) {
cout << "Please enter a non intiger with only on decimal point." << endl;
cin.clear();
cin.ignore(1000, '\n');
if(ftemp < 10000) {
ctemp = (ftemp - 32) * 5/9;
cout << setw(1) << ftemp << setw(20) << ctemp << endl;
else if (ftemp >= 10000) {
ctemp = (ftemp - 32) * 5/9;
cout << setw(1) << ftemp << setw(20) << ctemp << endl;
}}
system("pause");

}}}
What error are you getting and what have you tried?
Are you using an IDE that formats your code automatically? (Then you can see the error immediately)
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
#include <iostream>
#include<iomanip>

using namespace std;

int main() 
{
  //decimal rules

  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(1);

  //variables

  char kay = 'y';

  float ctemp, ftemp;

  cout << "Please enter the Fahrenheit temperature(s). ";
  cout << "Press ENTER after entering all the numbers you wish to have converted to Celsius." << endl;
  cout << endl;
  cout << endl;

  //Beginning of Chart

  cout << setw(1) << "Fahrenheit" << setw(20) << "Centigrade" << endl;
  while (kay == 'y' || kay == 'Y')
  {
    while (!(cin >> ftemp))
    {
      cout << "Please enter a non intiger with only on decimal point." << endl;
      cin.clear();
      cin.ignore(1000, '\n');
      if (ftemp < 10000)
      {
        ctemp = (ftemp - 32) * 5 / 9;
        cout << setw(1) << ftemp << setw(20) << ctemp << endl;
      else if (ftemp >= 10000)
      {
        ctemp = (ftemp - 32) * 5 / 9;
        cout << setw(1) << ftemp << setw(20) << ctemp << endl;
      }
      system("pause");
    }
  }
}


There was a } missing before the else if (ftemp >= 10000)
Also there was one } too many before system("pause");
These things are easy to spot if you format your code properly.
Last edited on
closed account (48T7M4Gy)
Or this as a start:
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
#include <iostream>
#include<iomanip>

using namespace std;

int main ( )
{
    
    //decimal rules
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);
    
    //variables
    char kay = '?';
    float ctemp, ftemp;
    
    cout << "Please enter the Fahrenheit temperature(s). ";
    cout << "Press ENTER after entering all the numbers you wish to have converted to Celsius." << endl;
    
    //Beginning of Chart
    cout << setw(1) << "Fahrenheit" << setw(20) << "Centigrade" << endl;
    
    while(cout << " --->   Continue? <y/n>: " and cin >> kay and toupper(kay) == 'Y')
    {
        while(cout << "Please enter a temperature: " and cin >> ftemp)
        {
            cout << "Please enter a non integer with only on decimal point." << endl;
            
            ctemp = (ftemp - 32) * 5/9;
                cout << setw(1) << ftemp << setw(20) << ctemp << endl;
        }
    }
    return 0;
}
This is probably beacuse you have the else if statement inside your if block (The if block before elseif is not closed until after else if). The else if statement, thus, has no initial if statement.

Your code:
1
2
3
4
5
6
7
8
9
if(ftemp < 10000) {                       // 'if' block opened
      ctemp = (ftemp - 32) * 5/9;
      cout << setw(1) << ftemp << setw(20) << ctemp << endl;
                                                     // 'if' block still active
else if (ftemp >= 10000) {
     ctemp = (ftemp - 32) * 5/9;
     cout << setw(1) << ftemp << setw(20) << ctemp << endl;
}}                                                 // 'elseif' closed and then 'if' closed


My suggestion

1
2
3
4
5
6
7
8
9
if(ftemp < 10000) {                                // 'if' block opened
      ctemp = (ftemp - 32) * 5/9;
      cout << setw(1) << ftemp << setw(20) << ctemp << endl;
}                                                            // 'if' block closed

else if (ftemp >= 10000) {                     // 'elseif' block opened
      ctemp = (ftemp - 32) * 5/9;
      cout << setw(1) << ftemp << setw(20) << ctemp << endl;
}                                                            // 'elseif' block closed 
Topic archived. No new replies allowed.