Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何重构逻辑以更好地处理错误? 我为我的入门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; } #包括 使用名称空间std; 先浮; 浮动秒; 浮动第三; 浮动第四; 浮动第五; 浮动总额; int main(){ //提示用户输入5个十进制值 cout>第一>>第二>>第三>>第四>>第五; //清除并放弃输入错误 if(cin.fail()){ cout_C++_Validation_Input_Refactoring - Fatal编程技术网 第一>>第二>>第三>>第四>>第五; //清除并放弃输入错误 if(cin.fail()){ cout,c++,validation,input,refactoring,C++,Validation,Input,Refactoring" /> 第一>>第二>>第三>>第四>>第五; //清除并放弃输入错误 if(cin.fail()){ cout,c++,validation,input,refactoring,C++,Validation,Input,Refactoring" />

如何重构逻辑以更好地处理错误? 我为我的入门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; } #包括 使用名称空间std; 先浮; 浮动秒; 浮动第三; 浮动第四; 浮动第五; 浮动总额; int main(){ //提示用户输入5个十进制值 cout>第一>>第二>>第三>>第四>>第五; //清除并放弃输入错误 if(cin.fail()){ cout

如何重构逻辑以更好地处理错误? 我为我的入门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; } #包括 使用名称空间std; 先浮; 浮动秒; 浮动第三; 浮动第四; 浮动第五; 浮动总额; int main(){ //提示用户输入5个十进制值 cout>第一>>第二>>第三>>第四>>第五; //清除并放弃输入错误 if(cin.fail()){ cout,c++,validation,input,refactoring,C++,Validation,Input,Refactoring,您的评论已经走上了正确的轨道: 我觉得也许有一种方法可以通过数组和do…while循环来实现这一点 您可以通过在输入周围使用循环来实现这一点。这意味着您不断要求输入,直到他们给您有效的输入 为此,我在用户输入周围放置了一个循环,然后在开始时添加了一些在输入之后清理的代码。这意味着在它请求输入之前,它会首先清理所有内容,并且每次循环时都会这样做 一个可能的解决办法是: #include <iostream> using namespace std; float first; floa

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

我觉得也许有一种方法可以通过数组和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;
}
#包括
使用名称空间std;
先浮;
浮动秒;
浮动第三;
浮动第四;
浮动第五;
浮动总额;
int main(){
做{
//清除并放弃输入错误
cin.clear();
cin.忽略(10000,“\n”);
//提示用户输入5个十进制值
cout>第一>>第二>>第三>>第四>>第五;
}而(cin.fail());
//将这些值相加
总计=第一+第二+第三+第四+第五;
//转换为最接近的整数并打印结果

cout对于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;
#包括
#包括
使用名称空间std;
浮点输入;
浮动总额;
int main(){
//提示用户输入5个十进制值
int valuesEntered=0;
而(输入的值小于5)
{

你甚至不需要数组吗。