Help with void functions

I'm writing this program for class but I got stuck. I keep getting an error saying uninitialized local variables (line 25) in main. Should I initialize these in the void? I tried initializing them at 0 in my variable declarations. When I ran the program and typed the input, the calculations were all messed up. Sorry still kinda new at this. Hope someone can clarify for me on how to fix this.

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
 #include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants

// Function Prototypes
void openMessage ();
void getPrices (double currPrice, double prevPrice);
void calcInflaRate (double currPrice, double prevPrice, double& rate);
void calcEstPrice (double currPrice, double rate, double& estPrice1, double& estPrice2);
void printResults (double rate, double estPrice1, double estPrice2);
int main()
{
	// Declare variables below here

	double currPrice, prevPrice, rate, estPrice1, estPrice2;
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here
	openMessage ();
	getPrices (currPrice, prevPrice); //This is where the error is.
	{
		calcInflaRate (currPrice, prevPrice, rate);
		calcEstPrice (currPrice, rate, estPrice1, estPrice2);
		printResults (rate, estPrice1, estPrice2);
		getPrices (currPrice, prevPrice);
	}while (currPrice != 0);

	return 0;
}

// function definitions below here

void openMessage ()
{
	cout<<"***************Inflation Rate Calculation Program***************"<<endl;
}

void getPrices (double currPrice, double prevPrice)
{

	cout<<"Please enter the current price of the item >> ";
	cin>>currPrice;
	cout<<endl;
	cout<<"Please enter the price of the item one year ago >> ";
	cin>>prevPrice;
	cout<<endl;
}


void calcInflaRate (double currPrice, double prevPrice, double& rate)
{
	
	rate = (currPrice - prevPrice)/prevPrice;

}

void calcEstPrice (double currPrice, double rate, double& estPrice1, double& estPrice2)
{

	estPrice1 = currPrice * rate;
	estPrice2 = estPrice1 * rate;

}

void printResults (double rate, double estPrice1, double estPrice2)
{
	cout<<"The estimated inflation rate is "<<rate<<endl;
	cout<<"The estimated price next year is "<<estPrice1<<endl;
	cout<<"The estimated price in two years is "<<estPrice2<<endl;

}
Last edited on
If you want to modify a variable that exists in another scope from a function you need to pass it by reference, otherwise you're just modifying copies that will go out of scope once the function ends.
void getPrices(double &currPrice, double &prevPrice)
Topic archived. No new replies allowed.