0

Aim: Well, What I am trying to do is to store the names in an array and scores scored in another array. Then Write them into the file. When the program is ran again, I will like to read the stored names and scores, add the new name and score, sort them by highest score and write them into the file.

Background: THe program is a simple guessing game, you enter your name and you score points after guessing. the points are to be stored corresponding to your name in the file.
They are then to be stored and displayed sorted when ever some one runs the game.

Problem: I am using the below code but when I am sorting the names and scores the name string is populated in a manner which I am not understanding...
If you run the program, it will work as intended but when you will run it second time, the sorting block of code is acting in a manner which I don't want!
a break point before the sorting block and watching the name and hscores array will clarify what I am saying..

I am a newbie programmer and also new to the forums, pardon my mistakes if I make and guide me...
Thanks....


Code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

void main()
{
	int rnum=0;
	int gnum=0;
	time_t t;
	char name[6][4]={"\0\0\0\0","\0\0\0\0","\0\0\0\0","\0\0\0\0","\0\0\0\0","\0\0\0\0"};
	int rscore=0;
	int tscore=0;
	srand(t);


	clrscr();


	printf("\t\tWelcome to the Guessing Game!");
	printf("\n\nThe of the game is to guess a secret number");
	printf("\nYou will be given 5 chances to guess the number");
	printf("\nYou will get 10 points if guessed in 1st chance,");
	printf("\nPoints will deduce in successive turns");
	printf("\nThe game will have 5 rounds");
	printf("\nThe Total points achieved will be your Final Score");

	printf("\n\nThe Games Starts Now!\n\n");
	printf("Please Enter Your Name: [4 Characters Only]	");

	gets(name[5]);

	for(int r=0;r<5;r++)
	{
		printf("\n\nCommencing Round %d",r+1);
		printf("\nPress any key to proceed...");
		rscore=0;
		getch();
		rnum = 55;//(int)(rand()%100)+1;
		for(int i=0;i<5;i++)
		{
			clrscr();
			printf("\nRound %d",r+1);
			printf("\nTurn %d",i+1);
			fflush(stdin);
			printf("\nEnter Your Guessed Number: ");
			scanf("%d",&gnum);

			if(rnum==gnum)
			{
				printf("\nCongratulations!! You figured out the Secret Number");
				switch(i+1)
				{
					case 1:
						rscore=20;
						printf("\n\nYou Score %d points",rscore);
						break;
					case 2:
						rscore=18;
						printf("\n\nYou Score %d points",rscore);
						break;
					case 3:
						rscore=16;
						printf("\n\nYou Score %d points",rscore);
						break;
					case 4:
						rscore=14;
						printf("\n\nYou Score %d points",rscore);
						break;
					case 5:
						rscore=12;
						printf("\n\nYou Score %d points",rscore);
						break;
					default:
						rscore=0;
						printf("\n\nSorry!");
				}
				break;
			}
			else if (rnum>gnum)
			{
				printf("\nOops!! Sorry The Secret Number is greater than your guessed number");
			}
			else if(rnum<gnum)
			{
				printf("\nOops!! Sorry The Secret Number is less than your guessed Number");
			}

			printf("\n\nPress any key to continue..."); getch();
		}

		if(rscore==0)
		{
			printf("\n\nSorry You were not able to guess the secret number!!");
		}

		tscore+=rscore;
	}

	printf("\n\nYour Total Score is %d",tscore);


	//Reading Highest Scores
	FILE *fprscore;
	fprscore=fopen("C:\\BIN\\hscore.txt","r");
	if(fprscore==NULL)
	{
		//printf("Cannot Open The File");
		fprscore=fopen("C:\\BIN\\hscore.txt","a");
		if(fprscore==NULL)
		{
		printf("Cannot Create File");
		}
		fclose(fprscore);
		fprscore=fopen("C:\\BIN\\hscore.txt","r");
	}

	//High Scores Array
	int temp=0;
	char tempname[4]="\0\0\0\0";
	int hscores[6]={00,00,00,00,00,00};

	for(int i=0;i<5;i++)
	{
		fscanf(fprscore,"%4s",&name[i]);
		fscanf(fprscore,"%2d",&hscores[i]);
	}

	fclose(fprscore);
	hscores[5]=tscore;

//Sorting the SCORES
	int k=1;
	while (k>0)
	{
		k=0;
		for(int i=0;i<5;i++)
		{
                        //Here is where the problem starts...
//PROBLEM PROBLEM PROBLEM PROBLEM
			if (hscores[i]<hscores[i+1])
			{
				temp=hscores[i];
				strcpy(tempname,name[i]);
				strcpy(name[i],"\0\0\0\0");  //addition
				hscores[i]=hscores[i+1];
				strcpy(name[i],name[i+1]);
				strcpy(name[i+1],"\0\0\0\0"); //addition
				hscores[i+1]=temp;
				strcpy(name[i+1],tempname);
				k=1;
			}
		}
	}


	printf("\n\n");

	//Storing Highest Scores
	FILE *fpscore;
	fpscore=fopen("C:\\BIN\\hscore.txt","w");
	if(fpscore==NULL)
	{
		printf("Cannot Open The File");
	}

	for(i=0;i<5;i++)
	{
		printf("%d\t%4s\t%d\n",i+1,name[i],hscores[i]);
		fprintf(fpscore,"%4s",name[i]);
		fprintf(fpscore,"%2d",hscores[i]);
	}


	
	fclose(fpscore);
	getch();
}
1
Contributor
1
Reply
3
Views
7 Years
Discussion Span
Last Post by saad749
0

Can Some One Just Explain me what is happening in the sorting block when we enter 4 characters in the name...... I just can't understand how when I am defining the the string to be 4 characters long it becomes more than 4 characters long..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.