Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

New to programming ..need knowledge


  • Please log in to reply

#1
mysterpaco

mysterpaco

    New Member

  • Member
  • Pip
  • 8 posts
I am writing a program have taken bits and pieces from books and tutoials but i am getting a program crash when the program calls SetPlace within another function. I think I may be really confusing the passing of pointers I am not sure. Also when I go to the choice to read the file I open it is printing addresses so I went ahead and created the file first and try to read from it but i dont think i had the format right for reading the file because it would all read into the first structure element. So my problem is reading the file just right and I get a reading error crash/no call stack break when i go into that SetPlace function .....any help would be appreciated i keep trying to go throught these tutorials but no such luck yet.....here is code


#include <stdio.h> /*Created by: James Hayes*/
#include <conio.h> /*Set necessary library headers for project*/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#define MAX 5 /*MAX is a macro constant we can use that does not need to be changed to use as a value in the program*/
#define MAXPLAY 10

struct player {
char name[30];
int score;
};

typedef struct player Player;


void Show_menu(); /* function prototype to call a menu for end user*/
char Get_choice(); /* function prototype to return uppercase character choice from end user*/
void Count_Change(); /* function prototype to display a message pertaining to end user choice*/
void Play_HighLow(player *game,char *Name); /* function prototype to display a message pertaining to end user choice*/
void LoadPlayer(player *local,int size);
void Initialize(player *game);
void Print( player *game);
void Get_Name(player *game);
int SetPlace(player *local,int hold);
void save_array(struct player *local,int size);


void Summary(int *E1ptr,int *E2ptr,int *E3ptr,char *E4);

int main(void)
{
int counter1=0;
int counter2=0;
int inttotal=0;
char Name[30];



player game[MAXPLAY];








char MCharacter; /*initialize variable to hold menu character*/

do
{
Show_menu();
MCharacter = Get_choice();

switch (MCharacter){



case 'A':
Get_Name(game);

break;

case 'B':
Play_HighLow(game,Name);
counter1++;
break;

case 'C':
Count_Change();
counter2++;
break;

case 'D':
LoadPlayer(game,sizeof(game));
Print(game);
break;

save_array(game,sizeof(game));



}
}while(MCharacter != 'Q');



return 0;
} /*Take end user choice displaying one of two displays as long as character entry is valid*/




void Show_menu() /*Clear screen ready for new output to be displayed*/
{
system ("cls");
printf("Select from the following options:\n");
printf("A. Enter Player\nB. Play_HighLow\nC. Play Count Change Game\nD. High Players\nQ. Quit Program\n"); /*display options to end user*/

}

char Get_choice()
{
char FChar;

do
{
FChar = toupper(_getch());

} while (!strchr("ABCDQ",FChar));

return FChar;
}


void Count_Change()
{

char Answer[11]; /*Declare variables to hold integer after atoi function changes the input string*/
int HalfDollar;double HalfTotal; /*Declare one char array that can be used over and over to get the users input*/
int Quarter; double QuarterTotal;
int Dime; double DimeTotal;
int Nickel; double NickelTotal;
int Penny; double PennyTotal;
int CoinTotal; double MoneyTotal;





system ("cls"); /*List all the print statements to gather and store needed information from the user*/
printf("Enter the number of half dollars:");
gets(Answer);
HalfDollar = atoi(Answer);
printf("Enter the number of quarters:");
gets(Answer);
Quarter = atoi(Answer);
printf("Enter the number of dimes:");
gets(Answer);
Dime = atoi(Answer);
printf("Enter the number of nickels:");
gets(Answer);
Nickel = atoi(Answer);
printf("Enter the number of pennies:");
gets(Answer);
Penny = atoi(Answer);


HalfTotal = HalfDollar * .5; /*Take all the integer variables and multiply them by necessary numbers to reach monetary values*/
QuarterTotal = Quarter * .25;
DimeTotal = Dime * .1;
NickelTotal = Nickel * .05;
PennyTotal = Penny * .01;
CoinTotal = HalfDollar + Quarter + Dime + Nickel + Penny;
MoneyTotal = HalfTotal + QuarterTotal + DimeTotal + NickelTotal + PennyTotal;


printf("\n\n\nYou have entered %d half dollars for a total of: $%6.2f",HalfDollar,HalfTotal); /*Display information gathered and calculated back to the user*/
printf("\nYou have entered %d quarters for a total of: $%6.2f",Quarter,QuarterTotal);
printf("\nYou have entered %d dimes for a total of: $%6.2f",Dime,DimeTotal);
printf("\nYou have entered %d nickels for a total of: $%6.2f",Nickel,NickelTotal);
printf("\nYou have entered %d pennies for a total of: $%6.2f",Penny,PennyTotal);
printf("\n\n\nYou have entered %d coins for a total of: $%6.2f",CoinTotal, MoneyTotal);


printf ("\n\n\n\n......Press any key to return to the main menu");
_getch();



}

