I need help with step 3 If done correctly the program should
I need help with step 3. If done correctly, the program should repeat the same answer that is under step 2.
1 // Lab 6 fortunes.cpp
2 // This fortune telling program will be modified to use a void function.
3 // PUT YOUR NAME HERE.
4 #include <iostream>
5 #include <cmath>
6 using namespace std;
7
8 // Function prototype
9 // WRITE A PROTOTYPE FOR THE tellFortune FUNCTION HERE.
10
11 /***** main *****/
12 int main()
13 {
14 int numYears,
15 numChildren;
16
17 cout << \"This program can tell your future. \ \"
18 << \"Enter two integers separated by a space: \";
19
20 cin >> numYears >> numChildren;
21
22 numYears = abs(numYears) % 5; // Convert to a positive integer 0 to 4
23 numChildren = abs(numChildren) % 6; // Convert to a positive integer 0 to 5
24
25 cout << \"\ You will be married in \" << numYears << \" years \"
26 << \"and will have \" << numChildren << \" children.\ \";
27
28 return 0;
29 }
30
31 /***** tellFortune *****/
32 // WRITE THE tellFortune FUNCTION HEADER HERE.
33 // WRITE THE BODY OF THE tellFortune FUNCTION HERE.
Step 2: Run the program to see how it works. What output do you get when you input the following values at the prompt? -99 14
You will be married in 4 years and will have 2 children.
Step 3: Create a function that contains the fortune telling part of the code by doing the following:
On line 9 write the prototype for a void function named tellFortune that has two integer parameters.
On line 32 write the function header for the tellFortune function. Following that should be the body of the function. Move lines 22 – 26 of the program to the function body.
Replace current lines 22 – 26 of main with a call to the tellFortune function that passes it two arguments, numYears and numChildren.
Step 4: Recompile and rerun the program. Enter -99 and 14 again. It should work the same as before.
Solution
#include <iostream>
#include <cmath>
using namespace std;
// Function prototype
// WRITE A PROTOTYPE FOR THE tellFortune FUNCTION HERE.
void tellFortune(int numYears, int numChildren);
/***** main *****/
int main()
{
int numYears,
numChildren;
cout << \"This program can tell your future. \ \"
<< \"Enter two integers separated by a space: \";
cin >> numYears >> numChildren;
tellFortune(numYears, numChildren);
return 0;
}
void tellFortune(int numYears, int numChildren){
numYears = abs(numYears) % 5; // Convert to a positive integer 0 to 4
numChildren = abs(numChildren) % 6; // Convert to a positive integer 0 to 5
cout << \"\ You will be married in \" << numYears << \" years \"
<< \"and will have \" << numChildren << \" children.\ \";
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
This program can tell your future.
Enter two integers separated by a space: -99 14
You will be married in 4 years and will have 2 children.

