Invalid Operand?

Posts 1–4 of 4 · Page 1 of 1
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;

Code:
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";
Try this:
Code:
#include <iostream>
#include <cmath>

using namespace std;

void tellFortune(double, double);

int main()
{
  int 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(int numYears, int numChildren)
{
  auto numYears = abs(numYears) % 5;
 auto numChildren = abs(numChildren) % 6; 

 cout << "\nYou will be married in " << numYears << " years " << "and will have " << numChildren << " children.\n";
}
Quote Originally Posted by Hitokiri~ View Post
Try this:
Code:

void tellFortune(int numYears, int numChildren)
{
  auto numYears = abs(numYears) % 5;
 auto numChildren = abs(numChildren) % 6; 

 cout << "\nYou will be married in " << numYears << " years " << "and will have " << numChildren << " children.\n";
}
You're using the same name for the local variables as the parameters.
Quote Originally Posted by ~BlackMaster~ View Post
You're using the same name for the local variables as the parameters.
Didn't notice that. Just C&P'd.
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us