Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ while statement help
New Posts  All Forums:Forum Nav:

C++ while statement help

post #1 of 15
Thread Starter 
I got help from you guys before and figured I'd come back since I'm stuck again. I have to make this program:
Write, compile, debug, and run a program that will calculate gasoline mileage for a trip consisting of multiple parts. For each part of a trip, it will prompt for and accept as input an odometer reading (if you are not sure what this is, please ask or look it up) and number of gallons of gasoline, and will calculate miles driven and the mpg (miles per gallon) for that part and print out all 4 values in row of a table.
Assume that the odometer reading before the start of the trip was 0.
Display all doubles with 2 decimal places.
Your program will continue to request input until the user types in 0 for the odometer.
You will have to calculate the miles driven during each part! How?
After all input is done, print the length of the trip, and calculate and print out Trip MPG, which is the total mileage for the whole trip divided by the total gallons for the whole trip. (Note: the trip MPG is NOT the average of all the partial MPG’s!)
Code Header input/output:
List the input of 3 specific odometer readings and 3 specific number of gallons, list expected miles driven and mpg for each. Also list the Trip MPG.
Note: You will have prompts and input in between the output lines of the table.  That's okay!  Have the output table display over to the right so that it looks better.
Here is an example of the output:
Odometer Gallons Miles MPG
(some prompts and input)
321 20 321 16.05
(some prompts and input)
543 13 222 17.08
(some prompts and input)
601 3 58 19.34
(some prompts and input)
774 13 173 13.31
This trip was 774 miles. You used 49 gallons.
Your mileage for this trip was: 15.80 miles per gallon.

The code I have so far is follows:

int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
prevodometer = totalodometer - prevodometer;
MPG = prevodometer/gallons;
cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

}
system("PAUSE");
return 0;
}
When I get to the third input which would be 601 and 3, it screws up because it's subtracting 222 and not 543. Is there any way to fix this so it works properly?

Edit: Sorry for the formatting. Is there a better way to format?
Edited by Damaging Excess - 3/6/13 at 9:45am
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #2 of 15
I don't think this is the problem, but you declare totalodometer and use it before you actually initialize it.
 
El Escorial
(17 items)
 
La Reconquista
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770K GA-Z87-UD5H MSI GeForce GTX 780 G.SKILL Trident X 10-12-12-31 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro Samsung F3 XSPC Raystorm CPU Waterblock XSPC Razor GTX Titan/GTX780 
OSOSMonitorMonitor
Gentoo x86_64 Windows 7 Professional 64 bit QNIX QX2710 Gateway FHD2401 
KeyboardPowerCaseMouse
Filco Majestouch-2 Tenkeyless Seasonic SS-760XP2 Fractal Define R4 Window Logitech G500 
Audio
Asus Xonar Essence STX 
CPUMotherboardGraphicsRAM
Intel Core i7 D0 @ 4.0 Ghz Gigabyte GA-EX58-UD3R PNY GTX 470 OCZ 6GB DDR3 1600 
Hard DriveHard DriveHard DriveOptical Drive
Western Digital Green Samsung F3 Western Digital Blue x16 DVD-ROM burner 
OSOSMonitorMonitor
Arch Linux Windows 7 Professional x64 Qnix QX2710 Gateway FHD2401 24" 
KeyboardPowerCaseMouse
Filco Majestictouch Corsair 650w CM 690 II Logitech G500 
CPUMotherboardGraphicsRAM
AMD 64 Athlon San Diego 4000+ MSI MS-7191 XFX Nvidia Geforce 7800gt 2 Gig DDR 400 
Hard DriveOSMonitorKeyboard
Seagate 250 gig Harddrive Windows XP SP3 COMPAQ 7500 COMPAQ default 
PowerCaseMouseMouse Pad
"Power Up" 550 Watt XION Solaris XON-406 Logitech Lazer Mouse Fate/Stay Night Mouse Pad 
  hide details  
Reply
 
El Escorial
(17 items)
 
