What does" invalid operands of types `int' and `double' to binary `operator%' "

Hello, Im a complete newbie to c++, so i would appriciate any help. I keep getting these errors in my program.
37 invalid operands of types `int' and `double' to binary `operator%'
i dont understand the problem, all of my varibles are ints.

//this program calculates the number of quarters,
// dimes and nickles and pennies are nessasary to
//generate the number of cents imputed
#include <iostream.h>
#include<string.h>

int main()
{
int toBeChanged, numberOfDimes, numberOfQuarters, numberOfNickels, numberOfPennies;
cout << " I will show you how many quarters, dimes, nickels, and \npennies needed to make the amount. How much money do you have?" << '\n';
cin >> toBeChanged;

if(toBeChanged >= .25)
{
numberOfQuarters = (int) (toBeChanged / .25);
toBeChanged = (toBeChanged % .25);
}

if(toBeChanged >= .1)
{
numberOfDimes = (int) (toBeChanged / .1);
toBeChanged = (toBeChanged % .1);
}

if(toBeChanged >= .05)
{
numberOfNickels = (int) (toBeChanged / .05);
toBeChanged = (toBeChanged % .05);
}

if(toBeChanged >= .01)
{
numberOfPennies = (int) (toBeChanged / .01);
toBeChanged = toBeChanged % .01;
}

cout << "For "<< toBeChanged << " you will need:"<<'\n';
cout <<numberOfQuarters<< " quarters,"<<'\n';
cout << numberOfDimes << " dimes,"<< '\n';
cout << numberOfNickels << " nickels,"<< '\n';
cout << numberOfPennies << " pennies."<< '\n';

system("pause");
return 0;
}
variables: toBeChanged, numberOfDimes, numberOfQuarters, numberOfNickels, numberOfPennies;

should be float I think, cos you're losing floating points.
I could be wrong... I am still learning C++, but I believe the '%' or 'modulus' only works on values of an 'int' data type... I actually worked through a problem like this a bit ago... let me see if I can't find the file and see how I did this...
it looks like i used a 'const' or 'constants' in my code... and where you have a decimal to represent change, i used the constant variable identifier...

const int HALFDOLLAR = 50; // a constant

change = change % HALFDOLLAR; // my assignment statement

Last edited on
mjmckechnie is correct. The modulo operator needs an int (>0, unless your head just doesn't hurt as much as you want it to) as argument.
@ codekiddy
yea they started out as floats, then doubles. But my % inst working with either.
i did try to convert it to float and i get
invalid operands of types `float' and `double' to binary `operator%'

@ mjmckechnie
thank you, any help is very appriciated.
@ mjmckechnie
thank you, any help is very appriciated.


Does that mean you still need help?
Actually the message waas suppose to nbe sent after you said you'd look. but yes i still need some guidence.
ive tried making my varibles const int as so

numberOfQuarters = (int) (toBeChanged / .25);
toBeChanged = (const int)(toBeChanged % .25);

and im still etting the same errors.

invalid operands of types `int' and `double' to binary `operator%'
.25 is a double
Yes, but i cant use double with a %.
Yes that is your problem.

If you want to do mod on floating point numbers you can use std::fmod. I have not studied your code in detail so I don't know if this is what you want.
Last edited on
So then i guess my question becomes
what do i use instead of a mod operation that will work the same with doubles?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	//Header file
#include <iostream>

using namespace std;

	//Named constants
const int HALFDOLLAR = 50;
const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;

int main()
{
		//Declare variable
	int change;

	cout << "Enter change in cents: ";		
	cin >> change;							
	cout << endl;

	cout << "The change you entered is " << change << endl;		

	cout << "The number of half-dollars to be returned is " << change / HALFDOLLAR << endl;		

	change = change % HALFDOLLAR;		

	cout << "The number of quarters to be returned is " << change / QUARTER << endl;		

	change = change % QUARTER;		

	cout << "The number of dimes to be returned is " << change / DIME << endl;		

	change = change % DIME;		

	cout << "The number of nickels to be returned is " << change / NICKEL << endl;		

	change = change % NICKEL;		

	cout << "The number of pennies to be returned is " << change << endl;		

	cout << endl << "Please press enter to exit the program...";

	cin.get();

	return 0;
}


generally, I believe that in learning to program we are supposed to look at other peoples code, and that will help us to learn. At the same time, I don't know if it is frowned upon to give you the whole answer or not here at this forum. But, here it is, this is the way I did it...

you can also write the assignment expression "change = change % NICKEL" like this, change "change %= NICKEL". It is just a simplified way of doing the same thing.

Hope it helps and that you learn something from it... :)
Last edited on
Thank you mjmckechnie,
it does help to look at other similar codes. yes this seems much more of a simple way of writing this program.
i rewrote my program a bit, but it still is kinda freaking out.
but at least i dont have the errors anymore
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//this program calculates the number of quarters,
// dimes and nickles and pennies are nessasary to 
//generate the number of cents imputed
#include <iostream.h>
#include<string.h>

main() 
{
     double toBeChanged; 
     int numberOfDimes, numberOfQuarters, numberOfNickels, numberOfPennies;
     cout << " I will show you how many quarters, dimes, nickels, and \npennies needed to make the amount. \nHow much money do you have? (i.e. 1.25)" << '\n';
     cin >> toBeChanged;
     
cout << "For "<< toBeChanged << " you will need:"<<'\n';  
   
     toBeChanged = toBeChanged * 100;
          
      if(toBeChanged >= 25)
{
      numberOfQuarters =  (toBeChanged / 25);
      toBeChanged = (int)toBeChanged % 25;
}

      if(toBeChanged >= 10)
{
               numberOfDimes = (toBeChanged / 10);
               toBeChanged = (int)toBeChanged % 10;
}

               if(toBeChanged >= 5)
{
               numberOfNickels =  (toBeChanged / 5);
               toBeChanged = (int)toBeChanged % 5;
}

               if(toBeChanged >= 1)
{
               numberOfPennies = (toBeChanged  / 1);
               toBeChanged = (int)toBeChanged % 1;
}


cout << numberOfQuarters<< " quarters,"<<'\n';
cout << numberOfDimes << " dimes, "<< '\n';
cout << numberOfNickels << " nickels,"<< '\n';
cout << numberOfPennies << " pennies."<< '\n';

system("pause"); 
return 0;
}


so, it can get the quarters right, but then it messes up on the dimes and below.
any help?
Last edited on
So, if you do not initialize the variables and the change is say 785, it will only need the biggest value; which is quarters. But if you have 785.66, than all of the 'if' statements will work... this is why there is weird output... at least that is what I believe... I didn't spend to much time with it...

In other words, if the value of the input is a integer (say 785), then only the first 'if' statement executes...
and dimes, nickles, and pennies never get initialized...
Last edited on
Topic archived. No new replies allowed.