![]() |
| | #1 |
| Registered User Join Date: Dec 2008
Posts: 13
| Let me get straight to the point. Here's the code: Code: void readfile(){
char row[55];
int n=0;
if(source_file=fopen("meccs.txt","r"))
printf("1) file succefully opened!\n");
fgets(row,55,source_file);
number_of_games=atoi(&row);
while(fgets(row,55,source_file)){
sscanf(row,"%d%d%d%d%d%s%s",&games[n].round,&games[n].home_final,&games[n].guest_final,&games[n].home_half,&games[n].guest_half,&games[n].home_team,&games[n].guest_team);
n++;
}
}
Code: struct game{
int round;
int home_final;
int guest_final;
int home_half;
int guest_half;
char home_team[20];
char guest_team[20];
};
Code: void get_team(){
int i=0,n=0;
char team_name[20];
printf("Give me a team name:\n");
scanf("%s",team_name);
if(team_name==games[0].home_team)
printf("ok!\n");
}
Last edited by laczfinador; 05-11-2009 at 01:22 PM. |
| laczfinador is offline | |
| | #2 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| each conversion format should have a corresponding pointer to it, as in Code: sscanf(row,"%d%d%d%d%d%s%s",&games[n].round,&games[n].home_final,&games[n].guest_final,
&games[n].home_half,&games[n].guest_half,games[n].home_team,games[n].guest_team);
|
| itCbitC is offline | |
| | #3 |
| Registered User Join Date: Dec 2008
Posts: 13
| Hey there itCbitC! I altered the code, according to your reply, and guess what... it still doesn't work! But what is more interesting, is that all the other functions work properly, the program compiles without any ramblings, only the evaluation of the team's name doesn't work! But if I say: Code: printf("%s",games[0].home_team);
Any other guesses? |
| laczfinador is offline | |
| | #4 |
| Sweet Join Date: Aug 2002 Location: Tucson, Arizona
Posts: 1,678
|
__________________ Woop? |
| prog-bman is offline | |
| | #5 |
| Registered User Join Date: Sep 2006
Posts: 2,510
| In C you don't want to compare strings, by comparing the addresses to those strings, which is what this bit of code, does: Code: void get_team(){
int i=0,n=0;
char team_name[20];
printf("Give me a team name:\n");
scanf("%s",team_name);
You need to replace this line of code, with a call to
if( strcmp(team_name, games[0].home_team) ==0)
// if(team_name==games[0].home_team)
printf("ok!\n");
}
|
| Adak is offline | |
| | #6 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Calling fgets() followed by sscanf() mostly lead to problems so append whitespace to the sscanf() format specification. The first fget() isn't followed by a sscanf(), meaning that the file offset is on the second line, past the team name of "Agarak". Moreover string comparison uses strcmp(). Code: fgets(row,55,source_file);
number_of_games=atoi(&row);
/* need a sscanf() here */
while(fgets(row,55,source_file)) {
sscanf(row,"%d%d%d%d%d%s%s ",&games[n].round,&games[n].home_final,&games[n].guest_final,
&games[n].home_half,&games[n].guest_half,&games[n].home_team,&games[n].guest_team);
n++;
}
Last edited by itCbitC; 05-11-2009 at 01:17 PM. |
| itCbitC is offline | |
| | #7 |
| Registered User Join Date: Dec 2008
Posts: 13
| Hello Adak and prog-bman! Your replies has saved my day - or rather night, as it is 19:14 over here. I looked over the fact, that in former excersises, I worked with integer values, which did not need any tricky functions to evaluate... another lesson learned. ![]() And to itCbitC: The first line contains information irrelevant for the functions, so I just "skipped" it before starting to harvest the real information for the program. Nevertheless, thanks trying to help out! Keep up the good work guys! I hope You'll be around for another few hours. |
| laczfinador is offline | |
![]() |
| Tags |
| condition, evaluation, string, structure |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need some help with C program writing | The_PC_Gamer | C Programming | 9 | 02-12-2008 09:12 PM |
| h/w help | helpme | C Programming | 20 | 10-21-2003 09:36 AM |
| need help with some input | blindleaf | C Programming | 2 | 03-16-2003 01:50 PM |
| why is the input not working | mackol | C Programming | 7 | 11-07-2002 04:28 AM |
| Working with arrays of structures, need advice... | Unregistered | C Programming | 3 | 08-14-2002 08:38 AM |