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;
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
@ 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.
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
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.
//Header file
#include <iostream>
usingnamespace std;
//Named constants
constint HALFDOLLAR = 50;
constint QUARTER = 25;
constint DIME = 10;
constint 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... :)
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
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...