| | |
Structs and Displaying Details
Thread Solved |
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
Thanks again for anyone who can help me
-Matt
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
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> /* Maximum Name Size */ #define NAMESIZE 21 #define BUFFER 200 /* player structure */ typedef struct { char *name; int level; }player; /* Global Variables */ FILE *data; FILE *playerdata; player p; /* Prototypes */ /*prints out the menu and then returns the users menu choice*/ int mainmenu(); int gamemenu(); /*creates a new save datafile*/ int newgame(); /*loads an existing save datafile*/ int loadgame(); /*loads data from the text file into the character struct*/ int loaddata(); /*prints out the characters details*/ int viewcharacter(); /*closes all files ready for program exit*/ int exitgame(); int main(void) { /*variables*/ /*holds the users menu choice*/ int menu; /*holds whether a character is loaded 1 = loaded*/ int charloaded; /*display first menu*/ do { menu = mainmenu(); switch (menu) { case 1: charloaded = newgame(); break; case 2: charloaded = loadgame(); break; case 3: return 0; break; } }while (charloaded == 0); /*while no character is loaded loop*/ /*load data*/ loaddata(); /*display second menu*/ do { menu = gamemenu(); switch (menu) { case 1: viewcharacter(); break; case 2: break; case 3: exitgame(); return 0; break; } }while(menu != 3); getchar(); return 0; } int mainmenu() { int menu; printf("1 - Start New Game\n"); printf("2 - Load New Game\n"); printf("3 - Exit\n\n"); printf("Please Make Your Choice: "); menu = inputinteger(); printf("\n\n"); return menu; } int gamemenu() { int menu; printf("1 - View Character\n"); printf("2 - Continue Adventure\n"); printf("3 - Exit\n\n"); printf("Please Make Your Choice: "); menu = inputinteger(); return menu; } int newgame() { char input[BUFFER]; char name[NAMESIZE]; int length; printf("Please Enter Your Name\n"); printf("Maximum Length 20 characters: "); fgets(input,BUFFER,stdin); length = strlen(input); /*find the length of the name*/ if ( length > NAMESIZE ) { printf("Error - Name to Long\n"); return 0; } else { strcpy(name,input); } length = strlen(name); if (name[length-1] == '\n') /*check if last character is new line char*/ { name[length-1] = 0; /*remove the newline char*/ } playerdata = fopen(name, "w+"); /*create the new file*/ if (!playerdata) { printf("Error - Unable to Create File\n"); return 0; } else { fputs(name,playerdata); /*write the players name to the file*/ fputs ("\n",playerdata); /*new line*/ fputs("1",playerdata); /*write players level to file*/ } return 1; } int loadgame() { char name[BUFFER]; int length; printf("Please enter the name of the character you wish to load\n"); fgets(name,BUFFER,stdin); length = strlen(name); if (name[length-1] == '\n') /*check if last character is new line char*/ { name[length-1] = 0; /*remove the newline char*/ } playerdata = fopen(name, "r"); if (playerdata == NULL) { printf("Unable to Load File\n"); return 0; } return 1; } int loaddata() { fscanf(playerdata, "%s", &p.name); fscanf(playerdata, "%d", &p.level); return 0; } int viewcharacter() { printf("Name:\t%s\n", p.name); printf("Level:\t%s\n",p.level); } int exitgame() { fclose(playerdata); fclose(data); }
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
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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);
}•
•
•
•
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); }
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.
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.
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.
![]() |
Other Threads in the C Forum
- Previous Thread: Quick functions question...
- Next Thread: Sammalest value in array
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






