requesting assistance with a class assignment

ok so this is the homework

Write a complete C++ program that calculates the miles per gallon for a tank of gasoline. It should prompt for the beginning and ending mileage for the tank, and the number of gallons of gasoline for the fill-up. Then it will calculate the number of miles driven and the average miles per gallon. Display these values with reasonable text. You may assume the user will enter reasonable values for all prompts.

Print the program to turn in. Use good style and appropriate comments. E-mail me the source and the output from several test runs. Allow fractions in all entries.

this is for a begginer class so i dont know much other then how to make comments the (;) for the end of a command and #include <iostream>
using namespace std; and thats it the teacher gave us an example of an already done program but im not sure how to use that to solve this

heres that program

#include <iostream>
using namespace std;
// named constant to give mnemonic name to "magic number"
const float SqFtPerGal = 350.0f;
// the function main() is always the entry point of the application
int main()
{
float length, width, area, paint; // to hold user values and results
// prompt user for length and width of wall
cout << "Enter length of wall : " << flush;
cin >> length;
cout << "Enter width of wall : " << flush;
cin >> width;
// calculate area and amount of paint needed
area = length * width;
paint = area / SqFtPerGal;
// output results with reasonable text
cout << "You need " << paint << " gallons of paint to cover "
<< area << " square feet of wall." << endl;
// program stops executing when it returns from main(), 0 means O.K.
return 0;
}

if someone can give me some tips or walk me through this that would be great.

This is your original program formatted using the source code tags:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream> 
using namespace std;
 
// named constant to give mnemonic name to "magic number"
const float SqFtPerGal = 350.0f;
// the function main() is always the entry point of the application
int main()
{
  float length, width, area, paint; // to hold user values and results
  // prompt user for length and width of wall
  cout << "Enter length of wall : " << flush;
  cin >> length;
  cout << "Enter width of wall : " << flush;
  cin >> width;
  // calculate area and amount of paint needed
  area = length * width;
  paint = area / SqFtPerGal;
  // output results with reasonable text
  cout << "You need " << paint << " gallons of paint to cover "
       << area << " square feet of wall." << endl;
  // program stops executing when it returns from main(), 0 means O.K.
  return 0;
}


Before you start writing code, you should take some time to read your problem and think it through carefully (and if necessary, write psuedo-code statements about what you want to do/accomplish).

I will not write out the code needed to solve your problem; but, I can explain the above program as the techniques used there are required to be used in your HW problem.

BEGIN

Line 1: This "includes" the 'iostream' library. What is the iostream library and what is its purpose? It is a library of functions dedicated to generic input and output. If you want to output something to the screen/user, or request a value from the user - you must include this library. The program you listed does both, so this library is needed.

Line 2: "using namespace std;" is the std namespace, or a way to shorten the amount of code called when writing all the functions and classes in the C++ standard library. This saves a lot of time writing code as instead of cout you will have to write std::cout.

Line 5: A constant variable of type float is declared (SqFtPerGal). You can declare constants of any primitive type. What this means is that the value that the variable holds (in this case SqFtPerGal cannot be changed later on). The f at the end of 350.0f;, ensures that the type of the value of the variable is a float value. If you wanted an integer, it would be i;

Line 7: This is the beginning of the main function, which is where the program is run.

Line 8: Beginning of the main function body. To close that body of code, and effectively terminate the program, a closing brace is required at the end of the program.

Line 9: float length, width, area, paint; declares 4 variables of type float: length, width, area, paint respectively.

Line 11: cout, pronounced "see-out", sends the information to the screen, this is called an output stream. What information? The information to the right of the insertion operator "<<". Finally, at the end of the line, the last insertion operator is followed by the word flush. This is not a variable, but is actually a function of cout. What this does is insert an end of line "endl" at the end of the stream.

Line 12: cin, pronounced "see-in", takes the information from the keyboard and puts it into a variable. Concordantly, the operator that cin uses is called the extraction operator ">>", as it extracts information from the keyboard, an input stream and stores it into a variable on the right side of the extraction operator. In this case, information is extracted from the keyboard and stored in the variable length which is of type float. At runtime, there will simply be a blinking cursor waiting on the user to enter information to store in a variable.

Line 13: Similar process to line 11.

Line 14: Similar process to line 12.

Line 16: The variable area, is assigned the value of the product of values of the variables length and width.

Line 17: Similar process to line 16. Quotient is assigned instead.

Line 19: Information is sent to the screen/user, and the values of the variables at the right of each insertion operator is sent to the screen as well.

Line 20: Since the previous line was not terminated by a ";", the insertion operator can continue as it does in this line. Finally, the stream is terminated;

Line 22: The program returns 0, and effectively ends.

Line 23: Closing brace. End of program.


I will also strongly suggest that you peruse the tutorials on this site, and practice with them - they will help you a great deal. As always, keep programming!
Topic archived. No new replies allowed.