Hey
I'm trying to create a Database program and every time I run the Program it will take in information but when i ask for it to reprint it it comes out with a massage that says "Unhandled exception" after returning the first two points of data. if you could give me any help that would be great. Please and Thank you
Here is my program if you need it...
(Header File)
struct Player
{
char m_szName[40];
int m_nNumber;
char m_szPosition;
char m_szTeam;
};
Player PlayerArray[MAXPLAYERS];
int nPlayerCount = 0; // NO cars yet!
void PrintData(Player x)
{
printf("Name: %s\n", x.m_szName);
printf("Number: %d\n", x.m_nNumber );
printf("Position Played: %s\n", x.m_szPosition);
printf("Team: %s\n", x.m_szTeam);
}
// Function to enter data into a Car structure
fflush(stdin);
printf("Enter Number Of the Player: ");
scanf("%d", &pPlayer->m_nNumber );
fflush(stdin);
printf("Enter Players Team Name: ");
scanf("%s", &pPlayer->m_szTeam );
}
// Delete Record #x from the database
// Returns 0 on success, -1 on error
//
int DeleteRecord(int x)
{
if (nPlayerCount == 0) // nothing to delete
return -1; // error
if (x > (nPlayerCount - 1))
return -1; // error
// Range ok
// Test for last element's deletion
if ( x == (nPlayerCount -1))
{
nPlayerCount--;
return 0; // success, last element deleted
}
if (nPlayerCount == 0)
{
printf("\nSorry, no elements to delete. Better luck next time.\n\n");
return;
}
printf("Which one to delete (1 to %d)?", nPlayerCount);
fflush(stdin);
scanf("%d", &x);
if (DeleteRecord(x-1) == 0)
printf("\nRecord %d deleted!\n", x);
else
printf("\7Delete on record %d failed!\n", x);
}
//
// AddEntry()
//
// Add an entry to the database
//
void AddEntry()
{
if (nPlayerCount >= MAXPLAYERS)
{
printf("Hey! I'm already full. Can't add any more records.\n\n");
return;
}
EnterData( &PlayerArray[nPlayerCount] );
nPlayerCount++;
}
// Browse routine
void Browse()
{
int i;
char c;
if (nPlayerCount == 0)
{
printf("\7\nThere are no players.\n\n");
return;
}
for(i=0;;)
{
printf("Record #%d\n", i+1 );
PrintData( PlayerArray[i] );
printf("\n<N>ext, <P>revious, <S>top?");
fflush(stdin);
c=toupper(getchar());
printf("\n");
switch(c)
{
case 'N': if (i < (nPlayerCount -1 ))
i++;
break;
case 'P': if (i > 0) i--;
break;
case 'S': return;
}
}
}
//
// Menu - - the main event for this database program
//
void menu()
{
int nChoice;
for(;;)
{
printf("\nWelcome to the Comer Football Player Database.\n\n");
printf("There are %d records in this database.\n\n",nPlayerCount );
printf("1. Add record\n2. Browse Database\n3. Delete a record\n");
printf("4. Search for something\n5. Exit\n\nCommand >");
fflush(stdin);
scanf("%d", &nChoice);
// dispatch command here
switch( nChoice)
{
case 1:
AddEntry();
break;
case 2:
Browse();
break;
case 3:
Delete();
break;
case 4:
//Search();
break;
case 5:
return; // End the program if '5' was typed
default:
printf("\n\7You idiot! Wrong command!\n");
break;
}
}
}
void main(void)
{
menu(); // Display the menu and run the actual program
There is a significant difference between char and char*:
char: (%c)The compiler expects a single character.
char*:(%s) The compiler expects a string of characters terminated by null.
in your structure
struct Player
{
char m_szName[40];
int m_nNumber;
char m_szPosition;
char m_szTeam;
};
position , Team are of type char so they can hold a single character.
In Enterdata function you are accepting the input as %s
fflush(stdin);
printf("Enter Players Position: ");
scanf("%s", &pPlayer->m_szPosition );
modify these to %c
while printing also modify it to %c , that will take care of your problem.