2

I am trying to do a modulus operation. I ask the user to input two numbers, since modulus only works with integers, I have a while loop that checks if the inputs are integers. Then the while loop ask the user to re-enter the two numbers. But the while loop keeps on repeating and does not allow the user a chance to re-enter the numbers. What will be the proper to go about doing this?


#include <iostream>
using namespace std;

int Modulus (int, int,struct Calculator);

struct Calculator
{
    int per_numb1, per_numb2;
    int per_Result; };

int main () 
{ 
    Calculator Operation1;

    cout << "\nPlease enter the first number to calculate as a modulus: "; 
    cin >> Operation1.per_numb1; 

    cout << "\nPlease enter the second number to calculate modulus: "; 
    cin >> Operation1.per_numb2; 

while ( !( cin >> Operation1.per_numb1)  ||   !( cin >> Operation1.per_numb2))
{ 

        cout << "\nERROR\nInvalid operation \nThe first number or second number   must be an integer"; 
        cout << "\n\nPlease re-enter the first number to begin Modulus: "; 
        cin >> Operation1.per_numb1;  

        cout << "\nPlease re-enter the second number to begin Modulus: ";
        cin >> Operation1.per_numb2;
}





Operation1.per_Result = Modulus(Operation1.per_numb1, Operation1.per_numb2, Operation1); 

cout << "\nThe result  is: " << Operation1.per_Result << endl;

}

int Modulus (int n1, int n2, struct Calculator)
{
    int Answer; 

    Answer = n1 % n2; 

    return Answer; 
} 
Mysticial
  • 464,885
  • 45
  • 335
  • 332
user2203675
  • 23
  • 1
  • 3

2 Answers2

3

Refactor to something like this:

 #include <iostream>
 #include <string>
 #include <limits>

 using namespace std;

 class Calculator
 {
 public:
     static int Modulus (int n1, int n2);
 };

 int Calculator::Modulus (int n1, int n2)
 {
     return n1 % n2; 
 }

 int getInt(string msg)
 {
     int aa;

     cout << msg;
     cin >> aa;
     while (cin.fail())
     {
         cin.clear();
         cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
         cerr <<  "Input was not an integer!" << endl;
         cout << msg;
         cin >> aa;
     } 
     return aa;
 }

 int main () 
 { 
     int num1 = getInt("Enter first value: ");
     int num2 = getInt("Enter second value: ");
     int value = Calculator::Modulus(num1,num2);
     cout << "Answer:" << value << endl ;
 }
John Jesus
  • 2,284
  • 16
  • 18
  • I haven't learned about classes yet. What specifics can I just change in the original program that I have for it to function properly? – user2203675 Mar 24 '13 at 03:35
  • Don't use your while loop. Instead, use a function like the `getInt()` I provided. Notice how it checks for the error using `cin.fail()` and how it clears the input stream and the error using `cin.clear()` and `cin.ignore()`. – John Jesus Mar 24 '13 at 03:49
  • Thank You. I used the cin.ignore(std::numeric_limits::max(),'\n'); – user2203675 Mar 24 '13 at 03:54
2

When the input parsing fails, invalid input data will remain in the stream. You need to

  1. clear the stream error state by calling cin.clear().
  2. and skip the remaining invalid input.

See the answer to this question.

Community
  • 1
  • 1
Alex B
  • 82,554
  • 44
  • 203
  • 280