gpt4 book ai didi

C++ 变量在 "if"循环中丢失赋值

转载 作者:行者123 更新时间:2023-11-28 00:11:55 27 4
gpt4 key购买 nike

所以我确定这是一个非常简单的问题,但我才刚刚开始。

在程序中,有简单的输入验证。如果输入正确,没有问题。

问题是,当测试程序出现错误时,例如输入零或负数,输出中的所有变量都是空白(即字符串变为空白,数字变为零)。

提前感谢您的帮助和见解。

// This menu driven program determines the time sound will take to travel through
// gas, liquid, and solid, given a distance from a user.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
// constants
const double AIR_SPEED_RATE_PER_SECOND_SOUND = 1100.0, //in feet per second
WATER_SPEED_RATE_PER_SECOND_SOUND = 4900.0, // in feet per second
STEEL_SPEED_RATE_PER_SECOND_SOUND = 16400.0; // in feet per second

// Program defined variables
double Time_To_Travel = 0.0; // in seconds

string Medium;

// User defined variables
double distance_of_travel; //in feet

int menu_selection;

//Display a menu for mediums of sound conduction.
cout << "Sound travels at different speeds through air, water, and steel." << endl;
cout << "\nThis program will calculate the time it takes, in feet per second, for " \
"sound to travel a given distance." << endl;
cout << "Please select a number choice below:\n\n1. Air\n2. Water\n3. Steel " << endl;

//Get input from user.
cout << "\nEnter Selection: ";
cin >> menu_selection;

cout << "\nEnter distance in feet the sound will travel: ";
cin >> distance_of_travel;


// Input validate selection is on the menu
if (menu_selection >= 1 && menu_selection <= 3)
{
if (distance_of_travel > 0.0) // input validation distance is positive
{
switch (menu_selection) // calculate the time of travel based on user input
{
case 1: Medium = "air";
Time_To_Travel = distance_of_travel / AIR_SPEED_RATE_PER_SECOND_SOUND;
break;

case 2: Medium = "water";
Time_To_Travel = distance_of_travel / WATER_SPEED_RATE_PER_SECOND_SOUND;
break;

case 3: Medium = "steel";
Time_To_Travel = distance_of_travel / STEEL_SPEED_RATE_PER_SECOND_SOUND;
break;
}
}
else
{
cout << "\nPlease enter a distance greater than zero: ";
cin >> distance_of_travel;
}
}

else
{
cout << "\nMenu selection is not 1, 2, or 3.\n\nPlease correctly enter a number 1 through 3: ";
cin >> menu_selection;
}
// Format to four decimal places and display the time sound takes to travel given distance.
cout << fixed << showpoint << setprecision(4);
cout << "Sound would take " << Time_To_Travel << " seconds to travel given distance of " << distance_of_travel << " feet in " << Medium << "." << endl;;

return 0;

最佳答案

if 语句是一个简单的分支,而不是循环。在 if 的末尾,执行会继续到 block 的末尾。

if (menu_selection >= 1 && menu_selection <= 3)

当为 false 时,将跳过程序的核心内容并跳转到处理无效输入的代码。

else
{
cout << "\nMenu selection is not 1, 2, or 3.\n\nPlease correctly enter a number 1 through 3: ";
cin >> menu_selection;
}

再次输入menu_selection后,控制流向

cout << fixed << showpoint << setprecision(4);
cout << "Sound would take " << Time_To_Travel << " seconds to travel given distance of " << distance_of_travel << " feet in " << Medium << "." << endl;;
return 0;

新的输入永远不会被执行,并且未被修改的值被打印出来。将初始的 if 替换为包装用户输入的 do {...} while(condition); 循环。一旦输入令人满意,您就可以进入程序的核心部分。

bool is_good;
do
{
is_good = false;
cout << "\nEnter Selection: ";
cin >> menu_selection;
cout << "\nEnter distance in feet the sound will travel: ";
cin >> distance_of_travel;
if (menu_selection < 1 || menu_selection > 3 || distance_of_travel < 0)
cout << "error message here";
else
is_good = true;
} while (!is_good);

关于C++ 变量在 "if"循环中丢失赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32556827/

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