La Reconquista
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770K GA-Z87-UD5H MSI GeForce GTX 780 G.SKILL Trident X 10-12-12-31 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro Samsung F3 XSPC Raystorm CPU Waterblock XSPC Razor GTX Titan/GTX780 
OSOSMonitorMonitor
Gentoo x86_64 Windows 7 Professional 64 bit QNIX QX2710 Gateway FHD2401 
KeyboardPowerCaseMouse
Filco Majestouch-2 Tenkeyless Seasonic SS-760XP2 Fractal Define R4 Window Logitech G500 
Audio
Asus Xonar Essence STX 
CPUMotherboardGraphicsRAM
Intel Core i7 D0 @ 4.0 Ghz Gigabyte GA-EX58-UD3R PNY GTX 470 OCZ 6GB DDR3 1600 
Hard DriveHard DriveHard DriveOptical Drive
Western Digital Green Samsung F3 Western Digital Blue x16 DVD-ROM burner 
OSOSMonitorMonitor
Arch Linux Windows 7 Professional x64 Qnix QX2710 Gateway FHD2401 24" 
KeyboardPowerCaseMouse
Filco Majestictouch Corsair 650w CM 690 II Logitech G500 
CPUMotherboardGraphicsRAM
AMD 64 Athlon San Diego 4000+ MSI MS-7191 XFX Nvidia Geforce 7800gt 2 Gig DDR 400 
Hard DriveOSMonitorKeyboard
Seagate 250 gig Harddrive Windows XP SP3 COMPAQ 7500 COMPAQ default 
PowerCaseMouseMouse Pad
"Power Up" 550 Watt XION Solaris XON-406 Logitech Lazer Mouse Fate/Stay Night Mouse Pad 
  hide details  
Reply
post #3 of 15
A better way to format your post would be to use the code tool
Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
prevodometer = totalodometer - prevodometer;
MPG = prevodometer/gallons;
cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

}
system("PAUSE");
return 0;
}

Just in case you didn't know you could do that smile.gif
Animus
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 3770K Asus Maximus V Formula Z77 EVGA GTX980Ti SC+ ACX 2.0+ 2 x 4GB Corsair Dominator GT 1866Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro 128GB 2 x WD 1TB Blue EZEX RAID0 EK Supremacy - Copper / Acetal EK Titan X & Backplate - Copper / Acetal 
CoolingCoolingCoolingCooling
EK 360XTX EK Spin Reservoir - Acetal Liang DDC / EK Top EK PSC Compression Fittings 
OSMonitorKeyboardPower
Windows 7 Professional 64bit 3 x Samsung P2450H 24" Filco Majestouch 2 - Cherry Blues Corsair TX750 
CaseMouseMouse PadAudio
Corsair 800D Razer Deathadder 2013 Razer Destructor 2 Creative Titanium HD 
AudioAudio
Fiio E9 Amplifier Beyerdynamic DT880 Premium (600 Ohm) 
  hide details  
Reply
Animus
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 3770K Asus Maximus V Formula Z77 EVGA GTX980Ti SC+ ACX 2.0+ 2 x 4GB Corsair Dominator GT 1866Mhz 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro 128GB 2 x WD 1TB Blue EZEX RAID0 EK Supremacy - Copper / Acetal EK Titan X & Backplate - Copper / Acetal 
CoolingCoolingCoolingCooling
EK 360XTX EK Spin Reservoir - Acetal Liang DDC / EK Top EK PSC Compression Fittings 
OSMonitorKeyboardPower
Windows 7 Professional 64bit 3 x Samsung P2450H 24" Filco Majestouch 2 - Cherry Blues Corsair TX750 
CaseMouseMouse PadAudio
Corsair 800D Razer Deathadder 2013 Razer Destructor 2 Creative Titanium HD 
AudioAudio
Fiio E9 Amplifier Beyerdynamic DT880 Premium (600 Ohm) 
  hide details  
Reply
post #4 of 15
Thread Starter 
Quote:
Originally Posted by Crooksy View Post

A better way to format your post would be to use the code tool
Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
prevodometer = totalodometer - prevodometer;
MPG = prevodometer/gallons;
cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

}
system("PAUSE");
return 0;
}

