C++ % Operator Problem

Started by
5 comments, last by Youbar 11 years, 6 months ago
Hi, I'm just starting out C++ and decided to have an attempt at making a calculator, probably very crude but it works. Anyway, I'm having a problem with the % operator. This is only a portion of my code, but you'll get the gist:

else if(response == 5) {
cout << "Remainder" << endl;
cout << "Enter first number: ";
cin >> fn;
cout << "Enter second number to divide and get remainder: ";
cin >> sn;
float ovrl = (fn % sn);
cout << ovrl << endl;
system("PAUSE");
goto startagain;
}


The code returns this error:
invalid operands of types `float' and `float' to binary `operator%'

Full code here:[spoiler]
#include <iostream>
using namespace std;
float fn = 0;
float sn = 0;
float response = 0;
int yn = 0;
int main() {
main:
system("CLS");
cout << "1. Division" << endl << "2. Multiplication" << endl << "3. Addition" << endl << "4. Subtraction" << endl << "5. Remainders" << endl;
cin >> response;
system("CLS");
if(response == 1) {
cout << "Divison" << endl;
cout << "Enter first number: ";
cin >> fn;
cout << "Enter second number to divide: ";
cin >> sn;
float ovrl = (fn / sn);
cout << ovrl << endl;
system("PAUSE");
goto startagain;
}

else if(response == 2) {
cout << "Multiplication" << endl;
cout << "Enter first number: ";
cin >> fn;
cout << "Enter second number to multiply: ";
cin >> sn;
float ovrl = (fn * sn);
cout << ovrl << endl;
system("PAUSE");
goto startagain;
}

else if(response == 3) {
cout << "Addition" << endl;
cout << "Enter first number: ";
cin >> fn;
cout << "Enter second number to add: ";
cin >> sn;
float ovrl = (fn + sn);
cout << ovrl << endl;
system("PAUSE");
goto startagain;
}

else if(response == 4) {
cout << "Subtraction" << endl;
cout << "Enter first number: ";
cin >> fn;
cout << "Enter second number to subtract: ";
cin >> sn;
float ovrl = (fn - sn);
cout << ovrl << endl;
system("PAUSE");
goto startagain;
}

else if(response == 5) {
cout << "Remainder" << endl;
cout << "Enter first number: ";
cin >> fn;
cout << "Enter second number to divide and get remainder: ";
cin >> sn;
float ovrl = (fn % sn);
cout << ovrl << endl;
system("PAUSE");
goto startagain;
}

else {
goto main;
}

startagain:
system("CLS");
cout << "Would you like to start again? (1 = Y / 2 = N)";
cin >> yn;
if(yn == 1) {
goto main;
}

else if(yn == 2) {
exit(0);
return 0;
}

else {
exit(0);
}
}

[/spoiler]
Advertisement
I believe the modulus operator (%) only works with integer types. You'll have to convert fn and sn to integer if you want to use it.

Or...a quick search on google and I stumbled upon "fmod". Used like so:

fmod(5.5, 3.5) // takes numerator and denominator

Note: I've never used this function before, so no promises. Though I believe you need to include the math header file to use fmod.
I don't understand the fmod function...but about the converting fn and sn to integers...wouldn't that remove the decimals and round it down?
e.g 1.2 % 5 would return 0.
fmod is simply a floating point version of the modulus operator. It takes two arguments (the numerator and denominator, in that order) and calculates the remainder like so:
remainder = numerator - quotient * denominator where quotient is an integer and the remainder is less than the denominator.
For example fmod(5.5,3.5) would return 2.
Could you give me an example of how I would apply it to my script please? It's returning errors.unsure.png
Thanks. smile.png
float ovrl = fmodf(fn, sn);



L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks, problem solved. laugh.png

This topic is closed to new replies.

Advertisement