Invalid Operand?

I'm working on this code which "Tells your future"
and despite my best efforts I cannot seem to fix this:
6_2.cpp: In function âvoid tellFortune(double, double)â:
6_2.cpp:19:35: error: invalid operands of types âdoubleâ and âintâ to binary âoperator%â
double numYears = abs(numYears) % 5;
^
6_2.cpp:20:41: error: invalid operands of types âdoubleâ and âintâ to binary âoperator%â
double numChildren = abs(numChildren) % 6;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
using namespace std;
void tellFortune(double, double);
int main()
{
double numYears, numChildren;
cout << "This program can tell your future. \n";
cout << "Enter two integers separated by a space: ";        
cin >> numYears >> numChildren;
tellFortune(numYears, numChildren);
return 0;
}
void tellFortune(double, double)
{
double numYears = abs(numYears) % 5;       
double numChildren = abs(numChildren) % 6; 
cout << "\nYou will be married in " << numYears << " years " << "and will have " << numChildren << " children.\n";  
}



Last edited on
Not sure if you are aware but you have to actually ask a question and show some code for anyone to help you.

Here is a link on how to apply code to your form question: http://www.cplusplus.com/articles/z13hAqkS/
Thanks for pointing that out Erudite
Last edited on
First your function definition and prototype are missing there formal parameters:
void tellFortune(double, double)
Here is a link to how to write and work with functions.
http://www.cplusplus.com/doc/tutorial/functions/

Here is what I can tell you to help. First you need formal parameters in your function. Thats variables with the datatype in the function parameters. The variable needs to be different from the variables your planning to apply to the function in the function call -- (The actual parameters) . Like you did on line 11. Line 11 looks right.

Now back to the function definition.
1
2
3
4
5
6
void tellFortune(double a, double b) // using pseudo code in place of the //variable a and b would be a smarter way to keep up with what your doing like you //did with the function call.
{
 a = abs(a) % 5;      //no need for declaring the variable again you //already declared the variable in the function header... 
b = abs(b) % 6; 
cout << "\nYou will be married in " << a << " years " << "and will have " << b << " children.\n";  
}
Hope that helps for a better understanding just refer to the link provided above.
Hopefully this is more correct,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
using namespace std;
void tellFortune(double a, double b);
int main()
{
double numYears, numChildren;
cout << "This program can tell your future. \n";
cout << "Enter two integers separated by a space: ";        
cin >> numYears >> numChildren;
tellFortune(numYears, numChildren);
return 0;
}
void tellFortune(double a, double b)
{
a = abs(a) % 5;       
b = abs(b) % 6; 
cout << "\nYou will be married in " << a << " years " << "and will have " << b << " children.\n";  
}

I'm still reciving these errors though:
1
2
3
4
5
6
7
6_2.cpp: In function âvoid tellFortune(double, double)â:
6_2.cpp:19:14: error: invalid operands of types âdoubleâ and âintâ to binary âoperator%â
 a = abs(a) % 5;       
              ^
6_2.cpp:20:14: error: invalid operands of types âdoubleâ and âintâ to binary âoperator%â
 b = abs(b) % 6; 
              ^

The link did help somewhat, but i'm still unsure
Last edited on
Oh, my bad, I completely looked right over it...

% is ONLY used with the integral datatype. So simply change the data type your using for all of the variable that are going to have the modulus applied to them. Also, I don't seem to see the need for doubles in this program anyway. On the other hand if using doubles is a requirement of some over zealous teacher or whatever you would then try either a static_cast or Type Conversion found here:

http://www.cplusplus.com/doc/tutorial/typecasting/
i can just switch it to intergrals, thanks a bunch! Ill click solved when it complies correctly.
Now taking a second look at what your program is suppose to do, I realize something ... You may want to second think the numbers your dividing the variable by, because the value is high and less likely to return a remainder... Hopefully you already know how modulus works...If not there's plenty of information out there to help...I lowered the numbers to 2 and was able to a more believable result.
Any luck?
Topic archived. No new replies allowed.