C++ 循环功能,直到满足正确的输入
我试图让我的代码保持循环,直到输入正确的输入,目前代码将编译并运行,但如果输入的int超出允许的范围,它将返回错误消息并结束,我试图找到一种方法来清除用户输入并再次请求输入,直到输入正确的值 我尝试过cin.clear和returns,但它们似乎不适用于函数,或者我根本不知道如何实现它们C++ 循环功能,直到满足正确的输入,c++,C++,我试图让我的代码保持循环,直到输入正确的输入,目前代码将编译并运行,但如果输入的int超出允许的范围,它将返回错误消息并结束,我试图找到一种方法来清除用户输入并再次请求输入,直到输入正确的值 我尝试过cin.clear和returns,但它们似乎不适用于函数,或者我根本不知道如何实现它们 #include <iostream> #include <string> using namespace std; void get_int_ref(int& n);
#include <iostream>
#include <string>
using namespace std;
void get_int_ref(int& n);
int main() {
int n = 0;
get_int_ref(n);
return 0;
}
void get_int_ref(int& n) {
cout<< "Please enter an integer\n";
cin>>n;
if( n > 4000 )
{
cout << "Error enter a number n: 0 < n < 4000: \n"
}
else if (n < 0)
{
cout << "Error enter a number n: 0 < n < 4000: \n";
}
else
{
cout<< n <<"\n";
}
}
#包括
#包括
使用名称空间std;
void get_int_ref(int&n);
int main(){
int n=0;
获取内部参考(n);
返回0;
}
void get_int_ref(int&n){
cout>n;
如果(n>4000)
{
当检测到正确的输入时,难道不能使用循环和中断吗
while( true )
{
cout<< "Please enter an integer\n";
cin>>n;
if( ( n > 4000 ) || ( n < 0 ) )
{
cout << "Error enter a number n: 0 < n < 4000: \n";
}
else
{
cout<< n <<"\n";
break;
}
}
while(true)
{
cout>n;
如果((n>4000)|(n<0))
{
当检测到正确的输入时,难道不能使用循环和中断吗
while( true )
{
cout<< "Please enter an integer\n";
cin>>n;
if( ( n > 4000 ) || ( n < 0 ) )
{
cout << "Error enter a number n: 0 < n < 4000: \n";
}
else
{
cout<< n <<"\n";
break;
}
}
while(true)
{
cout>n;
如果((n>4000)|(n<0))
{
cout将函数分为两个函数:
获取输入
检查输入是否有效
在main
的循环中调用它们
#include <iostream>
#include <string>
#include <limits> // Needed for numeric_limits
using namespace std;
bool is_input_valid(int n);
int get_input();
int main() {
int n = get_input();
while ( !(is_input_valid(n)) )
{
n = get_input();
}
return 0;
}
int get_input() {
int n;
cout << "Error enter a number between 0 and 4000: \n"
cin >> n;
// If there was a problem reading the input, clear the input stream
// and try again.
if ( !cin )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return get_input();
}
return n;
}
bool is_input_valid(int n)
{
return ( n >= 0 && n <= 4000 );
}
#包括
#包括
#包括//数字\u限制所需
使用名称空间std;
bool是输入有效(int n);
int get_input();
int main(){
int n=获取输入();
而(!(输入是否有效(n)))
{
n=获取_输入();
}
返回0;
}
int get_input(){
int n;
cout>n;
//如果读取输入时出现问题,请清除输入流
//然后再试一次。
如果(!cin)
{
cin.clear();
cin.ignore(std::numeric_limits::max(),'\n');
返回get_input();
}
返回n;
}
布尔值为有效输入值(整数n)
{
return(n>=0&&n将函数分为两个函数:
获取输入
检查输入是否有效
在main
的循环中调用它们
#include <iostream>
#include <string>
#include <limits> // Needed for numeric_limits
using namespace std;
bool is_input_valid(int n);
int get_input();
int main() {
int n = get_input();
while ( !(is_input_valid(n)) )
{
n = get_input();
}
return 0;
}
int get_input() {
int n;
cout << "Error enter a number between 0 and 4000: \n"
cin >> n;
// If there was a problem reading the input, clear the input stream
// and try again.
if ( !cin )
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return get_input();
}
return n;
}
bool is_input_valid(int n)
{
return ( n >= 0 && n <= 4000 );
}
#包括
#包括
#包括//数字\u限制所需
使用名称空间std;
bool是输入有效(int n);
int get_input();
int main(){
int n=获取输入();
而(!(输入是否有效(n)))
{
n=获取_输入();
}
返回0;
}
int get_input(){
int n;
cout>n;
//如果读取输入时出现问题,请清除输入流
//然后再试一次。
如果(!cin)
{
cin.clear();
cin.ignore(std::numeric_limits::max(),'\n');
返回get_input();
}
返回n;
}
布尔值为有效输入值(整数n)
{
return(n>=0&&n只需使用一个循环,例如:
int main()
{
int n = 0;
while (1)
{
cout << "Enter a number n: 0 < n < 4000: \n"
cin >> n;
if (is_entered_value_valid(n)) break;
}
return 0;
}
bool is_entered_value_valid(const int& n)
{
bool valid = true;
if( (n > 4000) || (n < 0) )
{
cout << "Error enter a number n: 0 < n < 4000: \n"
valid = false;
}
return valid;
}
intmain()
{
int n=0;
而(1)
{
cout>n;
如果(输入值有效(n))中断;
}
返回0;
}
输入布尔值有效(常量int&n)
{
bool valid=true;
如果((n>4000)|(n<0))
{
cout简单地使用一个循环,例如:
int main()
{
int n = 0;
while (1)
{
cout << "Enter a number n: 0 < n < 4000: \n"
cin >> n;
if (is_entered_value_valid(n)) break;
}
return 0;
}
bool is_entered_value_valid(const int& n)
{
bool valid = true;
if( (n > 4000) || (n < 0) )
{
cout << "Error enter a number n: 0 < n < 4000: \n"
valid = false;
}
return valid;
}
intmain()
{
int n=0;
而(1)
{
cout>n;
如果(输入值有效(n))中断;
}
返回0;
}
输入布尔值有效(常量int&n)
{
bool valid=true;
如果((n>4000)|(n<0))
{
库特
将输入读取为字符串
检查输入是否正确
如果没有,请再次询问用户
实施示例:
#include <sstream> // for using std::stringstream
void get_int_ref(int& n) {
// you shouldn't repeat writing the same message
static const char* message = "Error enter a number n: 0 < n < 4000: \n";
for(;;) // infinite loop
{
cout<< "Please enter an integer\n";
std::string inp;
if( !(cin>>inp) ) // failed to read a string
{
cout << "Input error\n";
n = -1;
break; // exit from the loop
}
std::stringstream ss;
ss << inp;
if( !(ss >> n) ) // what is read is not an integer
{
cout << message;
}
else if( n >= 4000 ) // correct condition according to the message
{
cout << message;
}
else if (n <= 0) // correct condition according to the message
{
cout << message;
}
else
{
cout << n << "\n";
break; // exit from the loop
}
}
}
#包含//用于使用std::stringstream
void get_int_ref(int&n){
//你不应该重复写同样的信息
static const char*message=“错误输入数字n:0inp))//读取字符串失败
{
cout n))//读取的不是整数
{
cout=4000)//根据消息更正条件
{
库特
将输入读取为字符串
检查输入是否正确
如果没有,请再次询问用户
实施示例:
#include <sstream> // for using std::stringstream
void get_int_ref(int& n) {
// you shouldn't repeat writing the same message
static const char* message = "Error enter a number n: 0 < n < 4000: \n";
for(;;) // infinite loop
{
cout<< "Please enter an integer\n";
std::string inp;
if( !(cin>>inp) ) // failed to read a string
{
cout << "Input error\n";
n = -1;
break; // exit from the loop
}
std::stringstream ss;
ss << inp;
if( !(ss >> n) ) // what is read is not an integer
{
cout << message;
}
else if( n >= 4000 ) // correct condition according to the message
{
cout << message;
}
else if (n <= 0) // correct condition according to the message
{
cout << message;
}
else
{
cout << n << "\n";
break; // exit from the loop
}
}
}
#包含//用于使用std::stringstream
void get_int_ref(int&n){
//你不应该重复写同样的信息
static const char*message=“错误输入数字n:0inp))//读取字符串失败
{
cout n))//读取的不是整数
{
cout=4000)//根据消息更正条件
{
你可以递归地做
void get_int_ref(int& n)
{
cout<< "Please enter an integer\n";
cin>>n;
if( n > 4000 )
{
cout << "Error enter a number n: 0 < n < 4000: \n";
get_int_ref(n);
}
else if (n < 0)
{
cout << "Error enter a number n: 0 < n < 4000: \n";
get_int_ref(n);
}
else
{
cout<< n <<"\n";
return;
}
}
void get\u int\u ref(int&n)
{
cout>n;
如果(n>4000)
{
你可以递归地做
void get_int_ref(int& n)
{
cout<< "Please enter an integer\n";
cin>>n;
if( n > 4000 )
{
cout << "Error enter a number n: 0 < n < 4000: \n";
get_int_ref(n);
}
else if (n < 0)
{
cout << "Error enter a number n: 0 < n < 4000: \n";
get_int_ref(n);
}
else
{
cout<< n <<"\n";
return;
}
}
void get\u int\u ref(int&n)
{
cout>n;
如果(n>4000)
{
你的正确输入是什么?任何介于0和4000之间的整数你的正确输入是什么?任何介于0和4000之间的整数不,我只能期望整数,其他什么都不能,所以这是fine@MikeCAT,谢谢你的留言,我会把这个添加到我的答案中。不,我只能期望int,其他什么都没有,所以这是fine@MikeCAT,谢谢你的留言,我会将此添加到我的ans中我们只需要int什么也不需要int什么也不需要int什么也不需要int什么else@Ausghostdogl,请详细说明。@MikeCAT是怎么说boo的,输入永远只能是int,而不能是double或string或其他任何东西ints@Ausghostdog,我明白了。不过,只要多做一点努力,让你的程序更健壮总是好的。@Ausghostdog如何授权?删除你的程序,不让任何人执行程序?只需要int-nothingelse@Ausghostdogl,请详细说明。@MikeCAT是怎么说boo的,输入永远只能是int,而不能是double或string或其他任何东西ints@Ausghostdog,我明白了。不过,让你的节目更精彩总是好的只需一点额外的努力,就变得健壮了。@Ausghostdog你是如何授予它的?删除你的程序,不让任何人执行程序?好主意