1

I wrote this short console program for my introductory C++ class, and technically it functions properly and I have met all the criteria. However, I dislike that the console window closes after a failed input, and would like to learn how I could refactor this program so that failed input instead prompts for new, correct input, continuing from where the user left off. I feel like maybe there's a way to do this with an array and a do...while loop, but my experiments have failed. I apologize if I'm not being very clear, I'm a total beginner.

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

By the way, I'm aware that using std is considered bad practice; however, it is part of the requirements for the assignment, so I left it in.

2 Answers2

0

You were already on the right tracks with your comment:

I feel like maybe there's a way to do this with an array and a do...while loop

You can do this by using a loop around your input. This would mean that you keep asking for input until they give you input that is valid.

To do this I put a loop around the user input and then added in some of the code that cleans up after the input at the start. This means that before it asks for input it cleans everything out first and does the same for every time it goes around the loop.

A possible solution would be:

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

It seems like the class you take follows Google Code University's C++ tutorial as mentioned in this Stack Overflow post. Take a look at those resources for more improvements to your code.

Community
  • 1
  • 1
Jamie
  • 927
  • 1
  • 7
  • 18
0

With a while loop you do not even need to use five variables like this:

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

}

Marco
  • 1,952
  • 1
  • 17
  • 23