#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);
}
}