-3

这是我的代码开始的地方。在代码的最底部,我将详细介绍错误点。

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    //declare variables

int usedWater = 0;
int prevReading = 0;
int currReading = 0;
const double WATER_RATE = .007;
const double MIN_CHARGES = 16.67;
double totalCharges = 0.0;

cout <<"enter the current reading: ";
cin >> currReading;
cout <<"enter the previous reading: ";
cin >> prevReading;



usedWater = currReading - prevReading;

totalCharges = usedWater * WATER_RATE;

if(totalCharges < MIN_CHARGES)
{

totalCharges = MIN_CHARGES << endl;
cout << "total charges: "  << MIN_CHARGES;   

}
else
{
cout << "Total Charges : " << totalCharges;

} 

cout << "total Charges : " << totalCharges;
cout << "Used Water : " << usedWater;

system ("pause");

 return 0;
}

第 36 行是我得到错误的地方。这个:(totalCharges = MIN_CHARGES << endl;)
我看不到任何拼写错误。或者可能是一些错误的词。

4

2 回答 2

2
totalCharges = MIN_CHARGES << endl;

不确定您到底想达到什么目的,但我怀疑它会更好:

totalCharges = MIN_CHARGES;

您当前的行试图做的是设置totalChargesMIN_CHARGES左移位的值endl。即使编译器允许,那可能也不会很好结束:-)


而且,顺便说一句,您可能可以用一行代码替换大部分代码:

totalCharges = std::min (MIN_CHARGES, usedWater * WATER_RATE);

您可能还希望在大多数语句的末尾添加换行符,cout <<这样您的输出就不会集中在一行中。

正确的做法通常是使用'\n'而不是std::endl因为后者强制刷新,这可能导致性能下降(尽管在这种特定情况下可能不明显)。

于 2013-02-20T02:50:21.440 回答
1

<< endl不属于此作业:

totalCharges = MIN_CHARGES;

std::endl是用于添加换行符和刷新流的流操纵器。MIN_CHARGES是一个double const值,因此这样的按位左移操作为零意义。

你可能的意思是:

if (totalCharges < MIN_CHARGES)
{
    totalCharges = MIN_CHARGES;
}

cout << "Total Charges : " << totalCharges;
于 2013-02-20T02:53:38.177 回答