title: Void function to print two different answers using same variable
body: <p>My homework last week was</p>
  
  <blockquote>
    <p>Write a program that will read in the number of liters of gasoline consumed and miles traveled by 2 cars, then output the number of miles per gallon of fuel consumed by each vehicle and compare the two for fuel efficiency.</p>
  </blockquote>
  
  <p>Which I completed. This week the project is to modify the previous project and</p>
  
  <blockquote>
    <p>Add two void functions:</p>
    
    <p>one called userInstruction that tells the user what to do</p>
    
    <p>Another displayResult that displays the original information (number of liters and miles driven) and the result (miles per gallon).</p>
  </blockquote>
  
  <p>It seems I have managed to get it mostly working. It compiles and runs at least. However, Car 1 seems to be displaying the liters of fuel used and miles driven from Car 2, despite it getting the correct miles per gallon. I'm not sure what I'm doing wrong. Maybe I'm completely off the mark in what she wants which is why I can't figure it out?</p>
  
  <p>Also, as a side question, is there any way to get the "Do the same with car 2" into the <code>userInstruction</code> void function?</p>
  
  <pre><code>const double LPG = 0.264179;
  
  double mpg(double miles, double liters);
  void userInstruction();
  void displayResult(double mpg1, double mpg2, double liters, double miles);
  
  int main()
  {
      char keepGoing;
      double liters, miles, mpg1, mpg2;
      do
      {
          userInstruction();
          cin &gt;&gt; liters;
          cin &gt;&gt; miles;
          //calling function
          mpg1 = mpg(miles, liters);
  
          cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Do the same for Car 2. \n" &lt;&lt; endl;
          cin &gt;&gt; liters;
          cin &gt;&gt; miles;
  
          mpg2 = mpg(miles, liters);
  
          displayResult(mpg1, mpg2, liters, miles);
  
          if (mpg1 == mpg2)
          {
              cout &lt;&lt; "Both cars have the same fuel efficiency. \n" &lt;&lt; endl;
          }
          else if (mpg1 &gt; mpg2)
          {
              cout &lt;&lt; "Car 1 has better fuel efficiency. \n" &lt;&lt; endl;
          }
          else
          {
              cout &lt;&lt; "Car 2 has better fuel efficency. \n" &lt;&lt; endl;
          }
          //Allows for repeating
          cout &lt;&lt; "Do you wish to keep going? (y/n)  \n";
          cin &gt;&gt; keepGoing;
      }
  
      while (keepGoing == 'y' || keepGoing == 'Y');
  
      system("pause");
      return 0;
  }
  
  double mpg(double miles, double liters)
  {
      double milesPerGallon;
      milesPerGallon = miles / (liters * LPG);
      return milesPerGallon;
  }
  
  void userInstruction()
  {
      cout &lt;&lt; "Please enter the liters of gasoline consumed as well as the miles travelled. \n" &lt;&lt; endl;
  }
  
  void displayResult(double mpg1, double mpg2, double liters, double miles)
  {
      cout &lt;&lt; "Car 1 used: " &lt;&lt; liters &lt;&lt; " liters of fuel \n";
      cout &lt;&lt; "Car 1 drove: " &lt;&lt; miles &lt;&lt; " miles \n";
      cout &lt;&lt; "Car 1 gets: " &lt;&lt; mpg1 &lt;&lt; " miles per gallon \n" &lt;&lt; endl;
  
      cout &lt;&lt; "Car 2 used: " &lt;&lt; liters &lt;&lt; " liters of fuel \n";
      cout &lt;&lt; "Car 2 drove: " &lt;&lt; miles &lt;&lt; " miles \n";
      cout &lt;&lt; "Car 2 gets: " &lt;&lt; mpg2 &lt;&lt; " miles per gallon \n" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Here is where I expected the results of the inputs (5 30) and (55 123) to print out in their respective correct spots.</p>
  
  <pre>
  Car 1 used: 55 liters of fuel
  Car 1 drove: 123 miles
  Car 1 gets: 22.7119 miles per gallon
  
  Car 2 used: 55 liters of fuel
  Car 2 drove: 123 miles
  Car 2 gets: 8.46533 miles per gallon
  
  Car 1 has better fuel efficiency.
  </pre>