I've been staring at this code for about 3 hours, changing things back and forth and making myself feel really stupid....please, anyone, help me.

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()

{
	//declare variables
	int gas, time, distance;

	// could have used \n all in one cout statement but felt it looked sloppy
	cout << "Please choose from the following gases:\n";
	cout << "(1) Carbon Dioxide" << endl;
	cout << "(2) Air" << endl;
	cout << "(3) Helium" << endl;
	cout << "(4) Hydrogen" << endl;
	cin >> gas;
	
	if (gas = 1)
	{
		cout << "You chose Carbon Dioxide. Now enter how long it took for sound to \n";
		cout << "travel through it in seconds." << endl;
		cout << "Time: ";
		cin >> time;

		
			distance = 258.0 * time;
		
		cout << "The sound came from " << distance << " meters away." << endl;
		
	else
	
		
		if (gas = 2)
		{
			cout << "You chose Air. Now enter how long it took for sound to \n";
			cout << "travel through it in seconds." << endl;
			cin >> time;

			distance = 331.5 * time;

			cout << "The sound came from " << distance << " meters away." << endl;
			
		else 
		
				
			if (gas = 3)
			{
				cout << "You chose Helium. Now enter how long it took for sound to \n";
				cout << "travel through it in seconds." << endl;
				cin >> time;

				distance = 972 * time;

				cout << "The sound came from " << distance << " meters away." << endl;

			else
			
			
				if (gas = 4)
				{
					cout << "You chose Hydrogen. Now enter how long it took for sound to \n";
					cout << "travel through it in seconds." << endl;
					cin >> time;

					distance = 1270 * time;

					cout << "The sound came from " << distance << " meters away." << endl;
	
				else
					cout << "You did not choose one of the 4 gases." << endl;
				}
			}
		}
	}				
					
	

	cin.ignore();
	cout << "Press ENTER to exit.";
	cin.get();


	return 0;
}

When I try to compile it, I get:

(46): error C2181: illegal else without matching if
(59): error C2181: illegal else without matching if
(72): error C2181: illegal else without matching if

I know its telling me something about my if statements, I just can't figure out what.

Recommended Answers

All 3 Replies

Shouldn't you have a closing brace on line 32.

You've defined your ifs

if ()
{
else
}

It should be

if ()
{
} <===== you need this
else
{ <===== and this
}

Here is how you use an if statement:

if(condition is true)
{
   ......
   ......
}
else:
   do this;

The else comes after the program statement(s), but you are making it a part of the program statements. It's OK to do this:

if(condition true)
{
   if(condition true)
   {
      ......
   }
   else:
      do this;
}
else:
   this instead;

Here each else has a matching if. The "else" means that if the statement(s) following the if aren't chosen to execute (i.e., the statements within the { } ), then the else statements will execute instead, so then how can you have this:

if(true)
{
   else:
}

With an if statement, you have two statement(s), but only one will be selected to execute:

if(true)
{
   ......
   ......
}

else:
{   ......
    ......
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.