在C++中处理字符串错误时的错误处理

在C++中处理字符串错误时的错误处理

问题描述:

我的任务是在课堂室中取得测试成绩的学生,并返回最高,最低和平均值。我目前正在调试可能由用户输入的错误。当用户输入十进制测试分数值的字符时,我已成功解决问题。我遇到的问题是,如果用户输入“4k”程序,但将4作为有效输入,但在程序不应该批准作为有效输入的分数时仍会给出错误,但应该只有错误并提示只有数字值。在C++中处理字符串错误时的错误处理

我会提供了一个输出实例

继承人的代码段:

for (int i = 0; i < students; i++) 
     { 

     cout << "Please enter a score: " << flush; 
     cin >> array[i]; 
     do{ 

      if(cin.fail()) 
      { 
      cin.clear(); 
      cout<<"Error, that's not a decimal number. Please reenter: "; 
      std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      cin >> array[i]; 
      } 
     }while(cin.fail()); 

     } 

样本输出错误::

How many students are in class? 3 
Please enter 3 scores 
- - - - - - - - - - - 
Please enter a score: jkl 
Error, that's not a decimal number. Please reenter: jkl 
Error, that's not a decimal number. Please reenter: 4k 
Please enter a score: Error, that's not a decimal number. Please reenter: 0 
Please enter a score: 3 
4 
0 
3 
The worstest score in the class is: 0 
The bestest score in the class is: 4 
The average score is: 2.33333 
+0

您需要将输入读取为字符串并进行解析以确保其有效。 – NathanOliver

+0

Dat _“worstest”_/_“bestest”_语言:/ –

+0

@NathanOliver您可以向我展示一个例子吗?我是这个语言的新手。 – ProgrammingKid33

注:根据您的标签,我我会假设你正在使用C++ 11。如果不是这个答案不会工作。

使用你已经有了,试试下面的方法来获取用户输入:

#include <iostream> 
#include <string> 
#include <limits> 
#include <stdexcept> 

double getValidInput() 
{ 
    // Some variables for controlling how this works 
    std::string user_input ; // Used to store input 
    bool bad_input(true) ;  // Set to true when the input is good 
    double value(0.0) ;   // Value that will be returned 
    size_t pos(0) ;    // Tells us when all of user_input was useable 

    cout << "Please enter a score: " << flush ; 
    cin >> user_input; 

    // Now do the thing 
    while (bad_input) { 

     try { 
      // Note that this may return an invalid string->double conversion 
      value = std::stod(user_input, &pos) ; 
     } catch (std::invalid_argument & err) { 
      // The input isnt a number that can be converted 
      // to a double 
      pos = 0 ; 
     } 

     // If the entire string was converted then all is good! 
     if (pos == user_input.size()){ 
      bad_input = false ; 
     } else { 
      cout<<"Error, that's not a decimal number. Please reenter: "; 
      std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      cin >> user_input ; 
     } 
    } 

    return value ; 
} 

好了,是什么这一切呢?让我们开始

try { 
     // Note that this may return an invalid string->double conversion 
     value = std::stod(user_input, &pos) ; 
    } catch (std::invalid_argument & err) { 
     // The input isnt a number that can be converted 
     // to a double 
     pos = 0 ; 
    } 

用户提供了一个字符串,你想从它那里得到一些(我假设double类型)。上面的代码将使用try将用户的输入从string转换为double,使用std::stod()。请注意,我们还传递另一个变量pos,其中存储了可以转换为double的字符串的长度。

让检查您输入:

  • 在您第一次输入jkl直转换double不能完成的情况。因此std::stod将引发std::invalid_argument类型的异常。代码捕获并设置pos = 0。以下if语句指出输入字符串的长度比pos长,因此并非所有输入都能够被转换,并且用户必须再次尝试。
  • 在你的第二个输入,4k的情况下,std::stod可以将字符串转换为双高达的K,但在这一点上它停止。没有异常抛出,但pos现在比字符串小一个字符,并且用户再次尝试。现在

,你会插入这个到您的for循环,像这样:

for (int i = 0; i < students; i++) 
{ 
    array[i] = getValidInput() ; 
} 

希望帮助!

+0

请注意,如果这是一项家庭作业,可能有部分答案你还没有上课,所以如果你使用它们,你可能会得到较低的分数。 – Jvinniec

+0

如果一个老师因为阅读材料而对你进行惩罚,咧嘴笑,忍耐,并且再也不要在这堂课上做,但是要悄悄地让老师把手指放在背后,确保他们的教育风格尽快烧到地上因为你已经成功脱颖而出。我们越早得到这样的白痴,就越好。 – user4581301