943,724 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 654
  • C RSS
Mar 7th, 2008
0

Structs and Displaying Details

Expand Post »
Well another problem, sorry for constantly asking.

My program is coming along well i think, it creates the file writes the data to it but it crashes when i try to view the information.

i'm not sure if its an error with my loaddata() function or my viewcharacter() function

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* Maximum Name Size */
  5. #define NAMESIZE 21
  6. #define BUFFER 200
  7. /* player structure */
  8. typedef struct
  9. {
  10. char *name;
  11. int level;
  12. }player;
  13. /* Global Variables */
  14. FILE *data;
  15. FILE *playerdata;
  16. player p;
  17. /* Prototypes */
  18. /*prints out the menu and then returns the users menu choice*/
  19. int mainmenu();
  20. int gamemenu();
  21. /*creates a new save datafile*/
  22. int newgame();
  23. /*loads an existing save datafile*/
  24. int loadgame();
  25. /*loads data from the text file into the character struct*/
  26. int loaddata();
  27. /*prints out the characters details*/
  28. int viewcharacter();
  29. /*closes all files ready for program exit*/
  30. int exitgame();
  31.  
  32.  
  33. int main(void)
  34. {
  35. /*variables*/
  36. /*holds the users menu choice*/
  37. int menu;
  38. /*holds whether a character is loaded 1 = loaded*/
  39. int charloaded;
  40.  
  41. /*display first menu*/
  42. do
  43. {
  44. menu = mainmenu();
  45. switch (menu)
  46. {
  47. case 1:
  48. charloaded = newgame();
  49. break;
  50. case 2:
  51. charloaded = loadgame();
  52. break;
  53. case 3:
  54. return 0;
  55. break;
  56. }
  57. }while (charloaded == 0); /*while no character is loaded loop*/
  58. /*load data*/
  59. loaddata();
  60.  
  61. /*display second menu*/
  62. do
  63. {
  64. menu = gamemenu();
  65. switch (menu)
  66. {
  67. case 1:
  68. viewcharacter();
  69. break;
  70. case 2:
  71. break;
  72. case 3:
  73. exitgame();
  74. return 0;
  75. break;
  76. }
  77. }while(menu != 3);
  78. getchar();
  79. return 0;
  80. }
  81.  
  82. int mainmenu()
  83. {
  84. int menu;
  85. printf("1 - Start New Game\n");
  86. printf("2 - Load New Game\n");
  87. printf("3 - Exit\n\n");
  88. printf("Please Make Your Choice: ");
  89. menu = inputinteger();
  90. printf("\n\n");
  91.  
  92. return menu;
  93. }
  94. int gamemenu()
  95. {
  96. int menu;
  97. printf("1 - View Character\n");
  98. printf("2 - Continue Adventure\n");
  99. printf("3 - Exit\n\n");
  100. printf("Please Make Your Choice: ");
  101.  
  102. menu = inputinteger();
  103. return menu;
  104. }
  105.  
  106. int newgame()
  107. {
  108. char input[BUFFER];
  109. char name[NAMESIZE];
  110. int length;
  111.  
  112. printf("Please Enter Your Name\n");
  113. printf("Maximum Length 20 characters: ");
  114. fgets(input,BUFFER,stdin);
  115. length = strlen(input); /*find the length of the name*/
  116. if ( length > NAMESIZE )
  117. {
  118. printf("Error - Name to Long\n");
  119. return 0;
  120. }
  121. else
  122. {
  123. strcpy(name,input);
  124. }
  125. length = strlen(name);
  126. if (name[length-1] == '\n') /*check if last character is new line char*/
  127. {
  128. name[length-1] = 0; /*remove the newline char*/
  129. }
  130.  
  131. playerdata = fopen(name, "w+"); /*create the new file*/
  132. if (!playerdata)
  133. {
  134. printf("Error - Unable to Create File\n");
  135. return 0;
  136. }
  137. else
  138. {
  139. fputs(name,playerdata); /*write the players name to the file*/
  140. fputs ("\n",playerdata); /*new line*/
  141. fputs("1",playerdata); /*write players level to file*/
  142. }
  143. return 1;
  144. }
  145.  
  146. int loadgame()
  147. {
  148. char name[BUFFER];
  149. int length;
  150. printf("Please enter the name of the character you wish to load\n");
  151. fgets(name,BUFFER,stdin);
  152. length = strlen(name);
  153. if (name[length-1] == '\n') /*check if last character is new line char*/
  154. {
  155. name[length-1] = 0; /*remove the newline char*/
  156. }
  157. playerdata = fopen(name, "r");
  158. if (playerdata == NULL)
  159. {
  160. printf("Unable to Load File\n");
  161. return 0;
  162. }
  163. return 1;
  164. }
  165.  
  166. int loaddata()
  167. {
  168. fscanf(playerdata, "%s", &p.name);
  169. fscanf(playerdata, "%d", &p.level);
  170. return 0;
  171. }
  172.  
  173. int viewcharacter()
  174. {
  175. printf("Name:\t%s\n", p.name);
  176. printf("Level:\t%s\n",p.level);
  177. }
  178.  
  179. int exitgame()
  180. {
  181. fclose(playerdata);
  182. fclose(data);
  183. }


Thanks again for anyone who can help me
-Matt
Last edited by midimatt; Mar 7th, 2008 at 4:39 am. Reason: typo in code tags
Reputation Points: 34
Solved Threads: 4
Light Poster
midimatt is offline Offline
49 posts
since Mar 2008
Mar 7th, 2008
1

Re: Structs and Displaying Details

p.level is an integer, hence %d instead of %s ...
int viewcharacter()
{
    printf("Name:\t%s\n", p.name);
    printf("Level:\t%d\n",p.level);
}
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 7th, 2008
0

Re: Structs and Displaying Details

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
p.level is an integer, hence %d instead of %s ...
int viewcharacter()
{
    printf("Name:\t%s\n", p.name);
    printf("Level:\t%d\n",p.level);
}
Thank you, but it hasnt solved the problem but i think i know what the problem is, its the inputing from a file thats causing the problem.

as soon as i comment out the 2 lines of code that input the data my program runs perfectly no crashes.

i've tried using fgets() instead for the input but thats still crashes it

i then tried using fgets again this time writing what it reads to a global variable this worked perfectly. so i'm thinking that the problem lies with writing data to the variables inside my struct.

-Sad Matt
Last edited by midimatt; Mar 7th, 2008 at 9:34 am.
Reputation Points: 34
Solved Threads: 4
Light Poster
midimatt is offline Offline
49 posts
since Mar 2008
Mar 7th, 2008
0

Re: Structs and Displaying Details

line 168: two problems:
1) doesn't look like malloc() was everf called to allocate space for name. That will most definitely crash the program.
2) you are passing a pointer to a pointer in that function. Remove the & because name is already a pointer.
Last edited by Ancient Dragon; Mar 7th, 2008 at 11:14 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 7th, 2008
0

Re: Structs and Displaying Details

Thank you very much Ancient, it works now.
Reputation Points: 34
Solved Threads: 4
Light Poster
midimatt is offline Offline
49 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Quick functions question...
Next Thread in C Forum Timeline: Sammalest value in array





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


Follow us on Twitter


© 2011 DaniWeb® LLC