Just in case you didn't know you could do that smile.gif
I did not know how to do that. Thank you biggrin.gif
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #5 of 15
Thread Starter 
Quote:
Originally Posted by Luminouslight View Post

I don't think this is the problem, but you declare totalodometer and use it before you actually initialize it.
I did that so the while loop would start no matter what because we're forced into using a while loop instead of a do while which would have been the preferred choice
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #6 of 15
Well, prevodometer from the looks of the code is actually the mileage between odometer readings, not the previous odometer reading. Not entirely sure if that is what you wanted.
 
El Escorial
(17 items)
 
La Reconquista
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770K GA-Z87-UD5H MSI GeForce GTX 780 G.SKILL Trident X 10-12-12-31 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro Samsung F3 XSPC Raystorm CPU Waterblock XSPC Razor GTX Titan/GTX780 
OSOSMonitorMonitor
Gentoo x86_64 Windows 7 Professional 64 bit QNIX QX2710 Gateway FHD2401 
KeyboardPowerCaseMouse
Filco Majestouch-2 Tenkeyless Seasonic SS-760XP2 Fractal Define R4 Window Logitech G500 
Audio
Asus Xonar Essence STX 
CPUMotherboardGraphicsRAM
Intel Core i7 D0 @ 4.0 Ghz Gigabyte GA-EX58-UD3R PNY GTX 470 OCZ 6GB DDR3 1600 
Hard DriveHard DriveHard DriveOptical Drive
Western Digital Green Samsung F3 Western Digital Blue x16 DVD-ROM burner 
OSOSMonitorMonitor
Arch Linux Windows 7 Professional x64 Qnix QX2710 Gateway FHD2401 24" 
KeyboardPowerCaseMouse
Filco Majestictouch Corsair 650w CM 690 II Logitech G500 
CPUMotherboardGraphicsRAM
AMD 64 Athlon San Diego 4000+ MSI MS-7191 XFX Nvidia Geforce 7800gt 2 Gig DDR 400 
Hard DriveOSMonitorKeyboard
Seagate 250 gig Harddrive Windows XP SP3 COMPAQ 7500 COMPAQ default 
PowerCaseMouseMouse Pad
"Power Up" 550 Watt XION Solaris XON-406 Logitech Lazer Mouse Fate/Stay Night Mouse Pad 
  hide details  
Reply
 
El Escorial
(17 items)
 
La Reconquista
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770K GA-Z87-UD5H MSI GeForce GTX 780 G.SKILL Trident X 10-12-12-31 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro Samsung F3 XSPC Raystorm CPU Waterblock XSPC Razor GTX Titan/GTX780 
OSOSMonitorMonitor
Gentoo x86_64 Windows 7 Professional 64 bit QNIX QX2710 Gateway FHD2401 
KeyboardPowerCaseMouse
Filco Majestouch-2 Tenkeyless Seasonic SS-760XP2 Fractal Define R4 Window Logitech G500 
Audio
Asus Xonar Essence STX 
CPUMotherboardGraphicsRAM
Intel Core i7 D0 @ 4.0 Ghz Gigabyte GA-EX58-UD3R PNY GTX 470 OCZ 6GB DDR3 1600 
Hard DriveHard DriveHard DriveOptical Drive
Western Digital Green Samsung F3 Western Digital Blue x16 DVD-ROM burner 
OSOSMonitorMonitor
Arch Linux Windows 7 Professional x64 Qnix QX2710 Gateway FHD2401 24" 
KeyboardPowerCaseMouse
Filco Majestictouch Corsair 650w CM 690 II Logitech G500 
CPUMotherboardGraphicsRAM
AMD 64 Athlon San Diego 4000+ MSI MS-7191 XFX Nvidia Geforce 7800gt 2 Gig DDR 400 
Hard DriveOSMonitorKeyboard
Seagate 250 gig Harddrive Windows XP SP3 COMPAQ 7500 COMPAQ default 
PowerCaseMouseMouse Pad
"Power Up" 550 Watt XION Solaris XON-406 Logitech Lazer Mouse Fate/Stay Night Mouse Pad 
  hide details  
