I wrote a 16 week bowling program for a college class I was in quite a few years ago. I wrote it in C, but now decided to convert it to C++. Pretty much, I got everything working. I can change the scores for any week, I can view any of the 5 players, individually or as a team. I can also delete a player. My problem comes when I would like to ADD a player. I get the prompt for a name, but the program crashes after I hit 'Enter'. I know the whole program isn't here, but I'm hoping that the routine in question, is enough. If whoever may help, needs the full program, I'll be happy to email to you. This last problem has gotten me stumped.
void Add_Name()
{
int Add;
printf("\n\n\tAdd which bowler?...{ 1 to 5 } ? : _\b");
Add = getchar();
Junk = getchar();
switch (Add)
{
case'1':
case'2':
case'3':
case'4':
case'5':
Add-=49;
if (Bowlers[Add].Present < 1)
printf("\n\t%s is already using this space...", Bowlers[Add].Name );
else
{
printf("\n\tPlease type in new bowlers' name. { first name only } : _\b");
Bowlers[Add].Present = 0; /* Show a bowler has arrived */
fflush(stdin);
scanf_s("%s",&Bowlers[Add].Name); // Crashes here after input
printf("\n\tNow I'll give the new bowler, %s, some scores....", Bowlers[Add].Name);
Fill_Scores( Add, 0 );
/* Place new scores into new bowlers game */
Arrange_Scores( Add, 0, 0);
}
break;
default:
printf("\n\t**ERROR** No such bowler... Make another selection ...");
}
Pause();
return;
} /* End of Delete_Name() */
naderST, I just tried as you suggested, but I get the same result. I get a window pop-up that says 'Bowling.exe has stopped working. Windows is searching for a solution..'. Thanks though.
struct Bowler
{
char * Name;
short Present; /* Using 0 for present, 1 for not playing */
struct Week
{
int Games[3]; /* 3 games per week */
float Game_Average; /* 3 game average, 1 bowler */
float Total_Game_Avg; /* Average of ALL games */
int Day_Total; /* Total of 3 games bowled */
int Game_Total; /* Total ALL games bowled */
} Weeks[SEASON];
} Bowlers[5] ;
int Choice;
int Game;
int Start;
int View_Score;
float Average_Total;
char Junk;
char NewName;
void main()
{
srand((unsigned)time(0));
int Bowler = 0;
Bowlers[0].Name = "Stafford";
Bowlers[1].Name = "Anna";
Bowlers[2].Name = "Kimberly";
Bowlers[3].Name = "Christopher";
Bowlers[4].Name = "Bruce";
/* Fill in the names */
Also I included Bowlers[?].Name initialization section in main(). The program runs. It shows the list of bowlers, the scores, etc. I can input new weekly bowling scores, and it updates the averages. It's just adding a new name to the deleted space stops the program. So, I figure I'll insert the 'Delete_Name' function, in case I messed it up somewhere.
void Delete_Name()
{
int Delete;
printf("\n\n\tDelete which bowler?...{ 1 to 5 } ? : _\b");
Delete = getchar();
Junk = getchar();
switch (Delete)
{
case'1':
case'2':
case'3':
case'4':
case'5':
Delete-=49;
if (Bowlers[Delete].Present < 1)
{
printf("\n\t%s has now QUIT this league...", Bowlers[Delete].Name );
Bowlers[Delete].Present = 1;
Renew_Bowler(Delete);
}
else
printf("\n\tThat bowler has already been dismissed....");
break;
default:
printf("\n\t**ERROR** No such bowler... Make another selection ...");
}
Pause();
return;
} /* End of Delete_Name() */
naderST, actually, when I subtract the 49, it's to end up with a number from 0 to 4. The 'Add = Getchar(); gives the decimal # of the input. Pressing 1 ends up as 49. Subtracting 49 now gives 0 which is the first Bowler since the array starts at 0. I have changed my arrays to 6, and using only 1 through 5. I found it's easier to know which bowler I'm working with. Now I subtract 48, to give me 1 through 5. While fiddling with the program, I did get it to accept a name. After typing in a name, it does print, "Now I'll give the new bowler, ...' and it prints the name.
But, when I choose to view the bowlers, the name I changed now shows - "Bowler#1: ╠╠╠╠╠╠╠╠╠╠" and a spade, or diamond afterward. Now it doesn't show the name.
Here is the Add_Name() with my additions.
void Add_Name()
{
int Add;
char AddName[20];
printf("\n\n\tAdd which bowler?...{ 1 to 5 } ? : _\b");
Add = getchar();
Junk = getchar();
switch (Add)
{
case'1':
case'2':
case'3':
case'4':
case'5':
Add-=48;
if (Bowlers[Add].Present < 1)
printf("\n\t%s is already using this space...", Bowlers[Add].Name );
else
{
printf("\n\tPlease type in new bowlers' name. { first name only } : _\b");
Bowlers[Add].Present = 0; /* Show a bowler has arrived */
//fflush(stdin);
cin.getline (AddName,20);
Bowlers[Add].Name = AddName;
printf("\n\tNow I'll give the new bowler, %s, some scores....", Bowlers[Add].Name);
Fill_Scores( Add, 0 );
/* Place new scores into new bowlers game */
Arrange_Scores( Add, 0, 0);
break;
}
//break;
default:
printf("\n\t**ERROR** No such bowler... Make another selection ...");
}
Pause();
return;
} /* End of Delete_Name() */