C4700 uninitialized local variable 'age' used

Currently working on a hw assignment and I thought I fixed all the errors but I guess I'm missing one. I added comments to the changes I've made so far. The assignment states the errors are only 1 character so we are either adding or removing something.

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void AskForInfoRefs(string &rName, int &rAge);
void AskForInfoPoints(string *pName, int *pAge);
string WriteInfo(string name, int age);

int main()
{
	string name, infoRefs, infoPoints;
	int *age;  

	AskForInfoRefs(name, *age); //added asterisk

	infoRefs = WriteInfo(name, *age); //added asterisk

	AskForInfoPoints(&name, age);

	infoPoints = WriteInfo(name, *age); //added asterisk

	cout << "\nThis is the info from the references:\n"
		<< &infoRefs << endl;

	cout << "\nThis is the info from the pointers:\n"
		<< infoPoints << endl;

	cin.get();
	return 0;
}

void AskForInfoRefs(string &rName, int &rAge)
{
	cout << "Please enter someone's name: ";
	getline(cin, rName);

	cout << "Please enter someone's age: ";
	cin >> rAge; //removed asterisk
	cin.ignore();
}

void AskForInfoPoints(string *pName, int *pAge)
{
	cout << "Please enter someone else's name: ";
	getline (cin, *pName);

	cout << "Please enter someone else's age: ";
	cin >> *pAge; 
	cin.ignore();
}

string WriteInfo(string name, int age)
{
	stringstream ss;

	ss << "Hi, " << name << "! I wish I were " << age << "!\n";

	return ss.str();
}
The error I'm getting is on line 13 but can't figure out why.
Hello hjabba89,

uninitialized local variable 'age' used
. When you learn to better understand the error messages this actually has your answer.

Based on your posted code the error is on line 16 not 13. You define int *age;, but at this point the only value it has is garbage because all it has done is reserve space for the variable and that space is for an address not a value.

on line 16 you are trying to call a function with "*age" this de-references the pointer that has no value and the pointer address has no value except maybe a null address or garbage. The compiler does not like this.

Without testing my first thought is to make "age" a regular variable and this should solve most if not all the problems. Then when you call a function just use "age".

That is my thoughts for now, but I will need a little time to load it up and test it.

Hope that helps,

Andy
Hello hjabba89,

Instead of letting me figure out the wrong way to fix this program you should post the requirements that you were given. This way everyone will know what has to be done.

Andy
Hello Andy,

Thank you so much, with your assistance I was able to correct the errors I added and solve the ACTUAL errors the code had.
Hello hjabba89,

You are welcome. Any Questions just ask.

In the future post what you were given for instructions and give the complete error message. Not everyone knows what C4700 means.

Andy
Topic archived. No new replies allowed.