void Play_HighLow(struct player *game,char *NewName)
{
int magic; /*Initialize three variables one for random number, one for tries, one for user input*/
int guess;
int tries = 0;
int row=0;









//printf("%30s",Name);debug
//getch();





srand((unsigned)(time( NULL )) ); /*Seed random number and set the scale to 5*/
magic = rand()%MAX;


system ("cls");
/* printf("magic:%d\n ",magic); this line can be used for debugging..take off comments to test*/
printf("Guess the random number from 0 to %d\n",MAX);
do
{

printf("guess a number: "); /*Run a loop until user gets random number choice right letting him know if he is too high or low*/
scanf("%d",&guess);
if(guess == magic)
{
printf("\n\nRight..... Way to Go!!!");
printf(" %d is the magic number\n", magic);
}
else if (guess > magic)
printf(".. Wrong .. Too High\n\n");
else
printf(".. Wrong .. Too Low\n\n");
tries++;



} while(guess != magic);
printf("\n\nYou took %d tries to get it right.\n\n", tries);
printf("%d\n",row);
printf("%d",tries);
printf("%30s",game[row].name,NewName);


getch();

row=SetPlace(game,tries);
printf("%d\n",row);
game[row].score=tries;
strcpy(game[row].name,NewName);


printf("\n\nPress any key to return to Main Menu");


getch();
}
void Summary(int *E1ptr,int *E2ptr,int *E3ptr,char *E4)
{
system("cls");
int answer;int answer2;int answer3;double answer4;

answer=*E1ptr;
answer2=*E2ptr;
answer3=*E3ptr;
answer4=(float)answer/answer2;


printf("%s",E4);
printf("\n\nYou played High/Low %d times\n",answer2);
printf("\n\nYour average is %.2f",answer4);
printf("\n\nYou played Count Change %d times",answer3);



getch();
}

void LoadPlayer(player *local,int size)
{
int i=0;
FILE *fptr;

if ((fptr = fopen("top10.dat","rb"))==NULL)
{
system("CLS");
printf("File does not exists...initializing new array...Press any key to continue");
getch();
Initialize(local);
}
else
{
i=sizeof(local);
printf("%d",i);
fread(local,sizeof(local),1,fptr);


fclose(fptr);
printf("Hello");
getch();
}
}
void Initialize(struct player *local)
{
int cur_row;
for(cur_row=0;cur_row<MAX;cur_row++)
{
local[cur_row].score=0;
strcpy(local[cur_row].name,"init");
}
printf("worked");

}
void Print(struct player *local)
{
system("cls");
int counter;
printf("\n");
//local[0].score=5;
//printf("%d", local[0].score);
for(counter=0;counter<MAXPLAY;counter++)
{

printf("Element Name is %30s\n",local[counter].name);
printf("Element Score is %6d\n",local[counter].score);


//printf("Element #%d is:%30s %6d\n"
// ,counter,local[counter].name,local[counter].score);
}
printf("\n\nPress any key");
getch();
}
int SetPlace(player *local,int hold)
{
int set_pt,incr;
printf("%d",local[set_pt].score);
printf("%d",set_pt);

for(set_pt=0;set_pt < MAXPLAY && hold > local[set_pt].score
&& local[set_pt].score!=0;set_pt++)
{
printf("%d",local[set_pt].score);
printf("%d",set_pt);
}

for(incr=MAXPLAY -1;incr >set_pt;incr--) //this for loop will make room in the array for the new data insertion
{
local[incr] = local[incr-1];
local[incr].score=local[incr-1].score;
strcpy(local[incr].name,local[incr-1].name);
printf("%d",set_pt);
getch();
}
return set_pt;
}

void Get_Name(struct player *game)
{
char NewName[30];


system("CLS");
printf("Enter your name and then press any key to return to main menu\n");
flushall();
gets(NewName);



}
void save_array(struct player *local,int size)
{
FILE *fptr;


if ((fptr = fopen("top10.dat","wb"))==NULL)
{
system("CLS");
printf("File write error...Press any key to terminate");
getch();
exit(0);
}
else
{
fwrite(local,size,1,fptr);
fclose(fptr);
}
}
  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
There is an awful lot of code here. Could you isolate the problem a little better? Or, start with a smaller project so we can help you. Finding bugs in code this long without having any idea what it is you are trying to do is difficult at best. I can get your code to compile if I comment out the flushall() line, which is not an ANSI C function. What exactly do you want this to do, and what is it doing wrong?
  • 0

#3
mysterpaco

