C++ 如何向用户显示特定错误,要求用户使用循环再次提供输入? 使用名称空间std; int main(){ //变量声明 字符串小时数=”; 双小时=0; bool cont=真; 做{ //获取员工一周工作多少的输入。 库特(小时) //检查值是否为值>=0,如果是,则退出循环 如果(小时数>=0){ 小时数\u输入>>empHours;//为empHours赋值并退出循环 cont=假; } //检查输入是否包含特殊字符或任何其他形式的错误输入,如果是,则重复循环并再次要求用户输入。 其他的 cont=真; }while(续); cout
您可以使用C++ 如何向用户显示特定错误,要求用户使用循环再次提供输入? 使用名称空间std; int main(){ //变量声明 字符串小时数=”; 双小时=0; bool cont=真; 做{ //获取员工一周工作多少的输入。 库特(小时) //检查值是否为值>=0,如果是,则退出循环 如果(小时数>=0){ 小时数\u输入>>empHours;//为empHours赋值并退出循环 cont=假; } //检查输入是否包含特殊字符或任何其他形式的错误输入,如果是,则重复循环并再次要求用户输入。 其他的 cont=真; }while(续); cout,c++,C++,您可以使用while循环 它可以是这样的: using namespace std; int main(){ // Variable declarations string hours = ""; double empHours = 0; bool cont = true; do{ // Get input of how much employee worked in a week. cout << "En
while
循环
它可以是这样的:
using namespace std;
int main(){
// Variable declarations
string hours = "";
double empHours = 0;
bool cont = true;
do{
// Get input of how much employee worked in a week.
cout << "Enter hours worked in a week: " ;
getline(cin, hours);
// Convert the input using string stream for easier validation.
stringstream hours_input(hours);
for(int i = 0; i <= hours[i]; i++)
// Check if input contains any alphabets e.g 90abc, if yes than repeat loop and ask user for input again.
if(isalpha(hours[i]))
cont = true;
// If the input successfully converts to double type
else if(hours_input >> empHours)
// Check if values are values >= 0, if yes than exit the loop
if(empHours >= 0){
hours_input >> empHours; // Assign value to empHours and exit loop
cont = false;
}
// Check if input contains special characters or any other form of bad input, if yes than repeat loop and ask user for input again.
else
cont = true;
}while(cont);
cout << "Value is: " << empHours << endl;
return 0;
}
while(true){
中国>>富;
if(检查foo是否为有效输入){
break;//如果输入有效
}
cout您可以使用while
循环
它可以是这样的:
using namespace std;
int main(){
// Variable declarations
string hours = "";
double empHours = 0;
bool cont = true;
do{
// Get input of how much employee worked in a week.
cout << "Enter hours worked in a week: " ;
getline(cin, hours);
// Convert the input using string stream for easier validation.
stringstream hours_input(hours);
for(int i = 0; i <= hours[i]; i++)
// Check if input contains any alphabets e.g 90abc, if yes than repeat loop and ask user for input again.
if(isalpha(hours[i]))
cont = true;
// If the input successfully converts to double type
else if(hours_input >> empHours)
// Check if values are values >= 0, if yes than exit the loop
if(empHours >= 0){
hours_input >> empHours; // Assign value to empHours and exit loop
cont = false;
}
// Check if input contains special characters or any other form of bad input, if yes than repeat loop and ask user for input again.
else
cont = true;
}while(cont);
cout << "Value is: " << empHours << endl;
return 0;
}
while(true){
中国>>富;
if(检查foo是否为有效输入){
break;//如果输入有效
}
cout当前,您的代码不包含任何用于打印错误消息的内容。不过,您似乎已经在处理错误场景,因此添加它并不难
如果像这样更改for
循环中的else
大小写,它应该可以工作:
while(true){
cin>>foo;
if(check if foo is a valid input){
break; //if the input is valid
}
cout<<"error, try again";
}
for(int i=0;i=0,如果是,则退出循环
如果(小时数>=0){
小时数\u输入>>empHours;//为empHours赋值并退出循环
cont=假;
}
//检查输入是否包含特殊字符或任何其他形式的错误输入,如果是,则重复循环并再次要求用户输入。
其他的
{
cout当前,您的代码不包含任何用于打印错误消息的内容。不过,您似乎已经在处理错误场景,因此添加它并不难
如果像这样更改for
循环中的else
大小写,它应该可以工作:
while(true){
cin>>foo;
if(check if foo is a valid input){
break; //if the input is valid
}
cout<<"error, try again";
}
for(int i=0;i=0,如果是,则退出循环
如果(小时数>=0){
小时数\u输入>>empHours;//为empHours赋值并退出循环
cont=假;
}
//检查输入是否包含特殊字符或任何其他形式的错误输入,如果是,则重复循环并再次要求用户输入。
其他的
{
库特