1

这是我的第一个计算通勤成本的程序。Visual Studio 在调试时遇到问题,所以我正在寻求帮助...

#include <iostream>
using namespace std;

int main()
{

int miles, gallons, gallonCost, mpg, mileCost, parking, tolls, FuelCost, TotalCost = 0.0;

有人可以解释上面的行在做什么(或不做什么)是制作浮点整数列表的正确方法吗?

cout << " How many miles do you drive per day? ";
    cin >> miles;

cout << " What is the price per gallon of fuel? ";
    cin << gallonCost;

cout << " How many gallons of fuel do you use per day? ";
    cin >> gallons;

mpg = miles / gallons;
mileCost = gallonCost / mpg;

cout << " Your fuel efficentcy is " << mpg ;" miles per gallon. ";
cout << " Your fuel cost is $" << mileCost ;" per mile. "; 

    FuelCost = mileCost * miles;

cout << " Your paying $" << FuelCost ;" for fuel per day.";

cout << " What are you daily parking fees? ";
    cin << parking;

cout << " How much do you spend on Tolls each day? ";
    cin >> tolls;

TotalCost = parking + tolls + FuelCost;

cout << " Your driving cost is $" << TotalCost ;" per day." endl;

system("PAUSE");
    return 0;
}

提前致谢

4

1 回答 1

3

不,这不是创建浮点变量的方法,而是创建整数变量的方法。没有“浮点整数”之类的东西。

您还应该收到很多关于表达式不执行任何操作的警告,例如在行中

cout << " Your fuel efficentcy is " << mpg ;" miles per gallon. ";
//                            Problem here ^

那是因为您在行中间有一个额外的分号,从而终止了输出语句。然后编译器找到一个字符串,它与表达式相同,所以没关系,但不做任何会导致警告的事情。而不是额外的分号,我怀疑你想要输出操作符<<

你应该在这一行得到一个错误:

cout << " Your driving cost is $" << TotalCost ;" per day." endl;
//                                              Error here ^

该错误是因为您有一个字符串后跟一个标识符。这不是一个有效的表达式。您可能在这里忘记了输出运算符<<

正是最后一个错误导致构建过程无法创建可执行文件,因此您无法运行/调试。始终注意编译器产生的消息,即使是警告也会告诉你一些有用的东西。

于 2013-09-20T07:20:19.713 回答