Advertisement

WHAT is up with this?

Started by February 21, 2002 12:19 AM
2 comments, last by Draxis 22 years, 9 months ago
I have this simple little program that does nothing but creates ''saved files'' of entered data. The flow is pretty self explanitory, however, a problem arises at the line reading scanf("%c", &choose2); is reached. It seems to bypass this scanf altogether and continues on through the loop. Why?! It works if I use an int instead of a character for the scanf and condition. But why should I have to? Thanks ahead of time
  
#include <iostream.h>
#include <stdio.h>

typedef struct FileData
{
	int a;
	int b;
	char string[80];
}data, *data_ptr;

FILE *SaveState;
FILE *LoadState;
char FileName[80];
const char SIGNATUREW[5] = "SAVE";
char SIGNATURER[5];
char choose;
char choose2;

int main(void)
{
	data Save;
	data Load;
	
	while(choose != ''q'')
	{
		printf("l: Load File\nq: Quit\n");
		scanf("%c", &choose);
		if(choose == ''l'')
		{
			printf("Enter filename: ");
			scanf("%s", FileName);
			LoadState = fopen(FileName, "rb");
			if(LoadState == NULL)
			{
				printf("File does not exist. Make new? y/n\n");
				scanf("%c", &choose2);
				if(choose2 == ''y'')
				{
					printf("Enter Interger: ");
					scanf("%d", &Save.a);
					printf("Enter Interger: ");
					scanf("%d", &Save.b);
					printf("Enter Word: ");
					scanf("%s", Save.string);

					SaveState = fopen(FileName, "wb");
					if(SaveState == NULL)
					{
						printf("Could not create new file.\n");
					};
					fwrite(SIGNATUREW, sizeof(const char), sizeof(SIGNATUREW), SaveState);
					fwrite(&Save, sizeof(FileData), 1, SaveState);
					fclose(SaveState);
				};
			}
			else
			{
				fread(SIGNATURER, sizeof(char), 5, LoadState);
				if(*SIGNATURER == *SIGNATUREW)
				{
					fread(&Load, sizeof(FileData), 1, LoadState);
				}
				else
				{
					printf("Could not fild valid Save file!\n");
				};
				fclose(LoadState);
				printf("\n\nLoad.a: %d\n", Load.a);
				printf("Load.b: %d\n", Load.b);
				printf("Load.string: %s\n\n\n", Load.string);
			};
		};
	};
	return 0;
}
  
It seems like there''re misc values being stored in choose and choose2.
Advertisement
I would initalizing your variables. I know it sounds simple but it normally the simple stuff that causes all the trouble.
No, no, intializing the variables didn''t help.
I''ve tried that.
I probably need to flush an io buffer or SOMETHING. I dunno.

This topic is closed to new replies.

Advertisement