You’re hitting a compiler error on this line:
cout << "The remainder is " << a % b << endl;
// ^^^^^^
In C++, the built-in modulus operator % only works on integral types (int, long, etc.). You declared both a and b as double, so a % b is illegal.
How to fix
-
If you really want the integer remainder, convert to integers:
cout << "The integer remainder is "
<< static_cast<long long>(a) % static_cast<long long>(b)
<< endl;
-
If you want the floating-point “remainder” (i.e. a − n·b where n = truncate(a/b)), use std::fmod from <cmath>:
#include <cmath>
// …
cout << "The floating-point remainder is "
<< std::fmod(a, b)
<< endl;
-
Always check for division by zero (or modulo by zero) before you divide:
if (b == 0.0) {
cerr << "Error: division/modulo by zero!" << endl;
return 1;
}
Complete example using fmod
#include <iostream>
#include <cmath> // for std::fmod
using namespace std;
int main() {
double a, b;
cout << "Enter two numbers: ";
if (!(cin >> a >> b)) {
cerr << "Invalid input!" << endl;
return 1;
}
if (b == 0.0) {
cerr << "Error: division by zero!" << endl;
return 1;
}
cout << "The sum is " << (a + b) << endl
<< "The difference is " << (a - b) << endl
<< "The product is " << (a * b) << endl
<< "The quotient is " << (a / b) << endl
<< "The remainder is " << std::fmod(a, b) << endl;
return 0;
}
That will compile and run, giving you a “remainder” for floating-point inputs.