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

daoz1
  • 43
  • 1
  • 6
  • 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 */ }`) – Some programmer dude Oct 25 '19 at 06:20
  • Possible duplicate of [integer input validation, how?](https://stackoverflow.com/questions/13212043/integer-input-validation-how) – moooeeeep Oct 25 '19 at 06:23
  • @Someprogrammerdude However, parsing -1 as unsigned int, might surpisingly produce a huge positive value instead of an error. – moooeeeep Oct 25 '19 at 06:47
  • @Someprogrammerdude This is what I would think too, but [there seem to be bugs with gcc and clang on tio then.](https://tio.run/##XYsxDoQgFER7TzFhGy0stlXCSWwIfM1P8EsEqs2enVXsdrp588bFOG7O1fpicaF4guYj5ZPsbjqWjN2y9AM@Ha4USbwJedwLz43xij5lP02OBcaABzz1KBlaQ0V7puui7sKNLKKeL4VEf/ZqOZBvxrfW8f0D) – moooeeeep Oct 25 '19 at 06:53
  • 1
    @moooeeeep Ah I know why that's happening... It's because input is parses as by [`std::strtoull`](https://en.cppreference.com/w/cpp/string/byte/strtoul) (see [this](https://en.cppreference.com/w/cpp/locale/num_get/get#Stage_3:_conversion_and_storage)). 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. – Some programmer dude Oct 25 '19 at 07:03
  • But shouldn't we expect the `operator>>(unsigned int)` overload do this on its own? :] – moooeeeep Oct 25 '19 at 07:08
  • 1
    @Someprogrammerdude Found a question already about this: https://stackoverflow.com/questions/7677158/how-to-detect-negative-numbers-as-parsing-errors-when-reading-unsigned-integers?rq=1 – moooeeeep Oct 25 '19 at 07:21

3 Answers3

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

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");

Johri87
  • 76
  • 10
-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;
    }
}
Masudur Rahman
  • 1,585
  • 8
  • 16