Help with Functions

I am trying to figure this program out. First off, I am new to C++. This is my third week in this class. I am having trouble though. This is suppose to ask for age, amount of cash, number of dependents, and how much you owe. Then it is suppose to print out If your rich by having assets over 1000000 and if it is under that it is suppose to say your not rich. I can not get my isRich() function to take the values inputted from the calcWealth function. My program works but right now it is always passing a true value to the main function. Can anyone give me some advise? here is my code.


#include <iostream>
#include <iomanip>
using namespace std;



// this is a function that asks for user input about thier age, how much cash they have, how many dependents they have and how much debt have.

void calcWealth(int age, int dependent, long cash, long owe)
{
// these 4 statements are were the age, amount of cash, dependents and debt is inputted


cout << "Please enter your age: ";
cin >> age;
cout << "Please enter the amount of cash on hand: ";
cin >> cash;
cout << "Please enter the number of dependents: ";
cin >> dependent;
cout << "Please enter the amount of money you owe: ";
cin >> owe;

} // end calcWealth


// This is where all the calculations are made. it takes the input from calcWealth function and subtracts the cash on hand from the amount that is owed. It then takes the age that is inputted and compares it. When the age is found it will take the amount of dependents and multiply it by a specific number and then that will get subtracted to the amount of leftover cash on hand. If the value aof cash on hans is 1 million or over it returns true. If it is lower then it will return false.

bool isRich()
{
long value;
long x = 0;
int age; int dependent; long cash; long owe;


calcWealth(age, dependent, cash, owe);

cash = cash - owe; //subtracting cash from the amount owed
cout << cash;

if(age < 40) // if age is less than 40 it does these steps
x = dependent * 150000; // multiplies the amount of dependents by 150000
value = cash - x; // subtracts the answer of previous from the amount of cash and set that as new value

if(age >= 40 && age <=50) // if age is greater than or equal to 40 and less than or equal to 50 it does these steps
x = dependent * 75000; // multiplies the amount of dependents by 75000
value = cash - x; // subtracts the answer of previous from the amount of cash and set that as new value

if(age > 50) // if age is greater than 50 it does these steps
x = dependent * 25000; // multiplies the amount of dependents by 25000
value = cash - x; // subtracts the answer of previous from the amount of cash and set that as new value


if (value >= 1000000) //this will take value and compare to see if it is greater than or equal to 1 million. if it is it will return a true value to value
return true;
else // if value is less than 1 million it will return a false value to value
return false;


} // end bool


int main()
{


// this calls the isRich() function which in turn runs the whole program
//call from the previous function
isRich();
// if value is true it prints out first statement. if it is false it prints out second statement
if(true)
cout << "Congratulations. You are actually rich! " << endl;
else
cout << "Sorry. You are not rich.";


} // end main
Last edited on
Hi!

I believe you have two problems:

The first:

void calcWealth(int age, int dependent, long cash, long owe)

You ask the computer to store the value of four variables: age, dependent, cash and owe. But when you go out of the function the values are lost because these are local variables.

There are many ways to solve this problem. One of them is declaring these variables as global (outside the function)

The second:

This is how the IF statement works:

if(test)
{
//If test is true do this
}// If test is false continue from here

If you replace test by TRUE, you will always do what is highlighted in bold.

Here is a suggestion, please test it:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>
using namespace std;
//These four variables are available for all the functions:
int age; int dependent; long cash; long owe;


// this is a function that asks for user input about thier age, how much cash they have, how many dependents they have and how much debt have.

void calcWealth()
{
// these 4 statements are were the age, amount of cash, dependents and debt is inputted


cout << "Please enter your age: ";
cin >> age;
cout << "Please enter the amount of cash on hand: ";
cin >> cash;
cout << "Please enter the number of dependents: ";
cin >> dependent;
cout << "Please enter the amount of money you owe: ";
cin >> owe;

} // end calcWealth


// This is where all the calculations are made. it takes the input from calcWealth function and subtracts the cash on hand from the amount that is owed. It then takes the age that is inputted and compares it. When the age is found it will take the amount of dependents and multiply it by a specific number and then that will get subtracted to the amount of leftover cash on hand. If the value aof cash on hans is 1 million or over it returns true. If it is lower then it will return false.

bool isRich()
{
long value;
long x = 0;



calcWealth();

cash = cash - owe; //subtracting cash from the amount owed
cout << cash;

if(age < 40) // if age is less than 40 it does these steps
x = dependent * 150000; // multiplies the amount of dependents by 150000
value = cash - x; // subtracts the answer of previous from the amount of cash and set that as new value

if(age >= 40 && age <=50) // if age is greater than or equal to 40 and less than or equal to 50 it does these steps
x = dependent * 75000; // multiplies the amount of dependents by 75000
value = cash - x; // subtracts the answer of previous from the amount of cash and set that as new value

if(age > 50) // if age is greater than 50 it does these steps
x = dependent * 25000; // multiplies the amount of dependents by 25000
value = cash - x; // subtracts the answer of previous from the amount of cash and set that as new value


if (value >= 1000000) //this will take value and compare to see if it is greater than or equal to 1 million. if it is it will return a true value to value
return true;
else // if value is less than 1 million it will return a false value to value
return false;


} // end bool


int main()
{

bool test;
// this calls the isRich() function which in turn runs the whole program
//call from the previous function
test = isRich();
// if value is true it prints out first statement. if it is false it prints out second statement
if(test)
cout << "Congratulations. You are actually rich! " << endl;
else
cout << "Sorry. You are not rich.";


} // end main 
Last edited on
Remember to format your code with code tags.
But the most obvious error is:
if(true) //it should be: if(isRich() == true)
How do you format the code with code tags? Also, unfortunately I can not use global variables. My instructor says it makes codes look sloppy. Is there anyway to get the values from calcWealth into the isRich? I probably should have mentioned about the global variables. I am sorry. But I appreciate your help.
Thank you so much. I just figured out how to use the overloaded function and the & to pass the reference values. My programs works like it should. Again. Thank you for your help. I really appreciate it.
Topic archived. No new replies allowed.