Reply
post #7 of 15
Thread Starter 
Quote:
Originally Posted by Luminouslight View Post

Well, prevodometer from the looks of the code is actually the mileage between odometer readings, not the previous odometer reading. Not entirely sure if that is what you wanted.
I know. I just don't know how to go about wording it so it actually takes the previous entery and subtracts it from the new entry given they're the same variable
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #8 of 15
Quote:
Originally Posted by Damaging Excess View Post

I know. I just don't know how to go about wording it so it actually takes the previous entery and subtracts it from the new entry given they're the same variable
Your previous total odometer is going to be the next prevodometer. You shouldn't need to subtract anything from the total to find it. Just make sure to keep it for the next iteration of the loop.

Separate your mileage in a different variable. or just
Code:
MPG = (totalodometer-prevoodometer)/gallons

EDIT:
This should probably be what you are looking for.
Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
MPG = (totalodometer-prevodometer)/gallons;

cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

prevodometer = totalodometer;

}
system("PAUSE");
return 0;
}

Edited by Luminouslight - 3/6/13 at 10:17am
 
El Escorial
(17 items)
 
La Reconquista
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770K GA-Z87-UD5H MSI GeForce GTX 780 G.SKILL Trident X 10-12-12-31 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro Samsung F3 XSPC Raystorm CPU Waterblock XSPC Razor GTX Titan/GTX780 
OSOSMonitorMonitor
Gentoo x86_64 Windows 7 Professional 64 bit QNIX QX2710 Gateway FHD2401 
KeyboardPowerCaseMouse
Filco Majestouch-2 Tenkeyless Seasonic SS-760XP2 Fractal Define R4 Window Logitech G500 
Audio
Asus Xonar Essence STX 
CPUMotherboardGraphicsRAM
Intel Core i7 D0 @ 4.0 Ghz Gigabyte GA-EX58-UD3R PNY GTX 470 OCZ 6GB DDR3 1600 
Hard DriveHard DriveHard DriveOptical Drive
Western Digital Green Samsung F3 Western Digital Blue x16 DVD-ROM burner 
OSOSMonitorMonitor
Arch Linux Windows 7 Professional x64 Qnix QX2710 Gateway FHD2401 24" 
KeyboardPowerCaseMouse
Filco Majestictouch Corsair 650w CM 690 II Logitech G500 
CPUMotherboardGraphicsRAM
AMD 64 Athlon San Diego 4000+ MSI MS-7191 XFX Nvidia Geforce 7800gt 2 Gig DDR 400 
Hard DriveOSMonitorKeyboard
Seagate 250 gig Harddrive Windows XP SP3 COMPAQ 7500 COMPAQ default 
PowerCaseMouseMouse Pad
"Power Up" 550 Watt XION Solaris XON-406 Logitech Lazer Mouse Fate/Stay Night Mouse Pad 
  hide details  
Reply
 
El Escorial
(17 items)
 
La Reconquista
(13 items)
 
CPUMotherboardGraphicsRAM
Intel Core i7 4770K GA-Z87-UD5H MSI GeForce GTX 780 G.SKILL Trident X 10-12-12-31 
Hard DriveHard DriveCoolingCooling
Samsung 840 Pro Samsung F3 XSPC Raystorm CPU Waterblock XSPC Razor GTX Titan/GTX780 
OSOSMonitorMonitor
Gentoo x86_64 Windows 7 Professional 64 bit QNIX QX2710 Gateway FHD2401 
KeyboardPowerCaseMouse
Filco Majestouch-2 Tenkeyless Seasonic SS-760XP2 Fractal Define R4 Window Logitech G500 
Audio
Asus Xonar Essence STX 
CPUMotherboardGraphicsRAM
Intel Core i7 D0 @ 4.0 Ghz Gigabyte GA-EX58-UD3R PNY GTX 470 OCZ 6GB DDR3 1600 
Hard DriveHard DriveHard DriveOptical Drive
Western Digital Green Samsung F3 Western Digital Blue x16 DVD-ROM burner 
OSOSMonitorMonitor
Arch Linux Windows 7 Professional x64 Qnix QX2710 Gateway FHD2401 24" 
KeyboardPowerCaseMouse
Filco Majestictouch Corsair 650w CM 690 II Logitech G500 
CPUMotherboardGraphicsRAM
AMD 64 Athlon San Diego 4000+ MSI MS-7191 XFX Nvidia Geforce 7800gt 2 Gig DDR 400 
Hard DriveOSMonitorKeyboard
Seagate 250 gig Harddrive Windows XP SP3 COMPAQ 7500 COMPAQ default 
PowerCaseMouseMouse Pad
"Power Up" 550 Watt XION Solaris XON-406 Logitech Lazer Mouse Fate/Stay Night Mouse Pad 
  hide details  
