Evaluation of structure's element against input not working!

This is a discussion on Evaluation of structure's element against input not working! within the C Programming forums, part of the General Programming Boards category; Hello World! 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")) ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    Thumbs up [SOLVED] Evaluation of structure's element against input not working!

    Hello World!

    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++;
    	}
    }
    The first function reads in the data from a file, and puts it into a structure, row by row. The structure deifinition looks like this:

    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];
    };
    And the function that does not work as it is supposed to looks like this:

    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");
    }
    NOTE: While trying to figure out, what is the problem, I simlified the get_team() function. You are supposed to enter the team name: "Agarak" . The contents of the games[0].home_team is "Agarak", thus the condition should evaluate to true - at least, in my oppinion... games[] is an array of structures, and if I print out the contents with a for loop, it seems to contain all the data correctly... I suspect, that there may be some stray \0 or \n characters in the strings, because of scanf() or sscanf() or fgets(), but I couldn't find proper references, which would shed some light on this matter. Any help ideas would be welcome!
    Last edited by laczfinador; 05-11-2009 at 02:22 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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);

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    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);
    it outputs "Agarak"...

    Any other guesses?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    You can't if check a string like that. You need to use strcmp.

    strcmp - C++ Reference
    Woop?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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");
    }
    That should get you rocking.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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++;
    }
    Note the blank space after the sscanf() control string format.
    Last edited by itCbitC; 05-11-2009 at 02:17 PM.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    19
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  2. h/w help
    By helpme in forum C Programming
    Replies: 20
    Last Post: 10-21-2003, 10:36 AM
  3. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  4. why is the input not working
    By mackol in forum C Programming
    Replies: 7
    Last Post: 11-07-2002, 04:28 AM
  5. Working with arrays of structures, need advice...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-14-2002, 09:38 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21