我是C ++的新手,我想问几个问题

时间:2016-03-18 01:21:56

标签: c++

问题是:

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

如何显示“原始加仑数为:100” 我的“感谢你玩”与“按任意键继续”相结合我不知道如何分开它们。

#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;

}

3 个答案:

答案 0 :(得分:4)

  1. Do not use "use namespace std;",这是不好的做法。

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

  3. 使用

    std::cout << std::endl;
    
  4. 输出换行符,以分隔不同的文本行。

答案 1 :(得分: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

答案 2 :(得分:-2)

尝试将变量放在main之外。 另外,在分号之前删除system("pause");和每个cout的末尾,添加<< "\n"; 最后,代码应如下所示:

#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;

}