C++ 变量在 "if" 循环中丢失赋值
C++ Variables Losing Assignment Inside "if" Loop
所以我确定这是一个非常简单的问题,但我才刚刚开始。
在程序中,有简单的输入验证。如果输入正确,没有问题。
问题是,当测试程序出现错误时,例如输入零或负数,输出中的所有变量均为空白(即字符串变为空白,数字变为零)。
提前感谢您的帮助和见解。
// 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
的末尾,执行继续超过块的末尾。
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);
您可以通过将 default
块添加到 switch 语句来处理 case
块中未定义的零、负数或所有可能的输入。然后你的代码看起来像这样。
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;
default:
// handle zero, negative numbers and so on.
break;
}
参考:http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm
所以我确定这是一个非常简单的问题,但我才刚刚开始。
在程序中,有简单的输入验证。如果输入正确,没有问题。
问题是,当测试程序出现错误时,例如输入零或负数,输出中的所有变量均为空白(即字符串变为空白,数字变为零)。
提前感谢您的帮助和见解。
// 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
的末尾,执行继续超过块的末尾。
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);
您可以通过将 default
块添加到 switch 语句来处理 case
块中未定义的零、负数或所有可能的输入。然后你的代码看起来像这样。
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;
default:
// handle zero, negative numbers and so on.
break;
}
参考:http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm