• Advertisement
Sign in to follow this  

C++ % Operator Problem

This topic is 1924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[CODE]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;
}[/CODE]

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

Full code here:[spoiler][CODE]
#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);
}
}
[/CODE]
[/spoiler]

Share this post


Link to post
Share on other sites
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:
[CODE]
fmod(5.5, 3.5) // takes numerator and denominator
[/CODE]
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.

Share this post


Link to post
Share on other sites
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. Edited by Youbar

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Could you give me an example of how I would apply it to my script please? It's returning errors.[img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]
Thanks. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
Thanks, problem solved. [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement