请放轻松我,因为我是C ++的新手,目前正在编程介绍课程:)
我目前在获取正确编译的do \ while语句时遇到问题。我的目标是输入以下内容,直到输入-1的标记值,这将结束该过程并给出我的值的累计总数。
以下是我需要为每个实体输入的值:
import requests
table_data = requests.get('http://bonusbagging.co.uk/odds-server/getdata_slow.php').content
输入标记值后,应计算所有实体组合的以下内容:
gallons used
starting mileage
ending mileage
miles driven
miles per gallon
以下是我到目前为止的情况,但输入-1时并没有结束。当输入-1时,我还没有添加最终值,因为我不确定如何继续并且我试图让第1部分首先正常工作。以下是我的代码(请原谅评论):
total gallons used
total miles driven
overall average miles per gallon
以下是发生的事情:
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;
}
如您所见,它循环,但在输入标记值时不会停止
非常感谢任何帮助!
谢谢!
答案 0 :(得分:3)
在用户响应所有其他提示并计算里程后,您不会在循环结束前测试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();
答案 1 :(得分:0)
循环体(花括号之间)被视为一个单元,由do-while控件重复。在cin用于检查-1的加仑后尝试放置if语句,使用break来退出循环。尝试这一点应该会有所帮助。
答案 2 :(得分:0)
/dev/null
表达式,就像while
循环中的条件一样,在代码中的位置进行评估,它们不是监督。
for
输出:
#include <iostream>
int main() {
int i = 0;
do {
std::cout << i << " ";
i += 2;
std::cout << i << ' ';
i -= 1;
std::cout << i << ' ';
} while (i != 2);
}