C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-11-2009, 12:22 PM   #1
Registered User
 
Join Date: Dec 2008
Posts: 13
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 01:22 PM.
laczfinador is offline   Reply With Quote
Old 05-11-2009, 12:33 PM   #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   Reply With Quote
Old 05-11-2009, 12:47 PM   #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);
it outputs "Agarak"...

Any other guesses?
laczfinador is offline   Reply With Quote
Old 05-11-2009, 12:59 PM   #4
Sweet
 
Join Date: Aug 2002
Location: Tucson, Arizona
Posts: 1,678
You can't if check a string like that. You need to use strcmp.

strcmp - C++ Reference
__________________
Woop?
prog-bman is offline   Reply With Quote
Old 05-11-2009, 01:01 PM   #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");
}
That should get you rocking.
Adak is offline   Reply With Quote
Old 05-11-2009, 01:04 PM   #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++;
}
Note the blank space after the sscanf() control string format.

Last edited by itCbitC; 05-11-2009 at 01:17 PM.
itCbitC is offline   Reply With Quote
Old 05-11-2009, 01:19 PM   #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   Reply With Quote
Reply

Tags
condition, evaluation, string, structure

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:38 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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