Structures, external files, and functions

Reply

Join Date: Nov 2007
Posts: 6
Reputation: deadswitch is an unknown quantity at this point 
Solved Threads: 0
deadswitch deadswitch is offline Offline
Newbie Poster

Structures, external files, and functions

 
0
  #1
Nov 8th, 2007
Hey folks --

DaniWeb has helped me a lot with various other programming projects, but I've run into a snag that I just can't get myself to understand. Hooray for vague compiler errors. Hopefully someone here can show me the error of my ways.

I'm making a little game, based on an external map file. You can traverse from room to room using various directional commands (i.e. north south east west). The "maps.c." file contains eight lines for each room, in this format:

room number
room name
room description
number of connections to other rooms (max: 2)
direction 1
connection1
direction 2
connection 2

In my program, I need to declare an array of structures, one structure for each room, each structure having a place for each piece of information about the room.

Then, I need a function to read the information for a particular room, print that information, and then wait for the user to input where to go next.

I've re-worked this code enough times that my brain is about to explode. The following is an (obviously) incomplete version of how I'm starting things off. I suspect some gross conceptual error, but I can't figure it out. At this point all I want to do is get the program to get the room name and room number, and print 'em.

Thanks a lot for any help.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. void readRoom (rooms);
  6.  
  7. int main (void) {
  8.  
  9. typedef struct {
  10. int room_num;
  11. char room_name[80];
  12. char desc[1000];
  13. int num_links;
  14. char direction1[6];
  15. int dest1;
  16. char direction2[6];
  17. int dest2;
  18. } roomStruct;
  19.  
  20. roomStruct rooms[100];
  21.  
  22. readRoom(rooms);
  23.  
  24. return 0;
  25.  
  26. }
  27.  
  28. void readRoom (rooms[]) {
  29.  
  30. FILE *fin;
  31. int i = 0;
  32.  
  33. fin = fopen("maps.c", "r");
  34.  
  35. fscanf(fin, "%d", &rooms[i].room_num);
  36.  
  37. fgets(rooms[i].room_name, 80, fin);
  38.  
  39. printf("Room number: %d ... Room name: %s\n", rooms[i].room_num, rooms[i].room_name);
  40.  
  41. return;
  42.  
  43. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Structures, external files, and functions

 
0
  #2
Nov 8th, 2007
A quick re-org to help:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. // typedefs don't belong inside functions (not usually, anyway...)
  6. typedef struct {
  7. int room_num;
  8. char room_name[80];
  9. char desc[1000];
  10. int num_links;
  11. char direction1[6];
  12. int dest1;
  13. char direction2[6];
  14. int dest2;
  15. } roomStruct;
  16.  
  17. // Functions need complete types
  18. void readRoom ( roomStruct rooms[] );
  19.  
  20. // int main() doesn't take any void arguments.
  21. int main () {
  22. roomStruct rooms[100];
  23.  
  24. readRoom(rooms);
  25.  
  26. return 0;
  27. }
  28.  
  29. // And the type must be the same as the prototype's
  30. void readRoom ( roomStruct rooms[] ) {
  31.  
  32. FILE *fin;
  33. int i = 0;
  34.  
  35. fin = fopen("maps.c", "r");
  36.  
  37. fscanf(fin, "%d", &rooms[i].room_num);
  38.  
  39. fgets(rooms[i].room_name, 80, fin);
  40.  
  41. printf("Room number: %d ... Room name: %s\n", rooms[i].room_num, rooms[i].room_name);
  42.  
  43. // no need to "return" from a void function
  44. }

Hope this helps.
Last edited by Duoas; Nov 8th, 2007 at 11:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: deadswitch is an unknown quantity at this point 
Solved Threads: 0
deadswitch deadswitch is offline Offline
Newbie Poster

Re: Structures, external files, and functions

 
0
  #3
Nov 8th, 2007
Thanks for the re-organization...I should probably learn to comment a little more.

I tried the code you gave me, and it compiled wonderfully! Yay.

Then I ran it. It read in the number correctly and displayed it. The name, however, didn't come up at all.

The output was:

Room number: 0 ... Room name:

What the heck is going on? Is fgets() not reading the next line in the map.c file?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Structures, external files, and functions

 
0
  #4
Nov 8th, 2007
Originally Posted by deadswitch View Post
I've re-worked this code enough times that my brain is about to explode. The following is an (obviously) incomplete version of how I'm starting things off. I suspect some gross conceptual error, but I can't figure it out. At this point all I want to do is get the program to get the room name and room number, and print 'em.
Figure what out? We know what you're trying to do, but you didn't tell us what's actually happening. Knowing that will help us understand he problem.

Kinda like "hey Mr. Mechanic, I want my car to start when I turn the key. Fix it.." What's likely to be his first question?

Also, knowing what's being read might be important...
Last edited by WaltP; Nov 8th, 2007 at 11:53 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: deadswitch is an unknown quantity at this point 
Solved Threads: 0
deadswitch deadswitch is offline Offline
Newbie Poster

Re: Structures, external files, and functions

 
0
  #5
Nov 9th, 2007
Sorry; I'm a bit of a coding forum noobie. I'll be more specific.

// maps.c //
  1. 0
  2. Logan and Eliseo's Room
  3. You are in Logan and Eliseo's room. It's full of people and it's really really loud.
  4. 1
  5. west
  6. 1
  7. none
  8. 0
  9. 1
  10. Jake and Garrett's Room
  11. You are in Jake and Garrett's room. There is a big fancy TV, and Garrett and his buddies are playing Splinter Cell.
  12. 2
  13. east
  14. 0
  15. west
  16. 2
  17. 2
  18. Will and Ryan's Room
  19. You are in Will and Ryan's room. There is a piano, and there's junk strewn all over the place.
  20. 1
  21. east
  22. 1
  23. none
  24. 2
  25.  

The idea is that you start in room 0. You read the description. If you type north, south, or east, it leaves you in room 0, but if you type west, it takes you to room 1, displays the description, and awaits input again. Zork flashbacks, I know.

The "none" is just the second link specifier if there is not a second room connection, so anything but the correct direction will just leave you in the same room.

The re-organized code Duoas gave me pleased the compiler, but didn't correctly print rooms[i].room_name. It didn't print anything, in fact.

Thanks for the help guys; I got really excited about this project and it was kind of a buzzkill to run into problems
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,997
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Structures, external files, and functions

 
0
  #6
Nov 9th, 2007
  1. void readRoom (rooms[]) {
  2.  
  3. FILE *fin;
  4. int i = 0;
  5.  
  6. fin = fopen("maps.c", "r");
  7. if ( fin == NULL )
  8. {
  9. /* some kind of error here */
  10. }
  11. while ( fscanf(fin, "%s%d", rooms[i].room_name,&rooms[i].room_num) > 1 )
  12. {
  13. printf("Room number: %d ... Room name: %s\n", rooms[i].room_num, rooms[i].room_name);
  14. }
  15.  
  16. return;
  17.  
  18. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Structures, external files, and functions

 
0
  #7
Nov 9th, 2007
I haven't played with scanf() in a long while, but what is probably happening is that you are reading the room number, but not the newline. Thereafter, you scan for a room name and get the newline immediately, which terminates the string.

Try getting the room number like this instead:
fscanf( fin, "%d%*[ \t]\n", &rooms[i].room_number );

This gets an int, skips spaces and tabs (if any) and reads a newline, leaving the fp at the beginning of the line containing the room name. Then, when you read the room name it works.

I hope this is correct.

[EDIT] Aia, that completely ignores his input format requirements.
Last edited by Duoas; Nov 9th, 2007 at 12:12 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 6
Reputation: deadswitch is an unknown quantity at this point 
Solved Threads: 0
deadswitch deadswitch is offline Offline
Newbie Poster

Re: Structures, external files, and functions

 
0
  #8
Nov 9th, 2007
Okay, thanks.

This code is working 3/4 of the way:

  1. void readRoom (roomStruct rooms[]) {
  2.  
  3. FILE *fin;
  4. int i = 0;
  5.  
  6. fin = fopen("map.c", "r");
  7.  
  8. fscanf(fin, "%d*[ \t]\n", &rooms[i].room_num);
  9. fscanf(fin, "%s", &rooms[i].room_name);

But it only prints out "Logan" as rooms[i].room_name

I tried fscanf() again, this time putting "%s" into rooms[i].desc, and then printing all three, and that prints

Room number: 0 ... Room name: Logan ... Room desc: and

So it's only getting the first word. Alas, I need the whole line.

Would fgets() be better?
Last edited by deadswitch; Nov 9th, 2007 at 12:30 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,997
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 172
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Structures, external files, and functions

 
0
  #9
Nov 9th, 2007
Originally Posted by Duoas View Post
[EDIT] Aia, that completely ignores his input format requirements.
Correct. Base in the information I had, I took some assumption.
Room_name 1
Room_name 2
etc...
It appeared to me at the time that the OP didn't know how to make use of fscanf and its parameters.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Structures, external files, and functions

 
0
  #10
Nov 9th, 2007
Heh, don't worry 'bout it.

deadswitch
Why did you change your
fgets(rooms[i].room_name, 80, fin);
(which worked fine) to
fscanf(fin, "%s", &rooms[i].room_name);
(which only reads until whitespace is encountered)?
Last edited by Duoas; Nov 9th, 2007 at 3:06 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC