gpt4 book ai didi

c++ - 为什么这个 while 循环无论你输入什么都输出同样的东西?

转载 作者:行者123 更新时间:2023-11-27 23:56:46 28 4
gpt4 key购买 nike

我有一个程序可以做三件事。问你要多少个变量,让你输入每个变量,然后存入一个vector。我已经放置了一些代码来检查您的输入是否正确,如果不正确,则重新循环代码以询问您的变量。我遇到的问题是,当您在第二个变量周围键入任何内容时,它会要求您无限重试。

例如,如果我在输入中键入这些值:

Variable amount: 5
Please input variable 1: 8
Please input variable 2: 8

ERROR, PLEASE ENTER ONLY VALID SYMBOLS
---------------------

Please input variable 2:

无论您输入什么,它都会一遍又一遍地输出ERROR, PLEASE ENTER ONLY VALID SYMBOLS。代码在下面,如果您对此问题有更好的名称,请告诉我。 (我真的不知道该怎么调用它)

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>

using namespace std;

int inputErrorMessage()
{
cout << "\n ERROR, PLEASE ENTER ONLY VALID SYMBOLS \n";
cout << "--------------------- \n";

return 0;
}

int main()
{
// Declare the variables, vectors, etc.
int varNum = 1;
int totVar = 0;
int choice = 0;
vector<int> userNums;
double input = 0;
string checktotVar = "";
string checkInput = "";
string sym = "";
bool valid = false;
stringstream sstotVar;
stringstream ssinput;

if (choice != 6) {

while (!valid) {

valid = true;

// Ask user for how many variables they want then record it
cout << "Variable amount: ";
getline(cin, checktotVar);
sstotVar << checktotVar;
sstotVar >> totVar;

if (sstotVar.fail() || totVar <= 0) {
inputErrorMessage();
valid = false;
sstotVar.clear();
sstotVar.ignore();
}
}

valid = false;

while (!valid) {

valid = true;

// Ask the user for each variable, then record it into the array
for (int i = 0; i < totVar; ++i) {
cout << "Please input variable " << varNum << ": ";
getline(cin, checkInput);
ssinput << checkInput;
ssinput >> input;

if (ssinput.fail()) {
inputErrorMessage();
valid = false;
ssinput.clear();
ssinput.ignore();
}
if (valid == true) {
userNums.push_back(input);
varNum++;
}
}
}
}
}

最佳答案

ssinput >> input;

读取 ssinput 中的一件事直到流的末尾,同时保持读取有效。下一次

ssinput << checkInput;

无法写入流,因为流到达了流的末尾。这意味着读取也失败了,

if (ssinput.fail()) {

进入程序清除错误的if主体

ssinput.clear();

然后立即用

读取流的末尾
ssinput.ignore();

再次导致错误。

最快的解决方案:

重新创建

stringstream ssinput;

在每次循环迭代中。所以

stringstream sstotVar;
//stringstream ssinput; gone from here

    getline(cin, checkInput);
stringstream ssinput(checkInput); // and now tighter scope recreated each loop.
ssinput >> input;

此外,通过保持流而不清空流,它会变得非常……非常大。

你也可以简化你的逻辑

while (!valid) {

并通过将读取验证移动到它自己的函数中来消除一些重复的代码

int getMeANumber(const std::string & message, int min)

循环直到它得到一个数字然后返回那个数字。例如:

int getMeANumber(const std::string & message, int min)
{
while (true)
{
cout << message;
string checktotVar;
getline(cin, checktotVar);
stringstream sstotVar(checktotVar);
int totVar;
sstotVar >> totVar;

if (!sstotVar || totVar <= min)
{
inputErrorMessage();
}
else
{
return totVar;
}
}
}

现在 main 是这个小东西。

int main()
{
int choice = 0;
vector<int> userNums;

if (choice != 6)
{
int totVar = getMeANumber("Variable amount: ", 0);
for (int i = 0; i < totVar; ++i)
{
stringstream varname;
varname << "Please input variable " << i+1 << ": ";
userNums.push_back(getMeANumber(varname.str(), numeric_limits<int>::min()));
// numeric_limits<int>::min requires #include <limits>
}
}
}

关于c++ - 为什么这个 while 循环无论你输入什么都输出同样的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42150374/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com