Logical Error: Change value inside void statement

Hi there, I've encountered a slight logical error in my code
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* 
 Lab06_pensionplans.cpp 
 Purpose : 
 - Create a simple financial application to calculate retirement plans 
*/ 
#include <iostream> 
#include <cstdlib> 
using namespace std; 
void displayMenu() 
 { 
	system("cls"); 
	cout << "+-------------------------------------+" << endl; 
	cout << "| Golden Years Financial Inc.         |" << endl; 
	cout << "|=====================================|" << endl; 
	cout << "| Select:                             |" << endl; 
	cout << "| 1 => Display Data                   |" << endl; 
	cout << "| 2 => Change Data                    |" << endl; 
	cout << "|-------------------------------------|" << endl; 
	cout << "| 3 => One Lump Sum Withdrawal Plan   |" << endl; 
	cout << "| 4 => Yearly Withdrawal Plan         |" << endl; 
	cout << "| 5 => Comparison of Both Plans       |" << endl; 
	cout << "|-------------------------------------|" << endl; 
	cout << "| Q => Quit                           |" << endl; 
	cout << "+-------------------------------------+" << endl; 
	cout << endl; 
	cout << "Choice => ";
 } 
/*#1*/ void displayData(int startingAge, int numOfYears, 
double lumpSumAmount, double yearlyAmount, 
double interestRate ) 
 { 
	cout << endl; 
	cout << "Starting Age = " << startingAge << endl; 
	cout << "Nunber of Years = " << numOfYears << endl; 
	cout << "Lump Sum Amount = " << lumpSumAmount << endl; 
	cout << "Yearly Amount = " << yearlyAmount << endl; 
	cout << "Interest Rate (%) = " << interestRate << endl; 
	cout << endl; 
	system("pause"); 
 } 
/*#2*/ void changeData(int startingAge, int numOfYears, 
double lumpSumAmount, double yearlyAmount, 
double interestRate ) 
 { 
	cout << endl; 
	cout << "Starting Age = "; 
	cin >> startingAge; 
	cout << "Nunber of Years = "; 
	cin >> numOfYears; 
	cout << "Lump Sum Amount = "; 
	cin >> lumpSumAmount; 
	cout << "Yearly Amount = "; 
	cin >> yearlyAmount; 
	cout << "Interest Rate (%) = "; 
	cin >> interestRate; 
 } 
int main() 
 { 
	int startingAge = 55; 
	int numOfYears = 20; 
	double lumpSumAmount = 400000; 
	double yearlyAmount = 25000; 
	double interestRate = 5; 
	char choice; 
	bool done = false; 
	do 
	 { 
		displayMenu(); 
		cin >> choice; 
		choice = toupper(choice);
		switch (choice) 
		{ 
			case '1' : displayData(startingAge, numOfYears,
 lumpSumAmount, yearlyAmount, interestRate); 
			break; 
			case '2' : changeData(startingAge, numOfYears,
  lumpSumAmount, yearlyAmount,  interestRate );
			system("pause"); 
			break; 
			case '3' : cout << "Not yet implement" << endl; 
			system("pause"); 
			break; 
			case '4' : cout << "Not yet implement" << endl; 
			system("pause"); 
			break; 
			case '5' : cout << "Not yet implement" << endl; 
			system("pause"); 
			break; 
			case 'Q' : done = true; 
			break; 
			default : cout << "Invalid selection, try again!" << endl; 
			system("pause"); 
			break; 
		} 
	 } while (!done); 
	cout << "Good Bye!" << endl; 
	cout << endl; 
 } 
 


Look at case 2, which the user supposed to key in a new input, the problem is the value will never got into main function, I don't know what should I modify with the functions

UPDATE:
Figured out I need to change the
1
2
void changeData(int startingAge, int numOfYears, 
double lumpSumAmount, double yearlyAmount, double interestRate ) 


into
1
2
void changeData(int &startingAge, int &numOfYears, 
double &lumpSumAmount, double &yearlyAmount, double &interestRate )  


:D
Last edited on
When you pass variables into your function, you are passing *copies* (different than the ones in your main function), so when the function ends, these copies are destroyed. Look up "pass by reference" for how to do what you want to do.
Hey thanks! :D
I only done the question thanks to google, now I know the exact term to use for this type of question and understand the problem
Topic archived. No new replies allowed.