mysterpaco

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
I apologize for not commenting back sooner I have been very busy with moving...what a drag....Like I said it was a template not exact usage I was trying to implement for a project and instead of passing the functions arrays i was just passing the pointer which wasn't working. I was trying to keep track of players and scores within a game. It works now i appreciate your response I will definitely post any other discrepancies in the future...........thanks
  • 0

#4
TomWij

TomWij

    Member

  • Member
  • PipPipPip
  • 125 posts
#include <stdio.h> /*Created by: James Hayes*/

#include <conio.h> /*Set necessary library headers for project*/

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

#include <time.h>

#define MAX 5 /*MAX is a macro constant we can use that does not need to be changed to use as a value in the program*/

#define MAXPLAY 10



struct player {

	char name[30];

	int score;

};



typedef struct player Player;



void Show_menu(); /* function prototype to call a menu for end user*/

char Get_choice(); /* function prototype to return uppercase character choice from end user*/

void Count_Change(); /* function prototype to display a message pertaining to end user choice*/

void Play_HighLow(player *game,char *Name); /* function prototype to display a message pertaining to end user choice*/

void LoadPlayer(player *local,int size);

void Initialize(player *game);

void Print( player *game);

void Get_Name(player *game);

int SetPlace(player *local,int hold);

void save_array(struct player *local,int size);



void Summary(int *E1ptr,int *E2ptr,int *E3ptr,char *E4);



int main(void)

{

	int counter1=0;

	int counter2=0;

	int inttotal=0;

	char Name[30];



	player game[MAXPLAY];



	char MCharacter; /*initialize variable to hold menu character*/



	do

	{

		Show_menu();

		MCharacter = Get_choice();



		switch (MCharacter){



case 'A':

	Get_Name(game);



	break;



case 'B':

	Play_HighLow(game,Name);

	counter1++;

	break;



case 'C':

	Count_Change();

	counter2++;

	break;



case 'D':

	LoadPlayer(game,sizeof(game));

	Print(game);

	break;



	save_array(game,sizeof(game));

		}

	}while(MCharacter != 'Q');



	return 0;

} /*Take end user choice displaying one of two displays as long as character entry is valid*/



void Show_menu() /*Clear screen ready for new output to be displayed*/

{

	system ("cls");

	printf("Select from the following options:\n");

	printf("A. Enter Player\nB. Play_HighLow\nC. Play Count Change Game\nD. High Players\nQ. Quit Program\n"); /*display options to end user*/



}



char Get_choice()

{

	char FChar;



	do

	{

		FChar = toupper(_getch());



	} while (!strchr("ABCDQ",FChar));



	return FChar;

}



void Count_Change()

{

	char Answer[11]; /*Declare variables to hold integer after atoi function changes the input string*/

	int HalfDollar;double HalfTotal; /*Declare one char array that can be used over and over to get the users input*/

	int Quarter; double QuarterTotal;

	int Dime; double DimeTotal;

	int Nickel; double NickelTotal;

	int Penny; double PennyTotal;

	int CoinTotal; double MoneyTotal;



	system ("cls"); /*List all the print statements to gather and store needed information from the user*/

	printf("Enter the number of half dollars:");

	gets(Answer);

	HalfDollar = atoi(Answer);

	printf("Enter the number of quarters:");

	gets(Answer);

	Quarter = atoi(Answer);

	printf("Enter the number of dimes:");

	gets(Answer);

	Dime = atoi(Answer);

	printf("Enter the number of nickels:");

	gets(Answer);

	Nickel = atoi(Answer);

	printf("Enter the number of pennies:");

	gets(Answer);

	Penny = atoi(Answer);



	HalfTotal = HalfDollar * .5; /*Take all the integer variables and multiply them by necessary numbers to reach monetary values*/

	QuarterTotal = Quarter * .25;

	DimeTotal = Dime * .1;

	NickelTotal = Nickel * .05;

	PennyTotal = Penny * .01;

	CoinTotal = HalfDollar + Quarter + Dime + Nickel + Penny;

	MoneyTotal = HalfTotal + QuarterTotal + DimeTotal + NickelTotal + PennyTotal;



	printf("\n\n\nYou have entered %d half dollars for a total of: $%6.2f",HalfDollar,HalfTotal); /*Display information gathered and calculated back to the user*/

	printf("\nYou have entered %d quarters for a total of: $%6.2f",Quarter,QuarterTotal);

	printf("\nYou have entered %d dimes for a total of: $%6.2f",Dime,DimeTotal);

	printf("\nYou have entered %d nickels for a total of: $%6.2f",Nickel,NickelTotal);

	printf("\nYou have entered %d pennies for a total of: $%6.2f",Penny,PennyTotal);

	printf("\n\n\nYou have entered %d coins for a total of: $%6.2f",CoinTotal, MoneyTotal);



	printf ("\n\n\n\n......Press any key to return to the main menu"); 

	_getch();

}



