Archived

This topic is now archived and is closed to further replies.

mrtie_dye

C code help

Recommended Posts

mrtie_dye    184
Can someone please tell me why this code will not work? When it gets to the gets(slot1); statement after printing "What would you like to name your file? ", it does not wait for input. It just goes directly to the next statement. And when it enters the while loop, it always prints "Are you sure you want to overwrite the file %s? (yes or no)" twice before asking for input. Thanks in advance for any help you can provide. DoSaveSlots() { char slot1[25] = "Empty"; char slot2[25] = "Empty"; char slot3[25] = "Empty"; char slot4[25] = "Empty"; char slot5[25] = "Empty"; int ans; FILE * slots; slots = fopen("slots.s", "r+"); if (!slots) { fclose(slots); slots = fopen("slots.s", "a+"); fwrite(slot1,sizeof(slot1),1,slots); fwrite(slot2,sizeof(slot2),1,slots); fwrite(slot3,sizeof(slot3),1,slots); fwrite(slot4,sizeof(slot4),1,slots); fwrite(slot5,sizeof(slot5),1,slots); } fseek(slots,0,SEEK_SET); fread(slot1,sizeof(slot1),1,slots); fread(slot2,sizeof(slot2),1,slots); fread(slot3,sizeof(slot3),1,slots); fread(slot4,sizeof(slot4),1,slots); fread(slot5,sizeof(slot5),1,slots); fclose(slots); clrscr(); printf("\n\n\t Slot 1 : %s\n" ,slot1); printf("\t Slot 2 : %s\n" ,slot2); printf("\t Slot 3 : %s\n" ,slot3); printf("\t Slot 4 : %s\n" ,slot4); printf("\t Slot 5 : %s\n" ,slot5); printf("\t Which slot would you like to save to (1 -5)? "); scanf(" %d", &ans); switch (ans) { case (1) : { if (strcmp("Empty", slot1)) { char press[5]; while (strcmp("YES", press) && strcmp("NO", press) && strcmp("Y", press) && strcmp("N", press)) { printf("Are you sure you want to overwrite the file %s? (yes or no) ", slot1); gets(press); DoUcasePress(press); if (!strcmp("NO", press) || !strcmp("N", press)) { return 0; } if (!strcmp("YES", press) || !strcmp("Y", press)) { printf("What would you like to name your file? "); gets(slot1); printf("Your game has been saved as %s\n", slot1); printf("Press any key to continue"); getch(); slots = fopen("slots.s", "r+"); fseek(slots,0,SEEK_SET); fwrite(slot1,sizeof(slot1),1,slots); fclose(slots); return 0; } press[0] = ''\n''; } } printf("What would you like to name your file? "); gets(slot1); printf("Your game has been saved as %s\n", slot1); printf("Press any key to continue"); getch(); slots = fopen("slots.s", "r+"); fseek(slots,0,SEEK_SET); fwrite(slot1,sizeof(slot1),1,slots); fclose(slots); return 0; } default : { break; } } return 0; } DoUcasePress(char press[]) { int a; for (a = 0; a < strlen(press); a++) { pressLink

Share this post


Link to post
Share on other sites
NewbieGamer    122
A few things to note:
1. There are some intricacies involved while using getch(), gets() et al....
You must flush the input buffer before using gets. How to do this...
fflush( stdin );

Or much better do this just before you do gets()... I would #define MYFLUSHALL fflush(stdin);
and use it everywhere...

Add this after your scanf(" %d", &ans) and getch() lines.
NOTE: your scanf(" %d", &ans) has an empty space between the quotes " %d". Try to avoid that.

This should fix you problem...

The other one i noted is your startup code...
if (!slots)
{
//fclose(slots);

}

You are trying to close a file stream knowing full well that it is NULL....... if(!slots) will take you in this if() only if slots is NULL and doing an fclose(NULL) will raise an exception...

One other minor suggestion...
Instead of doing your own DoUCase..... you can use _strupr(press) and continue.....

HTH,


Cheers!!!
V!

Share this post


Link to post
Share on other sites
mrtie_dye    184
Thanks so much for all your advice. My code is now up and running good. As far as the space in my scanf statement and the fclose inside the if(!slots), I am learning c from a book, and that is the way the book says do it. The book is very old (came with turbo c/c++ v1.0) I have implemented every thing you suggested. I have struggled to get this code working for days. Thanks SOOOO much for saving my sanity.

Share this post


Link to post
Share on other sites