943,568 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1571
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2007
0

Structures, external files, and functions

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deadswitch is offline Offline
6 posts
since Nov 2007
Nov 8th, 2007
0

Re: Structures, external files, and functions

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 8th, 2007
0

Re: Structures, external files, and functions

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deadswitch is offline Offline
6 posts
since Nov 2007
Nov 8th, 2007
0

Re: Structures, external files, and functions

Click to Expand / Collapse  Quote originally posted by deadswitch ...
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.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,716 posts
since May 2006
Nov 9th, 2007
0

Re: Structures, external files, and functions

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deadswitch is offline Offline
6 posts
since Nov 2007
Nov 9th, 2007
0

Re: Structures, external files, and functions

  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. }
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 9th, 2007
0

Re: Structures, external files, and functions

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 9th, 2007
0

Re: Structures, external files, and functions

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deadswitch is offline Offline
6 posts
since Nov 2007
Nov 9th, 2007
0

Re: Structures, external files, and functions

Click to Expand / Collapse  Quote originally posted by Duoas ...
[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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 9th, 2007
0

Re: Structures, external files, and functions

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Controlling wheter postfix expression is valid
Next Thread in C Forum Timeline: quick sort





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC