ERROR: "else with no matching if"

Here is my first program, I had to use a lot of if/ else statements. For some reason the last "else" (bolded) is giving me an error and not letting me run the program. whats going on?

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
#include <iostream>
using namespace std;

const float MILES_PER_KM      = 1.0 / 1.61;
const float KM_PER_MILE       = 1.61;
const float FREEZING_F        = 32.0;
const float FAHR_PER_CELSIUS  = 9.0 / 5.0;
const float CELSIUS_PER_FAHR  = 5.0 / 9.0;
const float MAX_F_TEMP        = 100.0;
const float MIN_F_TEMP        = 0.0;
   
int main ()
{
   char unit;
   float value;
   cout << "Input a type and an amount (K, M, F, C): ";
   cin >> unit;
   cin >> value;
   
   if (unit == 'K')
      if ( value < 0 )
         cout << "UNABLE TO PROCESS: Negative distances are not supported.";
      else   
         cout << "Distance " << value << " kilometers" << " is "
              << ( value * MILES_PER_KM ) << " miles.";
   else if (unit == 'M')
      if ( value < 0 )
         cout << "UNABLE TO PROCESS: Negative distances are not supported.";
      else
         cout << "Distance " << value << " miles" << " is "
              << value * KM_PER_MILE << " kilometers.";
    else if (unit == 'C')
      cout << "Temperature " << value << " C is "
           << ( (value * FAHR_PER_CELSIUS) + FREEZING_F ) << " F.";
    else if (unit == 'F')
       if (value > MAX_F_TEMP || value < MIN_F_TEMP)
          cout << "UNABLE TO PROCESS: Temperature too hot or too cold.";
          cout << "Please enter a temperature comfortable when wearing a sweater.";
       else 
          cout << "Temperature " << value << " F is "
               << ((value - FREEZING_F) / FAHR_PER_CELSIUS) << " C.";
   else 
      cout << "UNABLE TO PROCESS: " << unit << " is not a recognized measurement.";
   
      
            
   
   
   return 0;
}


and yes, the if else's are indented properly, because there are no errors for the first 3 unit types, only for the last one ( 'F' ), and i have them all exactly in the same format.
Last edited on
If you want to use multiple statements within an if statement, you need to use braces.

1
2
3
4
5
6
7
if(...) {
    ...
    ...
}
else {
    ...
}


Without braces, you can only have one statement that follows, and you have two right before that bolded else.

PS: Use code tags next time. It's the <> button to the right ->
Thank you! it worked.

and do the code tags reserve the spacing/ indenting?
Preserve? Yes.
To improve your program a bit, make use of the following:

A ShowMenu function, which prints out the options with descriptions.

A switch statement that has a case for each option in the menu, possibly calling a function for each one. the switch also has a default: clause that catches bad input.

Enclose the whole thing in a loop like this:

1
2
3
4
5
6
7
8
bool Quit = false;

while (!Quit) {
//switch statement here

//user wants to quit
Quit = true;
}


HTH
Branflakes91093 wrote:
If you want to use multiple statements within an if statement, you need to use braces.


In fact, I'd go further: as a matter of good practice, you should always put braces around the code that is executed conditionally, even if it is only a single statement. It is frighteningly easy to introduce bugs by going back and adding a second statement, and forgetting to add the braces. I've seen this happen in every single one of the jobs I've worked at, and I've done it a few times myself.

It's much safer to protect yourself from this by putting in the braces every time.
Topic archived. No new replies allowed.