モジュラス演算を実行しようとしています。モジュラスは整数でのみ機能するため、ユーザーに 2 つの数値を入力するように求めます。入力が整数かどうかを確認する while ループがあります。次に while ループは、ユーザーに 2 つの数字を再入力するように求めます。ただし、while ループは繰り返され続け、ユーザーが数字を再入力する機会を与えません。これを行うには何が適切でしょうか?
#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;
}