C++ 如果用户输入无效,则循环
我想创建一个程序,当用户输入我没有定义的内容时,程序会再次提示他 我用if语句做了,但它只循环了1次,不再循环了。我尝试了循环,但只要输入为false,它就会破坏条件并拒绝所有相同的输入。在C++中, 非常感谢您的帮助C++ 如果用户输入无效,则循环,c++,loops,conditional-statements,C++,Loops,Conditional Statements,我想创建一个程序,当用户输入我没有定义的内容时,程序会再次提示他 我用if语句做了,但它只循环了1次,不再循环了。我尝试了循环,但只要输入为false,它就会破坏条件并拒绝所有相同的输入。在C++中, 非常感谢您的帮助 #include <iostream> #include <string> using namespace std; void xD(){string x; do{cout << "Retry\n"; cin >>
#include <iostream>
#include <string>
using namespace std;
void xD(){string x;
do{cout << "Retry\n";
cin >> x;}while(true);}
//declaring a function to make the shop
void shop(){
string x;
float coins = 500;
float bow_cost = 200;
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
cin >> x;
// if u chose bow you get this and get to choose again
if (x == "bow"){
cout << "you bought the bow.\n you now have " <<coins - bow_cost << " coins." << endl; cin >> x;}
/*now the problem that whenever I excute the code and type something other than bow it gives me the cin only once more and then fails even if I type bow in the 2nd attempt*/
//in my desperate 5k attempt, I tried creating a function for it.. no use.
//i want it o keep prompting me for input till i type "bow" and the other block excutes. but it never happens.
else{xD();}
}
int main(){
string name;
string i;
cout << "if you wish to visit the shop type \"shop\"\n";
cin >> i;
if(i == "shop"){shop();}
else{cin >> i;}
return 0;
}
#包括
#包括
使用名称空间std;
void xD(){string x;
do{cout>x;}while(true);}
//声明一个函数来创建商店
无效商店(){
字符串x;
浮动硬币=500;
浮船首成本=200;
cout x;
//如果你选择了弓,你会得到这个,然后再次选择
如果(x=“船首”){
我不能;}
返回0;
}
而不是使用这种方法(只检查一次条件):
注意使用了compare方法而不是==运算符
有关详细信息,请参阅文档,而不是使用此方法(只检查一次条件): 注意使用了compare方法而不是==运算符
有关详细信息,请参阅文档您可以在此处使用cin>>[您的输入对象]的返回值来检查状态或istream的方法
fail()
。一旦输入流无法解析全部或部分流,它就会失败并保持失败状态,直到您清除它为止。未解析的输入被保留(所以您可以尝试以不同的方式解析它?)m所以,如果您再次尝试>>到相同类型的对象,您将得到相同的失败。要忽略输入的N个字符,有一种方法
istream::ignore(streamsize amount, int delim = EOF)
例如:
int getInt()
{
while (1) // Loop until user enters a valid input
{
std::cout << "Enter an int value: ";
long long x; // if we'll use char, cin would assume it is character
// other integral types are fine
std::cin >> x;
// if (! (std::cin >> x))
if (std::cin.fail()) // has a previous extraction failed?
{
// yep, so let's handle the failure, or next >> will try parse same input
std::cout << "Invalid input from user.\n";
std::cin.clear(); // put us back in 'normal' operation mode
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // and remove the bad input
}
// Thechnically you may do only the above part, but then you can't distingusih invalid format from out of range
else if(( x > std::numeric_limits<int>::max()) ||
( x < std::numeric_limits<int>::min()))
{
std::cout << "Invalid value.\n";
}
else // nope, so return our good x
return x;
}
}
intgetint()
{
while(1)//循环,直到用户输入有效的输入
{
std::cout>将尝试分析相同的输入
std::cout您可以在此处使用cin>>[您的输入对象]的返回值来检查状态或istream的方法fail()
。一旦输入流无法解析全部或部分流,它就会失败并保持失败状态,直到您清除它。未解析的输入会被保留(因此您可以尝试以不同方式解析它?)所以,若你们再次尝试>>相同类型的对象,你们会得到同样的失败
istream::ignore(streamsize amount, int delim = EOF)
例如:
int getInt()
{
while (1) // Loop until user enters a valid input
{
std::cout << "Enter an int value: ";
long long x; // if we'll use char, cin would assume it is character
// other integral types are fine
std::cin >> x;
// if (! (std::cin >> x))
if (std::cin.fail()) // has a previous extraction failed?
{
// yep, so let's handle the failure, or next >> will try parse same input
std::cout << "Invalid input from user.\n";
std::cin.clear(); // put us back in 'normal' operation mode
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // and remove the bad input
}
// Thechnically you may do only the above part, but then you can't distingusih invalid format from out of range
else if(( x > std::numeric_limits<int>::max()) ||
( x < std::numeric_limits<int>::min()))
{
std::cout << "Invalid value.\n";
}
else // nope, so return our good x
return x;
}
}
intgetint()
{
while(1)//循环,直到用户输入有效的输入
{
std::cout>将尝试分析相同的输入
std::cout问题在于该循环块中的条件
void xD(){
string x;
do{
cout << "Retry\n";
cin >> x;
}while(true);
}
这应该行得通。但是,它对我来说仍然太复杂。这可以简化为以下代码片段:
void shop(){
string x;
float coins = 500;
float bow_cost = 200;
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
cin >> x;
while (x!="bow"){
cout << "Retry\n";
cin>>x;
}
cout << "you bought the bow.\n you now have " <<coins - bow_cost << " coins." << endl; cin >> x;
}
void shop(){
字符串x;
浮动硬币=500;
浮船首成本=200;
cout x;
而(x!=“鞠躬”){
cout>x;
}
cout问题在于循环块中的条件
void xD(){
string x;
do{
cout << "Retry\n";
cin >> x;
}while(true);
}
这应该行得通。但是,它对我来说仍然太复杂。这可以简化为以下代码片段:
void shop(){
string x;
float coins = 500;
float bow_cost = 200;
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
cin >> x;
while (x!="bow"){
cout << "Retry\n";
cin>>x;
}
cout << "you bought the bow.\n you now have " <<coins - bow_cost << " coins." << endl; cin >> x;
}
void shop(){
字符串x;
浮动硬币=500;
浮船首成本=200;
cout x;
而(x!=“鞠躬”){
cout>x;
}
我们可以考虑一下,if()
语句是单向的,它检查条件,如果条件满足,它会转到括号中,然后诸如此类,如果条件编译器有任何问题,它会通过if
并跳到编译其他代码
每次开始编译代码时,它都从int main()
函数开始。您在if
和else
语句中又犯了错误
这是正确的代码。我做了必要的更改
#include "stdafx.h"
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
#define coins 500 ;
#define bow_cost 200 ;
int shop(string x)
{
//There is no need to allocate extra memory for 500 and 200 while they are constant.``
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
do
{
cout << "Input another :\n";
cin >> x;
if (x == "bow")
{
return (coins - bow_cost); //return to function as integer
}
} while (true);
}
int main()
{
string name, i;
cout << "if you wish to visit the shop type \"shop\"\n";
cin >> i;
if (i == "shop")
{
cout << "Input :\n";
cin >> name;
cout << shop(name) << "you bought the bow.\n you now have " << " coins." << "\n";
}
//argument passed to shop funnction parameters.
system("pause");
return 0;
}
#包括“stdafx.h”
#包括
#包括
使用std::string;
使用std::cin;
使用std::cout;
#定义货币500;
#定义成本200;
int shop(字符串x)
{
//当500和200保持不变时,不需要为它们分配额外的内存``
我不能;
如果(i=“店铺”)
{
姓名;
我们可以考虑一下,if()
语句是单向的,它检查条件,如果条件满足,它会转到括号中,然后诸如此类,如果条件编译器有任何问题,它会通过if
并跳到编译其他代码
每次开始编译代码时,它都从int main()
函数开始。您在if
和else
语句中又犯了错误
这是正确的代码。我做了必要的更改
#include "stdafx.h"
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
#define coins 500 ;
#define bow_cost 200 ;
int shop(string x)
{
//There is no need to allocate extra memory for 500 and 200 while they are constant.``
cout << "welcome to the shop\n";
cout << "Bow(bow)costs 150 coins.\n";
do
{
cout << "Input another :\n";
cin >> x;
if (x == "bow")
{
return (coins - bow_cost); //return to function as integer
}
} while (true);
}
int main()
{
string name, i;
cout << "if you wish to visit the shop type \"shop\"\n";
cin >> i;
if (i == "shop")
{
cout << "Input :\n";
cin >> name;
cout << shop(name) << "you bought the bow.\n you now have " << " coins." << "\n";
}
//argument passed to shop funnction parameters.
system("pause");
return 0;
}
#包括“stdafx.h”
#包括
#包括
使用std::string;
使用std::cin;
使用std::cout;
#定义货币500;
#定义成本200;
int shop(字符串x)
{
//当500和200保持不变时,不需要为它们分配额外的内存``
我不能;
如果(i=“店铺”)
{
姓名;
现在每当我输入一个无效值,然后输入一个有效值,程序就会意外结束。现在每当我输入一个无效值,然后输入一个有效值,程序就会意外结束。