Name: Anonymous 2006-11-21 5:01
will someone please explain to me why this is happenning.
compiles fine, when i run it though, i get this:
----jGRASP exec: /Users/mynamehere/Documents/CSC/a.out
----jGRASP wedge: process died on signal 10.
----jGRASP: operation complete.
heres the actual code, i've combed through it plenty times, and i still can't figure out this error. im using jGrasp, which is what was given to us by our professor, on mac os x 10.4
i've checked and verified the text file myself, and it's perfectly fine. i've also compiled and ran it on our campus' UNIX system using gcc -Wall -ansi -pedantic, but that returns "Segmentation fault (core dumped)"
/*
* my name here
* CPE 101
* Program 8
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[15];
int aFieldGoals, fieldGoals,
aFreeThrows, freeThrows;
} playerData;
int totalPoints(playerData player);
double fieldGoalPercent(playerData player);
double freeThrowPercent(playerData player);
double teamFieldGoalPercent(const playerData players[]);
double teamFreeThrowPercent(const playerData players[]);
void displayPlayer(playerData player);
void displayPlayerArray(const playerData player[]);
int main()
{
FILE *inp;
playerData players[1000];
char * name;
int count = 1;
int inputStatus;
int errors = 0;
int i;
int aFieldGoals;
int fieldGoals;
int aFreeThrows;
int freeThrows;
inp = fopen("bbstats.txt", "r");
if(inp == NULL)
{
printf("error, could not read file");
exit(1);
}
inputStatus = fscanf(inp, "%s %d %d %d %d", name,
&aFieldGoals, &fieldGoals,
&aFreeThrows, &freeThrows);
if((aFieldGoals >= 0) && (aFieldGoals <= 100) &&
(fieldGoals >= 0) && (fieldGoals <= 100) &&
(aFreeThrows >= 0) && (aFreeThrows <= 100) &&
(freeThrows >= 0) && (freeThrows <= 100) &&
(aFieldGoals >= fieldGoals) && (aFreeThrows >= freeThrows))
{
strcpy(name, players[count].name);
players[count].aFieldGoals = aFieldGoals;
players[count].fieldGoals = fieldGoals;
players[count].aFreeThrows = aFreeThrows;
players[count].freeThrows = freeThrows;
}
else
{
errors++;
count--;
printf("Invalid data in line %d", (count + errors));
}
while(inputStatus != EOF)
{
count++;
inputStatus = fscanf(inp, "%s %d %d %d %d", name,
&aFieldGoals, &fieldGoals,
&aFreeThrows, &freeThrows);
if((aFieldGoals >= 0) && (aFieldGoals <= 100) &&
(fieldGoals >= 0) && (fieldGoals <= 100) &&
(aFreeThrows >= 0) && (aFreeThrows <= 100) &&
(freeThrows >= 0) && (freeThrows <= 100) &&
(aFieldGoals >= fieldGoals) && (aFreeThrows >= freeThrows))
{
for(i = 1; i < count; i++)
{
if(strcmp(players[i].name, name) == 0)
{
players[i].aFieldGoals += aFieldGoals;
players[i].fieldGoals += fieldGoals;
players[i].aFreeThrows += aFreeThrows;
players[i].freeThrows += freeThrows;
errors++;
count--;
}
else
{
strcpy(name, players[count].name);
players[count].aFieldGoals = aFieldGoals;
players[count].fieldGoals = fieldGoals;
players[count].aFreeThrows = aFreeThrows;
players[count].freeThrows = freeThrows;
}
}
}
else
{
errors++;
count--;
printf("Invalid data in line %d", (count + errors));
}
}
players[0].aFieldGoals = count;
/*used to store the length of the array*/
displayPlayerArray(players);
fclose(inp);
return 0;
}
int totalPoints(playerData player)
{
return (player.fieldGoals * 2) + player.freeThrows;
}
double fieldGoalPercent(playerData player)
{
return (double)player.fieldGoals / (double)player.aFieldGoals;
}
double freeThrowPercent(playerData player)
{
return (double)player.freeThrows / (double) player.aFreeThrows;
}
double teamFieldGoalPercent(const playerData players[])
{
int aTotal = 0, total = 0, i;
for(i = 1; i <= players[0].aFieldGoals; i++)
{
aTotal += players[i].aFieldGoals;
total += players[i].fieldGoals;
}
return (double)total / (double)aTotal;
}
double teamFreeThrowPercent(const playerData players[])
{
int aTotal = 0, total = 0, i;
for(i = 1; i <= players[0].aFieldGoals; i++)
{
aTotal += players[i].aFreeThrows;
total += players[i].freeThrows;
}
return (double)total / (double)aTotal;
}
void displayPlayer(playerData player)
{
printf("%s\t\t\t", player.name);
printf("%d\t", player.aFieldGoals);
printf("%d.\t", player.fieldGoals);
printf("%.2f\t", fieldGoalPercent(player));
printf("\t%d\t", player.aFreeThrows);
printf("%d\t", player.freeThrows);
printf("%.2f\t", freeThrowPercent(player));
printf("\t%d", totalPoints(player));
}
void displayPlayerArray(const playerData player[])
{
int i;
for(i = 1; i <= player[0].aFieldGoals; i++)
{
printf("Player\t\t\tTried\tmade\tp\t\ttried\tmade\tp\t\tpoints\n");
displayPlayer(player[i]);
}
printf("\t\t\tTeam Avg %.2f\t\tTeam Avg %.2f",
teamFieldGoalPercent(player),
teamFreeThrowPercent(player));
}
compiles fine, when i run it though, i get this:
----jGRASP exec: /Users/mynamehere/Documents/CSC/a.out
----jGRASP wedge: process died on signal 10.
----jGRASP: operation complete.
heres the actual code, i've combed through it plenty times, and i still can't figure out this error. im using jGrasp, which is what was given to us by our professor, on mac os x 10.4
i've checked and verified the text file myself, and it's perfectly fine. i've also compiled and ran it on our campus' UNIX system using gcc -Wall -ansi -pedantic, but that returns "Segmentation fault (core dumped)"
/*
* my name here
* CPE 101
* Program 8
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[15];
int aFieldGoals, fieldGoals,
aFreeThrows, freeThrows;
} playerData;
int totalPoints(playerData player);
double fieldGoalPercent(playerData player);
double freeThrowPercent(playerData player);
double teamFieldGoalPercent(const playerData players[]);
double teamFreeThrowPercent(const playerData players[]);
void displayPlayer(playerData player);
void displayPlayerArray(const playerData player[]);
int main()
{
FILE *inp;
playerData players[1000];
char * name;
int count = 1;
int inputStatus;
int errors = 0;
int i;
int aFieldGoals;
int fieldGoals;
int aFreeThrows;
int freeThrows;
inp = fopen("bbstats.txt", "r");
if(inp == NULL)
{
printf("error, could not read file");
exit(1);
}
inputStatus = fscanf(inp, "%s %d %d %d %d", name,
&aFieldGoals, &fieldGoals,
&aFreeThrows, &freeThrows);
if((aFieldGoals >= 0) && (aFieldGoals <= 100) &&
(fieldGoals >= 0) && (fieldGoals <= 100) &&
(aFreeThrows >= 0) && (aFreeThrows <= 100) &&
(freeThrows >= 0) && (freeThrows <= 100) &&
(aFieldGoals >= fieldGoals) && (aFreeThrows >= freeThrows))
{
strcpy(name, players[count].name);
players[count].aFieldGoals = aFieldGoals;
players[count].fieldGoals = fieldGoals;
players[count].aFreeThrows = aFreeThrows;
players[count].freeThrows = freeThrows;
}
else
{
errors++;
count--;
printf("Invalid data in line %d", (count + errors));
}
while(inputStatus != EOF)
{
count++;
inputStatus = fscanf(inp, "%s %d %d %d %d", name,
&aFieldGoals, &fieldGoals,
&aFreeThrows, &freeThrows);
if((aFieldGoals >= 0) && (aFieldGoals <= 100) &&
(fieldGoals >= 0) && (fieldGoals <= 100) &&
(aFreeThrows >= 0) && (aFreeThrows <= 100) &&
(freeThrows >= 0) && (freeThrows <= 100) &&
(aFieldGoals >= fieldGoals) && (aFreeThrows >= freeThrows))
{
for(i = 1; i < count; i++)
{
if(strcmp(players[i].name, name) == 0)
{
players[i].aFieldGoals += aFieldGoals;
players[i].fieldGoals += fieldGoals;
players[i].aFreeThrows += aFreeThrows;
players[i].freeThrows += freeThrows;
errors++;
count--;
}
else
{
strcpy(name, players[count].name);
players[count].aFieldGoals = aFieldGoals;
players[count].fieldGoals = fieldGoals;
players[count].aFreeThrows = aFreeThrows;
players[count].freeThrows = freeThrows;
}
}
}
else
{
errors++;
count--;
printf("Invalid data in line %d", (count + errors));
}
}
players[0].aFieldGoals = count;
/*used to store the length of the array*/
displayPlayerArray(players);
fclose(inp);
return 0;
}
int totalPoints(playerData player)
{
return (player.fieldGoals * 2) + player.freeThrows;
}
double fieldGoalPercent(playerData player)
{
return (double)player.fieldGoals / (double)player.aFieldGoals;
}
double freeThrowPercent(playerData player)
{
return (double)player.freeThrows / (double) player.aFreeThrows;
}
double teamFieldGoalPercent(const playerData players[])
{
int aTotal = 0, total = 0, i;
for(i = 1; i <= players[0].aFieldGoals; i++)
{
aTotal += players[i].aFieldGoals;
total += players[i].fieldGoals;
}
return (double)total / (double)aTotal;
}
double teamFreeThrowPercent(const playerData players[])
{
int aTotal = 0, total = 0, i;
for(i = 1; i <= players[0].aFieldGoals; i++)
{
aTotal += players[i].aFreeThrows;
total += players[i].freeThrows;
}
return (double)total / (double)aTotal;
}
void displayPlayer(playerData player)
{
printf("%s\t\t\t", player.name);
printf("%d\t", player.aFieldGoals);
printf("%d.\t", player.fieldGoals);
printf("%.2f\t", fieldGoalPercent(player));
printf("\t%d\t", player.aFreeThrows);
printf("%d\t", player.freeThrows);
printf("%.2f\t", freeThrowPercent(player));
printf("\t%d", totalPoints(player));
}
void displayPlayerArray(const playerData player[])
{
int i;
for(i = 1; i <= player[0].aFieldGoals; i++)
{
printf("Player\t\t\tTried\tmade\tp\t\ttried\tmade\tp\t\tpoints\n");
displayPlayer(player[i]);
}
printf("\t\t\tTeam Avg %.2f\t\tTeam Avg %.2f",
teamFieldGoalPercent(player),
teamFreeThrowPercent(player));
}