如果用户输入无效,则循环
Looping if user input invalid
我想创建一个程序,当用户输入我没有定义的内容时,程序会再次提示他。
我用 if 语句做了,但它只循环了 1 次,不会再循环了。我尝试了循环,但每当输入为假时,它只会破坏条件并拒绝所有输入。在 C++ 中。
任何帮助都非常感谢。
#include <iostream>
#include <string>
using namespace std;
void xD(){string x;
do{cout << "Retryn";
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 shopn";
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;
}
问题出在此循环块中的条件上
void xD(){
string x;
do{
cout << "Retryn";
cin >> x;
}while(true);
}
while(true)
条件使其永远循环,无论输入如何。要解决此问题,您可以更改条件:
void xD(){
string x;
do{
cout << "Retryn";
cin >> x;
}while(x!="bow");
cout << "you bought the bow. and some other messages"<<endl;
}
这应该行得通。但是,这对我来说仍然太复杂了。这可以简化为下面的代码片段:
void shop(){
string x;
float coins = 500;
float bow_cost = 200;
cout << "welcome to the shopn";
cout << "Bow(bow)costs 150 coins.n";
cin >> x;
while (x!="bow"){
cout << "Retryn";
cin>>x;
}
cout << "you bought the bow.n you now have " <<coins - bow_cost << " coins." << endl; cin >> x;
}
而不是执行此方法(仅检查一次条件(:
if (x == "bow"){
cout << "you bought the bow.n you now have " <<coins - bow_cost << "
coins." << endl; cin >> x;
} else{
xD();
}
它实际上是对方法 xD(( 的递归invocation
你应该做一个边做循环,
例:
while (x.compare("bow") != 0)
{
cout << "sorry, wrong input, try again...";
cin >> x;
}
请注意使用比较方法而不是 == 运算符
在文档中有更多关于它的信息
您可以在此处使用 cin>> [您的输入对象] 的返回值来检查状态或 istream 的方法fail()
。一旦输入流无法解析全部或部分流,它就会失败并保持失败状态,直到您清除它。未解析的输入被保留(所以你可以尝试以不同的方式解析它?因此,如果您尝试再次>>相同类型的对象,则会遇到相同的失败。要忽略 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;
}
}
对于字符串,解析几乎总是成功的,但您需要一些比较您拥有的字符串的机制和允许的机制。尝试寻找使用std::find()
和一些包含允许选项的容器,例如以pair<int,string>
的形式,并在switch((语句中使用int index(或在你给它的函数中使用find_if
和switch()
(。
考虑到if()
语句是一条one_direction路,它会检查条件,如果条件满足,它会转到其括号并执行等等等等,如果条件编译器有任何问题,则传递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 shopn";
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;
}
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 函数中的"pragma omp parallel for"在另一个并行循环中调用函数时无效
- C++循环:如何让程序在用户输入无效输入时多次重复"error"消息
- C++,程序输出无效输入,但继续通过循环
- 循环中的延迟流无效
- 如果用户输入无效,则循环
- 如何在无效的数据类型输入后继续循环
- 在循环内删除文件时使提升recursive_directory_iterator无效
- 如果用户只按Enter或输入无效,则C 如何重新启动循环
- 在迭代向量的 for 循环中无效使用非静态数据成员
- 在基于范围的 for 循环中获取无效引用
- 无效输入后卡在循环中
- 通过宏替换展开循环无效
- 为无效字符创建循环
- 第二个无效响应在我的验证循环中被接受
- 无效的 int 输入卡在无限循环中
- C++ For 循环:二进制表达式的操作数无效
- 程序工作,但当输入无效时,它会继续循环 c++
- 如何停止由于输入C++无效而导致的无限循环
- 简单for循环的openMP错误:无效的控制谓词