1

Input Validation:

The program should output an error and let the user retry if any if the numbers are negative. Only one iteration is required (i.e. you program does not need to prompt for another three numbers, it can exit after printing the result).

So I have the homework completed and the only thing im missing is this input validation as stated above. In other words (from my understating), I should be implementing a loop in my code where the user has to input another 3 set of numbers if they previously entered a negative number.

#include <iostream>
#include <iomanip>
using namespace std;

double getAverage (double num1, double num2, double num3){

    double averageAmount;

    averageAmount = (num1 + num2 + num3)/ 3;

    return averageAmount;
}
double getMedian (int num1, int num2, int num3){

       int x = num1-num2;
       int y = num2-num3;
       int z = num1-num3;
       if(x*y > 0) return num2;
       if(x*z > 0) return num3;
       return num1;
}
double countDigits( int num1){

    int digits = 0;

    while (num1){

        num1 = num1/10;
        digits++;
    }

    return (digits);
}

int main() {

    double num1 = 0;
    double num2 = 0;
    double num3 = 0;

    cout << "Enter 3 integers" << endl;
    cin >> num1;
    cin >> num2;
    cin >> num3;

    cout << "The average is " << fixed << setprecision(2) << getAverage(num1, num2, num3) << endl;
    cout << "The median is " << fixed << setprecision(2) << getMedian(num1, num2, num3) << endl;
    cout << "There are " << countDigits(num1) << " digits in the number " << num1 << endl;

    return 0;
}

If user enters an negative number I expect the output to be "Enter 3 integers" //since we want only positive numbers

7
  • 2
    If you want the user to input unsigned integer values, start by using unsigned integer types for the input. And do error checking (e.g. unsigned int num1, num2, num3; if (!(std::cin >> num1 >> num2 >> num3)) { /* error in input */ }) Commented Oct 25, 2019 at 6:20
  • Possible duplicate of integer input validation, how? Commented Oct 25, 2019 at 6:23
  • @Someprogrammerdude However, parsing -1 as unsigned int, might surpisingly produce a huge positive value instead of an error. Commented Oct 25, 2019 at 6:47
  • 1
    @moooeeeep Ah I know why that's happening... It's because input is parses as by std::strtoull (see this). And if the value is out of range (like e.g. a negative number) then it returns ULLONG_MAX (and errno is set to ERANGE). This needs to be taken into account when reading and validating. Commented Oct 25, 2019 at 7:03
  • 1
    @Someprogrammerdude Found a question already about this: stackoverflow.com/questions/7677158/… Commented Oct 25, 2019 at 7:21

3 Answers 3

1

you can write the code using a do-while loop:

do {
   cout << "Enter 3 integers" << endl;
   cin >> num1;
   cin >> num2;
   cin >> num3;
} while ((num1 < 0) || (num2 < 0) || (num3 < 0));

otherwise with just a while loop as:

while (true) {
   cout << "Enter 3 integers" << endl;
   cin >> num1;
   // if any number is -ve, continue the whole loop
   if (num1 < 0) continue;
   cin >> num2;
   if (num2 < 0) continue;
   cin >> num3;
   if (num3 < 0) continue;
   // if every number is positive, just break out of the loop
   break; 
}

Hope this helps, Thanks - Rajkumar

Sign up to request clarification or add additional context in comments.

Comments

0
cout << "Enter 3 integers" << endl;
        cin >> num1;
        cin >> num2;
        cin >> num3;

            while(num1 < 0 || num2 < 0 || num3 < 0)
            {
                if (num1 < 0)
                {
                    cout << "Your first number [" << num1 << "] is negetiv. Please enter a positive number!";
                    cin >> num1;
                }
                if (num2 < 0)
                {
                    cout << "Your second number [" << num2 << "] is negetiv. Please enter a positive number!";
                    cin >> num2;
                }
               if (num3 < 0)
                {
                    cout << "Your third number [" << num3 << "] is negetiv. Please enter apositive number!";
                    cin >> num3;
                }
            }

Don't forget to wait for a keypress by the user after printing the numbers by using:

cin;

or

system("pause");

Comments

-1

You can use for loop to input the numbers.

When user enters any negative number you can show an error message and exit the main function.

For example:

double num[3];

cout << "Enter 3 positive integers" << endl;
for(int i=0; i<3; i++) {
    cin >> num[i];
    if (num[i] < 0 ) {
        cout << "Only 3 positive integers are allowed";
        return 0;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.