gpt4 book ai didi

c++ - do\while 语句将无法正确编译(初学者)

转载 作者:太空狗 更新时间:2023-10-29 21:14:51 25 4
gpt4 key购买 nike

请放轻松,因为我是 C++ 的新手,目前正在上编程入门课:)

我目前在正确编译 do\while 语句时遇到问题。我的目标是输入以下内容,直到输入标记值 -1,这将结束该过程并提供我的值的累计总数。

以下是我需要为每个实体输入的值:

 gallons used 
starting mileage
ending mileage
miles driven
miles per gallon

一旦输入标记值,它就应该为所有合并的实体计算以下值:

total gallons used
total miles driven
overall average miles per gallon

以下是我到目前为止的内容,但输入 -1 时并未结束。当输入 -1 时,我还没有添加尚未使用的最终值,因为我不确定如何继续并试图让第 1 部分首先正常工作。下面是我的代码(请原谅评论):

int main(void) 
{

StartUp(); //indicates beginning of code

double startmileage{ 0 }; //indicates starting mileage, initialized at the value of 0
double endmileage{ 0 }; //indicates ending mileage, initialized at the value of 0
double milesdriven{ 0 }; //indicates total miles driven, initialized at the value of 0
double gallonsused{ 0 }; //indicates total gallons used, initialized at the value of 0
long double milespergallon{ 0 }; //indicates total miles per gallon, initialized at the value of 0

do
{
cout << "enter the gallons used: "; //prompts user to enter total number of gallons used for trip
cin >> gallonsused; //user enters data

cout << "enter starting mileage: "; //prompts user to enter start mileage for trip
cin >> startmileage; //user enters data

cout << "enter ending mileage: "; //prompts user to enter end mileage for trip
cin >> endmileage; //user enters data

milesdriven = startmileage + endmileage; //calculates total miles driven

cout << "Miles Driven: " << milesdriven << endl; //outputs total miles driven to screen

milespergallon = milesdriven / gallonsused; //calculates total miles per gallon

cout << "Miles Per Gallon: " << milespergallon << endl; //outputs miles per gallon to screen
}

while (gallonsused != -1);
WrapUp(); //indicates end of code

return Success;

}

这是正在发生的事情:

enter the gallons used:  12.8
enter starting mileage: 0
enter ending mileage: 277
Miles Driven: 277
Miles Per Gallon: 21.6406
enter the gallons used: -1
enter starting mileage:

如您所见,它会循环,但不会在输入标记值时停止

如有任何帮助,我们将不胜感激!

谢谢!

最佳答案

在循环结束之前,您不会测试 gallonsused != -1,在用户响应所有其他提示并计算里程之后。

不要使用 do-while(),而是在用户输入后立即测试 gallonsused,然后跳出循环。

您还需要总行驶英里数和使用的总加仑数的变量。每次输入后,您将该行程的值添加到总计中,并在最后将它们用于总计。

double totalmiles = 0;
double totalgallons = 0;

while (true)
{
cout << "enter the gallons used: "; //prompts user to enter total number of gallons used for trip
cin >> gallonsused; //user enters data
if (gallonsused == -1) { // Check for sentinel
break;
}

totalgallons += gallonsused;

cout << "enter starting mileage: "; //prompts user to enter start mileage for trip
cin >> startmileage; //user enters data

cout << "enter ending mileage: "; //prompts user to enter end mileage for trip
cin >> endmileage; //user enters data

milesdriven = startmileage + endmileage; //calculates total miles driven

totalmiles += milesdriven;

cout << "Miles Driven: " << milesdriven << endl; //outputs total miles driven to screen

milespergallon = milesdriven / gallonsused; //calculates total miles per gallon

cout << "Miles Per Gallon: " << milespergallon << endl; //outputs miles per gallon to screen
}

// Show grand totals
cout << "Grand totals:" << endl;

cout << "Total Miles Driven: " << totalmiles << endl;
cout << "Total Miles Per Gallon: " << totalmiles/totalgallons << endl;

WrapUp();

关于c++ - do\while 语句将无法正确编译(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39563754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com