Need help with simple programming

Sep 1, 2017 at 8:16pm
write a C++ program that will calculate the miles per gallon a car gets given the gallons of gas used and the total miles driven. Assume that the car holds 15 gallons of gas and travels a total of 350 miles on a full tank. The formula for miles per gallon (MPG) is given by

MPG = total miles driven/total gallons used.

(so we already know the answer: 23.33 mpg). Your output should be one line:

“The car gets xxx miles per gallon.”, where the xx is the MPG computed by the program (should be about 23.33)


I keep trying and it won't run. this was my latest attempt:
// This program calculates the miles per gallon a car gets given the gallons of gas used and the total miles driven.

Last edited on Sep 2, 2017 at 7:57pm
Sep 1, 2017 at 8:35pm
The compiler doesn't know what to do with this line:
 
    gallons, miles, MPG;

It should be something like:
 
    double gallons, miles, MPG;


See Declaration of variables
http://www.cplusplus.com/doc/tutorial/variables/
Sep 1, 2017 at 8:41pm
okay thank you! i added the double and the little red lines underneath gallons, miles and MPG disappeared, however, it still gives me an error when i run it. Any idea what else im doing wrong?
Sep 1, 2017 at 8:47pm
It gives a compiler error at cout.

For now add the line
 
using namespace std;
at the top, just above int main()
Sep 1, 2017 at 8:48pm
In C++ almost everything is in a namespace, so you need to qualify.
This code works on the shell.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    double gallons, miles, MPG;
// Get the number of gallons the car holds.
cout << "How many gallons of gas does your car hold? ";
cin >> gallons;

// Get total miles driven.
cout << "How many miles does it travel on a full tank? ";
cin >> miles;

// Calculate the miles per gallon.
MPG = miles / gallons;

// Display the pay.
cout << "The car gets xx miles per gallon" << MPG << endl ;
return 0;
}

Output

How many gallons of gas does your car hold? 15
How many miles does it travel on a full tank? 350
The car gets xx miles per gallon23.3333


http://cpp.sh/6me5s

About namespaces: https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm
Last edited on Sep 1, 2017 at 8:49pm
Sep 1, 2017 at 8:58pm
so sorry but it gives giving me an error! :( there are no squiggly lines though, so i dont know why its not working
Sep 1, 2017 at 9:00pm
What is the error message - can you copy and paste it here please.
Sep 1, 2017 at 9:12pm
1>------ Build started: Project: ConsoleApplication8, Configuration: Debug Win32 ------
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Platforms\Win32\PlatformToolsets\v141\Toolset.targets(34,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".
1>Done building project "ConsoleApplication8.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

but first i get this: C:// name of file is not recognized as an internal or external command, oprable program or batch file. press any key to continue....
Sep 2, 2017 at 9:26am
Looks like you are missing the Windows SDK 8.1.
https://developer.microsoft.com/en-us/windows/downloads/windows-8-1-sdk

Another option:
change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution".
Sep 2, 2017 at 6:19pm
thank you thomas, that seemed to be the problem. however, i'm still having trouble with it.
this is my current code:


and when i run the program, it works perfectly up to how many moles does it travel on a full tank, after i input the number and press enter, it takes me out. any suggestions?
Last edited on Sep 2, 2017 at 7:57pm
Sep 2, 2017 at 6:23pm
Your code updated code should work. What does it mean it takes you out? Try using system("pause");
Sep 2, 2017 at 6:36pm
so i click start without debugging, and it takes me to a black screen and it asks how many gallons of gas....then i put 15, enter and it asks me how many miles does it travel...then i enter 350 and enter, and it takes me to my homescreen
Sep 2, 2017 at 6:36pm
like it never displays "the car gets 23.333 miles per gallon"
Sep 2, 2017 at 7:18pm
What happens is this. The program reaches the end. Then the console window (the black screen) closes immediately. There are lots of ways to deal with this, some better than others.

Here you might try, just before the return 0;, just above that line, add these two lines:
1
2
    cin.ignore(1000, '\n');
    cin.get();


Now it should wait for you to press the 'enter' key.
Sep 2, 2017 at 7:56pm
okay it worked thank you so much!! :)
Sep 4, 2017 at 11:02am
Why do you keep deleting your code? It makes this thread useless as a resource for others to learn from. Please don't do that.
Last edited on Sep 4, 2017 at 11:02am
Topic archived. No new replies allowed.