0

Question is :

Please enter the number of gallons of gasoline: 100
Original number of gallons is: 100
100 gallons is the equivalent of 378.54 liters
Thanks for playing

how to display "Original number of gallons is: 100" and my "Thanks for playing" is combined with "press any key to continue" I dont know how to seperate them.

#include <iostream>
 using namespace std;
int main()
{
float g;
float l;

cout << "Please enter the number of gallons of gasoline ";
cin >> g;

l = g*3.7854;
cout << g << " Gallon is the equivalent of " << l << "liters" << endl;

cout << "Thank you for playing";

system("pause");
return 0;

}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Chen Long
  • 1
  • 1
  • 1
    `cout << "Original number of gallons is " << g << endl;`? And the second thing is because you forgot an `endl`. – user253751 Mar 18 '16 at 01:23
  • You mean.... `cout << "\"Original number of gallons is: 100\" and my "Thanks for playing" is combined with "press any key to continue" I dont know how to seperate them."` – Tdorno Mar 18 '16 at 01:25

3 Answers3

4
  1. Do not use "use namespace std;", this is bad practice.

  2. Using system("pause") is also bad practice.

  3. Use

    std::cout << std::endl;
    

To output a newline, to separate different lines of text.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0
#include <conio.h>   // Added for _getch();
#include <iostream>

// removed "using namespace std;"

int main() {
    float g = 0;  // Added Initialization To The Variables
    float l = 0;

    // From Here On Out I Will Be Using The Scope Resolution Operator To Access the std namespace.
    std::cout << "Please enter the number of gallons of gasoline" << std::endl;
    std::cin  >> g;


    l = g*3.7854f; // Added an 'f' at the end since you declared floats and not doubles

    std::cout "Original number of gallon(s) is: " << g << std::endl;
    std::cout << g << "Gallon(s) is the equivalent of " << l << "liter(s)" << std::endl;

    std::cout << "Thank you for playing" << std::endl;

    // Changed System( "pause" ); To

    std::cout << "\nPress any key to quit!\n";
    _getch();   
    return 0;

} // main
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
-2

Try placing the variables outside the main. Also, get rid of the system("pause"); and at the end of every cout, before the semicolon, add << "\n"; At the end, the code should look like this:

#include <iostream>

using namespace std;

float g;
float l;

int main()
{
cout << "Please enter the number of gallons of gasoline ";
cin >> g;

cout << "Original number of gallons is: " << g << "\n";

l = g*3.7854;
cout << g << " gallon(s) is/are the equivalent of " << l << " liters\n";

cout << "Thank you for playing\n";
return 0;

}