Reply
post #9 of 15
Thread Starter 
Quote:
Originally Posted by Luminouslight View Post

Your previous total odometer is going to be the next prevodometer. You shouldn't need to subtract anything from the total to find it. Just make sure to keep it for the next iteration of the loop.

Separate your mileage in a different variable. or just
Code:
MPG = (totalodometer-prevoodometer)/gallons

EDIT:
This should probably be what you are looking for.
Code:
int main ( )
{

double totalodometer, prevodometer, MPG, gallons;
prevodometer = 0;
cout << "\t\t\tOdometer Gallons Miles Driven MPG" << endl
<< "\t\t\t
" << endl;
while( totalodometer != 0)
{
cout << "Enter odometer reading " << endl;
cin >> totalodometer;
if (totalodometer == 0)
break;
else
cout << "Enter gallons used: " << endl;
cin >> gallons;
MPG = (totalodometer-prevodometer)/gallons;

cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;

prevodometer = totalodometer;

}
system("PAUSE");
return 0;
}
Not quite. It fixed the mpg but now the miles driven isn't being displayed right. It's just taking the previous entry and not the difference between the old entry and the new one
Trinity
(15 items)
 
  
Reply
Trinity
(15 items)
 
  
Reply
post #10 of 15
All you have to do is add
Code:
prevodometer = totalodometer;
Code:
while( totalodometer != 0)
{
        cout << "Enter odometer reading " << endl;
        cin >> totalodometer;
        if (totalodometer == 0)
                break;
        else
                cout << "Enter gallons used: " << endl;
                cin >> gallons;
        prevodometer = totalodometer - prevodometer;
        MPG = prevodometer/gallons;
        cout << "\t\t\t " << totalodometer << "\t " << gallons << "\t " << prevodometer << "\t " << MPG << endl;
        prevodometer = totalodometer;
}

You are changing prevodometer and not setting it back to totalodometer.
2009
(15 items)
 
   
CPUMotherboardGraphicsRAM
i7 920 4.0Ghz 1.20v golden? Asus p6x58D Premium 770gtx Windforce OC ocz gold 1600 8-8-8-24-74-1T 
Hard DriveCoolingOSMonitor
x25-m80G2 Black640 H100 GT AP-15s Windows 7 Pro. 64 bit Dell U2410 1920x1200 IPS 
PowerCaseAudio
corsair tx 850 antec 1200 SoundBlaster ZX 
CPUMotherboardGraphicsRAM
Intel i7 3720qm(ivy) 2.6 Sager GTX 670m 8 gig 
Hard DriveKeyboardPower
80gig mssd steel series Huge power brick 
  hide details  
Reply
2009
(15 items)
 
   
CPUMotherboardGraphicsRAM
i7 920 4.0Ghz 1.20v golden? Asus p6x58D Premium 770gtx Windforce OC ocz gold 1600 8-8-8-24-74-1T 
Hard DriveCoolingOSMonitor
x25-m80G2 Black640 H100 GT AP-15s Windows 7 Pro. 64 bit Dell U2410 1920x1200 IPS 
PowerCaseAudio
corsair tx 850 antec 1200 SoundBlaster ZX 
CPUMotherboardGraphicsRAM
Intel i7 3720qm(ivy) 2.6 Sager GTX 670m 8 gig 
Hard DriveKeyboardPower
80gig mssd steel series Huge power brick 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ while statement help