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.