0

Why Isn't My Code Working.

I'm trying to make a basic calculator using C++ with 6 operations but it isn't working: When I use double as a data type to store my numbers, I get an error. I also removed the exponent operator because of many other errors. Please help. Here is my code: int main() { //Declaring the variables I'm going to use in the program. double num1, num2; //I used doubles for more efficiency in the program. string op; //Storing the operator in the code cout << "Enter the first number:" << endl; cin >> num1; cout << "Enter the operator" << endl; cin >> op; cout << "Enter the second number:" << endl; cin >> num2; double res; //storing result of operation if (op == "+") { res = num1 + num2; cout << res << endl; } else if (op == "-") { res = num1 - num2; cout << res << endl; } else if (op == "/") { res = num1 / num2; cout << res << endl; } else if (op == "%") { res = num1 % num2; cout << res << endl; } return 0; } I get this error: ./Playground/file0.cpp: In function 'int main()': ./Playground/file0.cpp:38:20: error: invalid operands of types 'double' and 'double' to binary 'operator%' 38 | res = num1 % num2; | ~~~~ ^ ~~~~ | | | | double double

24th Jul 2023, 4:55 PM
Aaron Musisi
Aaron Musisi - avatar
2 ответов
+ 7
Aaron Musisi , https://code.sololearn.com/c1PUcs4mG5n1/?ref=app Don't post your code in description tag...add your code in code playground save it and post the link of that code playground here... For double you can't use modulo operator ...but we can use some in-built function to make it possible.... Like using fmod .... Try like this, I lil modified your code with in-built function....
24th Jul 2023, 5:51 PM
Riya
Riya - avatar
+ 5
Aaron Musisi You cant use modulo on doubles
24th Jul 2023, 5:33 PM
Junior
Junior - avatar