void Play_HighLow(struct player *game,char *NewName)

{

	int magic; /*Initialize three variables one for random number, one for tries, one for user input*/ 

	int guess;

	int tries = 0;

	int row=0;



	//printf("%30s",Name);debug

	//getch();



	srand((unsigned)(time( NULL )) ); /*Seed random number and set the scale to 5*/

	magic = rand()%MAX;



	system ("cls");

	/* printf("magic:%d\n ",magic); this line can be used for debugging..take off comments to test*/

	printf("Guess the random number from 0 to %d\n",MAX);

	do 

	{

		printf("guess a number: "); /*Run a loop until user gets random number choice right letting him know if he is too high or low*/

		scanf("%d",&guess); 

		if(guess == magic)

		{

			printf("\n\nRight..... Way to Go!!!");

			printf(" %d is the magic number\n", magic);

		}

		else if (guess > magic)

			printf(".. Wrong .. Too High\n\n");

		else

			printf(".. Wrong .. Too Low\n\n");

		tries++;

	} while(guess != magic);



	printf("\n\nYou took %d tries to get it right.\n\n", tries);

	printf("%d\n",row);

	printf("%d",tries);

	printf("%30s",game[row].name,NewName);



	getch();



	row=SetPlace(game,tries);

	printf("%d\n",row);

	game[row].score=tries;

	strcpy(game[row].name,NewName);



	printf("\n\nPress any key to return to Main Menu"); 



	getch();

}

void Summary(int *E1ptr,int *E2ptr,int *E3ptr,char *E4)

{

	system("cls");

	int answer;int answer2;int answer3;double answer4;



	answer=*E1ptr;

	answer2=*E2ptr;

	answer3=*E3ptr;

	answer4=(float)answer/answer2;



	printf("%s",E4);

	printf("\n\nYou played High/Low %d times\n",answer2);

	printf("\n\nYour average is %.2f",answer4);

	printf("\n\nYou played Count Change %d times",answer3);



	getch();

}



void LoadPlayer(player *local,int size)

{

	int i=0;

	FILE *fptr;



	if ((fptr = fopen("top10.dat","rb"))==NULL)

	{

		system("CLS");

		printf("File does not exists...initializing new array...Press any key to continue");

		getch();

		Initialize(local);

	}

	else

	{

		i=sizeof(local);

		printf("%d",i);

		fread(local,sizeof(local),1,fptr);



		fclose(fptr);

		printf("Hello");

		getch();

	}

}

void Initialize(struct player *local)

{

	int cur_row;

	for(cur_row=0;cur_row<MAX;cur_row++)

	{

		local[cur_row].score=0;

		strcpy(local[cur_row].name,"init");

	}

	printf("worked");



}

void Print(struct player *local)

{

	system("cls");

	int counter;

	printf("\n");

	//local[0].score=5;

	//printf("%d", local[0].score);

	for(counter=0;counter<MAXPLAY;counter++)

	{

		printf("Element Name is %30s\n",local[counter].name);

		printf("Element Score is %6d\n",local[counter].score);



		//printf("Element #%d is:%30s %6d\n"

		// ,counter,local[counter].name,local[counter].score);

	}

	printf("\n\nPress any key");

	getch();

}

int SetPlace(player *local,int hold)

{

	int set_pt,incr;

	printf("%d",local[set_pt].score);

	printf("%d",set_pt);



	for(set_pt=0;set_pt < MAXPLAY && hold > local[set_pt].score && local[set_pt].score!=0;set_pt++)

	{

		printf("%d",local[set_pt].score);

		printf("%d",set_pt);

	}



	for(incr=MAXPLAY -1;incr >set_pt;incr--) //this for loop will make room in the array for the new data insertion

	{

		local[incr] = local[incr-1];

		local[incr].score=local[incr-1].score;

		strcpy(local[incr].name,local[incr-1].name);

		printf("%d",set_pt);

		getch();

	}

	return set_pt;

}



void Get_Name(struct player *game)

{

	char NewName[30];



	system("CLS");

	printf("Enter your name and then press any key to return to main menu\n");

	flushall();

	gets(NewName);

}



void save_array(struct player *local,int size)

{

	FILE *fptr;



	if ((fptr = fopen("top10.dat","wb"))==NULL)

	{

		system("CLS");

		printf("File write error...Press any key to terminate");

		getch();

		exit(0);

	}

	else

	{

		fwrite(local,size,1,fptr);

		fclose(fptr);

	}

}

  • 0

#5
TomWij

TomWij

    Member

  • Member
  • PipPipPip
  • 125 posts
Threw the code trough an IDE for readibility, besides that the code looks very hard to understand.

It's missing comments and the functions are too big...
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP