Uninitialized variable

Hi! I'm working on a program but can't seem to figure out one run time error which keeps popping up, if anyone can give some ideas?

The error is: "Run-Time Check Failure #3 - The variable 'aa' is being used without being initialized."

Any Help would be greatly appreciated!

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
//Player_Details

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Player {
public:
	void GetPlayer (string fnamn, string enamn, string position, int alder);
	void PrintInfo();
private:
	vector<string>p_fnamn;
	vector<string>p_enamn;
	vector<string>p_position;
	vector<int>p_alder;
};

void Player::GetPlayer(string fnamn, string enamn, string position, int alder)
{
	char val;

	do
	{
	    cout << endl << endl;
	    cout << "Förenamn: ";
	    getline(cin, fnamn);
	    p_fnamn.push_back(fnamn);
	    cout << "Efternamn: ";
	    getline(cin, enamn);
	    p_enamn.push_back(enamn);
	    cout << "Position: ";
	    getline(cin, position);
	    p_position.push_back(position);
	    cout << "Ålder: ";
	    cin >> alder;
	    p_alder.push_back(alder);
	    cin.get();                         //Tar bort newline i input buffer, annars
	                                       //fungerar inte nästa getline()
	    cout << "\n\nVill du lägga till ny spelare(j/n)? ";
	    cin >> val;
	    cin.get();
	} while (val == 'j' || val == 'J');
};

void Player::PrintInfo()
{
	vector<string>::iterator fn;
	vector<string>::iterator en;
	vector<string>::iterator p;
	vector<int>::iterator a;

	for (fn = p_fnamn.begin(), en = p_enamn.begin(), p = p_position.begin(), a = p_alder.begin();
		 fn != p_fnamn.end(), en != p_enamn.end(), p != p_position.end(), a != p_alder.end();
		 ++fn, ++en, ++p, ++a)
	{
		cout << endl << endl; 

		cout << "Spelare: " << *en << ", " << *fn << ".    " << *a << ".    " << *p << endl << endl;
	}
};

int main ()
{
	Player player;
	int val_2;

	do
	{
		cout << "Menu. " << endl
		     << "================" << endl;
	    cout << "(1) Lägg till Spelare. " << endl
		     << "(2) Visa alla Spelare. " << endl << endl;
	    cout << "(0) Avsluta. " << endl << endl
		     << "Välj: ";
	    cin >> val_2;
	    cin.get();

	    if (val_2 == 1)
		{
			string ffnn, eenn, pp;
		    int aa;

		    player.GetPlayer(ffnn, eenn, pp, aa);
	    }
		else if (val_2 == 2)
		{
			player.PrintInfo();
	    }
		else
		{
			return 0;
		}
	}while(val_2 != 0);

    return 0;
}


a
Last edited on
The problem is here:
1
2
3
		    int aa;

		    player.GetPlayer(ffnn, eenn, pp, aa);


You pass aa to your function GetPlayer() without assigning it a value. It could contain anything. It won't always be zero by default, it will be fairly random.
Mmmm.....yeah! Didn't notice that....thank you :)
Topic archived. No new replies allowed.