New Posts  All Forums:Forum Nav:

if else trouble C++

post #1 of 4
Thread Starter 
Ok look at my program below. I can get the else statement to work if car 2 has a better mpg but even if car 1 does both statements execute.

I know I could condense some stuff but I like the spread out look at the moment

Probably something simple but I'm a little sleepy

Code:
#include <iostream>
using namespace std;

int main()
{
double lit1, lit2, gal1, gal2, mil1, mil2;
double  mpg1, mpg2;
double litr = 0.264179;
char ans;

do
{
cout << "How many miles did car 1 drive?" << endl;
cin >> mil1;
cout << endl;

cout << "How many liters did car 1 use?" << endl;
cin >> lit1;
cout << endl;

cout << "How many miles did car 2 drive?" << endl;
cin >> mil2;
cout << endl;

cout << "How many liters did car 2 use?" << endl;
cin >> lit2;
cout << endl;

gal1 = litr * lit1;

gal2 = lit2 * litr;

mpg1 = mil1 / gal1;

mpg2 = mil2 / gal1;

if (mpg2 < mpg1)
{
cout.precision(4);
cout << "Car 1 has a better MPG" << endl;
cout << "Car 1 " << mpg1 << endl;
cout << "Car 2 " << mpg2 << endl;
}

else (mpg1 < mpg2);
{
cout.precision(4);
cout << "Car 2 has a better MPG" << endl;
cout << "Car 1 " << mpg1 << endl;
cout << "Car 2 " << mpg2 << endl;
}

cout << "Do you want to run the program again?" << endl;
cout << "If so press Y anything else quits." << endl;
cin >> ans;
} while ((ans == 'Y') || (ans == 'y'));

return 0;
}
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #2 of 4
Don't put conditions after a else statement. A else will always run if the corresponding if statement is not true.

If you don't necessarily want the code in the else statement to run when the if statement fails then use a else if by doing:
Code:
if(condition1) //checks this condition
{ //runs this code if condition1 is true
}
else if(condition2) //will check condition2 if condition1 failed, otherwise this is skipped
{ //will run this code if condition2 is true
}
else //will run this code if both condition1 and 2 fail, this is skipped if either of the above are true
{
}
Note that one and only one part of a if-elseif-else block will ever be run. Also you can have as many elseifs as you want.
Edited by Xazen - 3/17/11 at 9:47pm
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #3 of 4
Quote:
Originally Posted by Xazen View Post
Don't put conditions after a else statement. A else will always run if the if statement is not true.
Yep.

Code:
if(<condition>)
{}
else if(<othercondition>)
{}
else
{}
Remember, you can have an if without an else, or an if-else if without an else.
Lappy
(9 items)
 
  
CPUMotherboardGraphicsRAM
P8700 Toshiba GM45 ICH9-M Intel 4 Series Samsung M4 
Hard DriveOptical DriveOSCase
Hitachi HTS723232L9A360 Matshita UJ880AS Windows 7 Pro 64 Toshiba A500-6621 
Mouse
Mionix Naos 5000 
  hide details  
Reply
Lappy
(9 items)
 
  
CPUMotherboardGraphicsRAM
P8700 Toshiba GM45 ICH9-M Intel 4 Series Samsung M4 
Hard DriveOptical DriveOSCase
Hitachi HTS723232L9A360 Matshita UJ880AS Windows 7 Pro 64 Toshiba A500-6621 
Mouse
Mionix Naos 5000 
  hide details  
Reply
post #4 of 4
Thread Starter 
Quote:
Originally Posted by Wiremaster View Post
Yep.

Code:
if(<condition>)
{}
else if(<othercondition>)
{}
else
{}
Remember, you can have an if without an else, or an if-else if without an else.
Thank you guys! I totally tried something like that but always left the parameter.

Will make note of that in my motes.

+rep
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming