دنبال کننده ها

۱۳۹۶ مرداد ۱۰, سه‌شنبه

Passing entire structure to a function and display the contents C

[ad_1]



I have been stuck on this for a while now.The program reads lines from a text file into array of structures. Here is a few lines so you get an idea of what is being stored into array of structures.



A|Baltimore Orioles|Oriole Park|333 West Camden Street|Baltimore|MD|21201|(410) 685-9800|orioles.com
N|Washington Nationals|Nationals Park|1500 South Capitol Street, SE|Washington|DC|20003-1507|(202) 675-6287|nationals.com


There is no problem when it comes to loading the arrays with correct data. I tested the contents using this piece of code and it displays correct data.



 scanf("%i",&userChoice);

if(userChoice == 1)
for(index=0; index<count; index++)

if(strcmp("A", teams[index].leagueName)== 0)


americanLeague(&teams[index]);







The program has a main menu and option 1 is to display all teams from American League or "A" from text file. Now I don't want the for loop/if statement to run in main function.I want it just to run the if statement for userchoice and call the americanLeague function. Here is my attempt



scanf("%i",&userChoice);



 if(userChoice == 1)


americanLeague(&teams[index]);





This just calls americanLeague function



void americanLeague(team_t *aTeamPtr)

int index;

for(index=0; index<=MAX_TEAMS; index++)
if(strcmp("A", aTeamPtr[index].leagueName)== 0)


printf("LEAGUE:%s TEAM:%s PARKNAME:%s ADDRESS:%s CITY:%s STATE:%s ZIPCODE:%s PHONE#:%s WEBADDRESS:%snn",
aTeamPtr->leagueName, aTeamPtr->teamName, aTeamPtr->parkName,
aTeamPtr->teamAddress, aTeamPtr->teamCity, aTeamPtr->teamState,
aTeamPtr->zipCode, aTeamPtr->phoneNumber, aTeamPtr->webAddress);







Here is my attempt to try and display teams info passing array of structures into another function.The code doesn't work, but it doesn't give me any errors it just outputs blank spaces.I will also add structure and how I read file just in case.



typedef struct

char leagueName[LEAGUE_NAME + 1];
char teamName[LEN_NAME + 1];
char parkName[PARK_NAME + 1];
char teamAddress[TEAM_ADDRESS + 1];
char teamCity[TEAM_CITY + 1];
char teamState[TEAM_STATE + 1];
char zipCode[ZIP_CODE + 1];
char phoneNumber[PHONE_NUMBER + 1];
char webAddress[WEB_ADDRESS + 1];


team_t;

int main(void)
{
FILE * filePtr;
int index, count;
char line[LEN_LINE + 1];
char repeat;
team_t teams[MAX_LINES];
filePtr = fopen("MLBteams.txt", "r");
if(filePtr == NULL)

printf("Unable to open file.n");

else
{
index = 0;
while(index<MAX_LINES && fgets(line, LEN_LINE, filePtr))

if(9 == sscanf(line,"%5[^
fclose(filePtr);
count = index;


There is no problem when reading correct data into array of structures.I already tested the output several times within main function.Anyways any help would be much appreciated.




[ad_2]

لینک منبع