Exception/assertion error

I am trying to add time to my program and I keep getting these "Unhandled exception" errors. Can anyone help? Here are a few code snippits:

Where all of my variables are declared:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private:
   string rank;
   int money;
   int tax;
   string squad;
   int taxRate;
   string CName;
   string FName;
   int ranknum;
   string craftingSquad;
   string militarySquad;
   int craftingSquadNum;
   int militarySquadNum;
   int rock;
   int wood;
   time_t startTime;



Where the error actually takes place:

1
2
3
4
5
6
7
void Player::display()
{
   cout << "Username: "<< FName << endl;
   cout << "Character Name: "<< CName << endl;
   cout << "Date & Time Added: " << ctime(&startTime) << endl;
   cout << "Rank: ";
   if(ranknum==NONE)



Where the objects are first declared and the time is declared:

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
int main()
{
   time_t currtime;
   currtime = time (NULL);
   int choice=0;
   char cchoice='n';
   int lastPlayer=0;
   string cn, un, pr, pms, pcs;
   int pm, currPlayer, ro, wo;

   while(true)
   {
      cout << "Welcome to the Tempest Tax program." << endl;
      cout << "Rules:" << endl;
      cout << "1. When promted for something enter it and press enter." << endl;
      cout << "2. No spaces, if you have to use them use an underscore." << endl;
      cout << endl;

      cout << "What would you like to do?" << endl;
      cout << "(1) Create a new player" << endl;
      cout << "(2) Change an existing player" << endl;
      cout << "(3) Delete an existing player" << endl;
      cout << "(4) Display a list of all players" << endl;
      cout << "(5) Sort players" << endl;
      cout << "(6) Save" << endl;
      cout << "(7) Load" << endl;
      cout << "(8) Exit" << endl;
      cin >> choice;
      cout << endl;
      if(choice==1) // create a new player
      {
         cout << "What is the player's character name?" << endl;
         cin >> cn;
         cout << "What is the player's username?" << endl;
         cin >> un;
         cout << "What is the player's rank?" << endl;
         cin >> pr;
         cout << "What is the player's crafting squad? If no crafting squad exists put NONE" << endl;
         cin >> pcs;
         cout << "What is the player's military squad? If no military squad exists put NONE" << endl;
         cin >> pms;
         cout << endl << endl << endl;

         array[lastPlayer]=Player(cn,un,pr,0,pcs,pms,0,0,currtime);
         lastPlayer++;
      }
You don't seem to be allocating any space for your array...I suggest you use std::vector so you don't have to worry about allocating the space.
Sorry, the array is global. Here is the code.

Player array[160];
Ah, I think I've got, when you are iterating through your array to display a list of all players, you might be iterating till one more then you need to...can I see the code for the 4th option?
Sure:
1
2
3
4
5
6
7
8
else if(choice==4)// display all players
		{
			for(int i=0; i<lastPlayer; i++)
			{
				array[i].display();
				cout << endl<< endl << "************************************" << endl;
			}
		}


Here is display():

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
void Player::display()
{
	cout << "Username: "<< FName << endl;
	cout << "Character Name: "<< CName << endl;
	cout << "Date & Time Added: " << ctime_s(NULL,0,&startTime) << endl;
	cout << "Rank: ";
	if(ranknum==NONE)
		cout << "None" << endl;
	else if(ranknum==RECRUIT)
		cout << "Recruit" << endl;
	else if(ranknum==PRIVATE)
		cout << "Private" << endl;
	else if(ranknum==CORPORAL)
		cout << "Corporal" << endl;
	else if(ranknum==SERGEANT)
		cout << "Sergeant" << endl;
	else if(ranknum==LIEUTENANT)
		cout << "Lieutenant" << endl;
	else if(ranknum==CAPTAIN)
		cout << "Captain" << endl;
	else if(ranknum==MAJOR)
		cout << "Major" << endl;
	else if(ranknum==HERALD)
		cout << "Herald" << endl;
	else if(ranknum==COLONEL)
		cout << "Colonel" << endl;
	else if(ranknum==GENERAL)
		cout << "General" << endl;
	else if(ranknum==SUPREME_GENERAL)
		cout << "Supreme General" << endl;
	else
		cout << "This player has an invalid rank" << endl;

	cout << "Crafting Squad: ";
	if(craftingSquadNum==NONE)
		cout << "None" << endl;
	else if(craftingSquadNum==JEWELCRAFTING_SQUAD)
		cout << "Jewelcrafting Squad" << endl;
	else if(craftingSquadNum==HERBALISM_SQUAD)
		cout << "Herbalism Squad" << endl;
	else if(craftingSquadNum==CONSTRUCTION_SQUAD)
		cout << "Construction Squad" << endl;
	else if(craftingSquadNum==FLETCHING_SQUAD)
		cout << "Fletching Squad" << endl;
	else if(craftingSquadNum==ARMORSMITHING_SQUAD)
		cout << "Armorsmithing Squad" << endl;
	else if(craftingSquadNum==TAILORING_SQUAD)
		cout << "Tailoring Squad" << endl;
	else if(craftingSquadNum==ENCHANTING_SQUAD)
		cout << "Enchanting Squad" << endl;
	else if(craftingSquadNum==WEAPONCRAFTING_SQUAD)
		cout << "Weaponcrafting Squad" << endl;
	else if(craftingSquadNum==COOKING_SQUAD)
		cout << "Cooking Squad" << endl;
	else if(craftingSquadNum==ALCHEMY_SQUAD)
		cout << "Alchemy Squad" << endl;
	else
		cout << "This player has an invalid squad" << endl;

	cout << "Military Squad: ";
	if(militarySquadNum==NONE)
		cout << "None" << endl;
	else if(militarySquadNum==ROYAL_CONQUISTADORS)
		cout << "Royal Conquistadors" << endl;
	else if(militarySquadNum==KINGDOM_SCOUTS)
		cout << "Kingdom Scouts" << endl;
	else if(militarySquadNum==SQUAD_REAVERS)
		cout << "Squad Reavers" << endl;
	else if(militarySquadNum==ALPHA_SQUAD)
		cout << "Alpha Squad" << endl;
	else if(militarySquadNum==TWILIGHT)
		cout << "Twilight" << endl;
	else if(militarySquadNum==LEIGON_SQUAD)
		cout << "Leigon Squad" << endl;
	else if(militarySquadNum==FIRSTRATE)
		cout << "Firstrate" << endl;
	else
		cout << "This player has an invalid squad" << endl << endl;
	cout << "Tax Summery: " << endl;
	cout << "Gold: " << money << endl;
	cout << "Wood: " << wood << endl;
	cout << "Stone: " << rock << endl;
	cout << "Tax Owed: ";
	if(tax-(money*(wood*3)*(rock*3))<=0)
		cout << "0" << endl;
	else
		cout << tax-(money*(wood*3)*(rock*3)) << endl;
	cout << "Donations: " ;
	if((money*(wood*3)*(rock*3))-tax<=0)
		cout << "0" << endl;
	else
		cout << (money*(wood*3)*(rock*3))-tax << endl;
}
On line 5 of display...you aren't passing correct arguments to ctime_s()...you are passing NULL as the char* and 0 as the length, which means that if it tries to write you will be writing on invalid memory.
Sorry that was just a fix i tried to do. The original line is:

cout << "Date & Time Added: " << ctime(&startTime) << endl;
Hrm, that's strange...can you try and step through the code to find the exact line the error is occurring on?
Last edited on
The program fails on the 5th line of the display function. it is when I am outputting the time and date:

cout << "Date & Time Added: " << ctime(&startTime) << endl;
what is the declaration of startTime?
player.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private:
	string rank;
	int money;
	int tax;
	string squad;
	int taxRate;
	string CName;
	string FName;
	int ranknum;
	string craftingSquad;
	string militarySquad;
	int craftingSquadNum;
	int militarySquadNum;
	int rock;
	int wood;
	time_t startTime;


Player.cpp:

1
2
3
4
5
6
7
Player::Player(string pCName, string pFName, string pRank, int pMoney, string pcsquad, string pmsquad, int pr, int pw, time_t st)
{
	CName=pCName;
	FName=pFName;
	rank=pRank;
	money=pMoney;
	st=startTime;


main.cpp:

1
2
time_t currtime;
	currtime = time (NULL);
Does ctime take a time_t* as a parameter or a char*?

I don't know if this is it, but I notice in your Player.cpp snippet above you assign

st = startTime;

instead of

startTime = st;

Oh man, you are a genius. That is why you should program in pairs. A fresh set of eyes never hurt. Thanks again.
If you use initializer lists, you can avoid this kind of problem, at least in constructors.

That's kinda nasty that ctime bombs if given a valid pointer to invalid data...
Topic archived. No new replies allowed.