Structs and Displaying Details

Thread Solved

Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

Structs and Displaying Details

 
0
  #1
Mar 7th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Structs and Displaying Details

 
1
  #2
Mar 7th, 2008
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);
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

Re: Structs and Displaying Details

 
0
  #3
Mar 7th, 2008
Originally Posted by mitrmkar View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Structs and Displaying Details

 
0
  #4
Mar 7th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

Re: Structs and Displaying Details

 
0
  #5
Mar 7th, 2008
Thank you very much Ancient, it works now.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC