【问题标题】:How to refactor logic for better error handling?如何重构逻辑以更好地处理错误?
【发布时间】:2015-02-10 21:01:28
【问题描述】:

我为我的 C++ 入门课程编写了这个简短的控制台程序,从技术上讲,它可以正常运行,并且我已满足所有标准。但是,我不喜欢控制台窗口在输入失败后关闭,并且想了解如何重构这个程序,以便失败的输入改为提示新的、正确的输入,从用户离开的地方继续。我觉得也许有一种方法可以使用数组和do...while 循环来做到这一点,但我的实验失败了。如果我不是很清楚,我很抱歉,我是一个完全的初学者。

#include <iostream>
using namespace std;

float first;
float second;
float third;
float fourth;
float fifth;
float total;

int main(){

    // Promt the user to enter 5 decimal values
    cout << "Enter 5 decimal values: ";
    cin >> first >> second >> third >> fourth >> fifth;

    // Clear and discard input errors
    if (cin.fail()) {
        cout << "Invalid entry; Please enter numbers only." << endl;
        cin.clear();
        cin.ignore(10000, '\n');
    }
    else {
        // Add the values together
        total = first + second + third + fourth + fifth;

        // Convert to the nearest integer and print the result
        cout << fixed << setprecision(0) << "The total is: " << total << endl;
    }

    system("pause");
    return 0;
}

顺便说一句,我知道using std 被认为是不好的做法;但是,这是作业要求的一部分,所以我把它留在了。

【问题讨论】:

  • 你甚至不需要数组。

标签: c++ validation input refactoring


【解决方案1】:

您的评论已经走在正确的轨道上:

我觉得也许有办法用数组和 do...while 循环来做到这一点

您可以通过在输入周围使用循环来做到这一点。这将意味着您一直要求输入,直到他们为您提供有效的输入

为此,我在用户输入周围放置了一个循环,然后在开始时添加了一些在输入之后进行清理的代码。这意味着在它要求输入之前,它会先清除所有内容,并且每次循环时都会这样做。

一个可能的解决方案是:

#include <iostream>
using namespace std;

float first;
float second;
float third;
float fourth;
float fifth;
float total;

int main(){

    do {
        // Clear and discard input errors
        cin.clear();
        cin.ignore(10000, '\n');

        // Prompt the user to enter 5 decimal values
        cout << "Enter 5 decimal values: ";
        cin >> first >> second >> third >> fourth >> fifth;
    } while (cin.fail());

    // Add the values together
    total = first + second + third + fourth + fifth;

    // Convert to the nearest integer and print the result
    cout << fixed << setprecision(0) << "The total is: " << total << endl;

    system("pause");
    return 0;
}

您参加的课程似乎遵循Google Code University's C++ tutorial 中提到的Stack Overflow post。查看这些资源以进一步改进您的代码。

【讨论】:

    【解决方案2】:

    使用 while 循环,您甚至不需要像这样使用五个变量:

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    float input;
    float total;
    
    
    int main(){
    
    // Promt the user to enter 5 decimal values
    int valuesEntered = 0;
    while (valuesEntered < 5)
    {
        cout << "please enter " << (5 - (valuesEntered)) << " numbers: ";
        cin >> input;
        if (cin.fail()) {
            cout << "Invalid entry; Please enter numbers only." << endl;
            cin.clear();
            cin.ignore(10000, '\n');
        }
        else
        {
            total += input;
            valuesEntered++;
        }
    }
    cout << fixed << setprecision(0) << "The total is: " << total << endl;
    
    system("pause");
    return 0;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2012-11-15
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多