Team Program

Hi! Im currently working on a school project which I am having slight trouble with if anyone can help??
It is a program where you can enter player details, like...first name, surname, age and playing position, later you have the choice to either end the program or list all input players.........the problem I am having is first it says: "Run-Time Check Failure #3 - The variable 'aa' is being used without being initialized."

and the second problem is how to loop so user gets back to the menu when wanted.

Below is the program (which is not very well commented yet ).

Grateful for any help!

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
#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;

	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;
	}
	
	system ("pause");
	return 0;
}
Last edited on
I'm not reading any of that until you wrap your code in [code] tags...
its looking hebrew...
i started to read it but then my eyes exploded.
How wude!

-Jar Jar 'Batros
oh.. there is a casualty...
closed account (D80DSL3A)
@monkeyboy. Code tags really do help us to see the code better. They also provide line numbers for the code , which is handy when we need to refer to portions of your code.
For code tags, click on the <> symbol. This to the right below Format: when replying, or at the bottom of the window when creating a new thread. Place your code between the tags.

May I suggest a design change?
Your Player class looks more like an all_players class.
Let the class represent a single player, then use a vector of players in main() to store them.
The Player class would then have no vector members, just 3 strings and an int.
Change your getPlayer() so it returns a player, and lose the unneeded arguments.
Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Player GetPlayer(void)
{
	Player P;// for reading data into

	cout << endl << endl;
	cout << "Förenamn: ";
	getline(cin, P.fnamn);
	cout << "Efternamn: ";
	getline(cin, P.enamn);
	cout << "Position: ";
	getline(cin, P.position);
	cout << "Ålder: ";
	cin >> P.alder;

	return P;
}

Also, redo PrintInfo() so it displays just ONE Players info.

Use a do while loop within main() to handle the events.
1
2
3
4
5
6
7
8
9
10
11
vector<Player> Pvec;// store all players here
int val_2 = 0;

do// this will repeat until val_2 == 0 ( user wishes to quit )
{
	// display menu
	// get value for val_2
	// if val_2 == 1  add a Player with Pvec.push_back( GetPlayer() );
	//if val_2 == 1  display the players by iterating through Pvec
			// and calling PrintInfo() for each one			
}while( val_2 != 0 );	 

Last edited on
Ok! Thanks for reply's and sorry about the tags but i'm new to this and didn't know :(

@fun2code....thanks for that, I will try this......
Topic archived. No new replies allowed.