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;
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";
}
#include <iostream>
#include <cmath>
usingnamespace 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;
^
% 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:
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.