2017-02-10 26 views
1

我有一个程序可以做三件事。询问你想要多少个变量,要求你输入每个变量,然后将其存储在一个向量中。我已经放了一些代码来检查您的输入是否正确,如果不正确,请重新循环代码以询问您的变量。我遇到的问题是,当你在第二个变量周围输入任何东西时,它会要求你无限次地尝试。无论你输入什么内容,为什么while循环都输出相同的内容?

举例来说,如果我输入这些值输入:

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++; 
       } 
      } 
     } 
    } 
} 
+0

我建议你学习如何使用你的调试器。请在将来除了解决这个问题之外,还会帮助你。 – Fureeish

+0

另外,请注意,您不需要在函数的开头部分声明所有变量。代码通常更容易阅读和理解变量声明接近它们的使用位置。 –

回答

1
ssinput >> input; 

读取ssinput一件事权流的结束而离开读取有效。下一次

ssinput << checkInput; 

无法写入流,因为流遇到流的结尾。这意味着读出也将失败和

if (ssinput.fail()) { 

进入其中程序清除错误

ssinput.clear(); 

然后及时读取的料流与

ssinput.ignore(); 
结束时 if的主体

重新造成错误。

最快溶液:

重新创建

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这是itty-bitty tiny lil的东西。

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> 
     } 
    } 
} 
1

下面是这段代码的问题。

在这一部分:

if (valid == true) { 
    userNums.push_back(input); 
    varNum++; 
} 

你忘了添加ssinput.clear()。这将重置流状态(清除错误标志),否则您将无法再使用它。这就是为什么它停止在第二个输入工作。

此外,即使这个方法有效,您也会将您声明为double的变量推回到整数的向量中。如果这是为了存储双变量而不是截断它们并将它们存储为整数,那么这肯定会导致问题。

1

它应该是:

#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; 

       } 
       if (valid == true) { 
        userNums.push_back(input); 
        varNum++; 
       } 

       ssinput.clear(); 
      } 
     } 
    } 
} 

编辑:你需要清除循环的每个迭代stringstream的,否则,当你抓住下一个你不写一个空流来自用户的输入,这是导致.fail()方法在循环的第一次迭代之后返回true的原因。

+0

添加说明 – Dmihawk

相关问题