PDA

View Full Version : Programming Problem.



fitcovboy
08-12-2006, 07:07 AM
The problem is with file handling with c programming.

Ok here are the program specifications:
To produce a program which will allow the user to play the dice game chicago. On starting the program a permanent file of 10 player accounts will be held opened and then copied from RAM into an array of 10 accounts.

An account record consists of:

An account holders name- this can be up to 19 letters long.
An account password- which should contain 8 alphanumeric characters.
An account balance- this field will store the number of points which a user has accumulated during all play sessions.
Number of games played
Average points played per game.

A welcome screen remains on the screen untill the enter key is pressed. The customer is then prompted to type in their name. They are then asked to type in a password. The name and password are checked to see that there is an appropriate player record

If the details are incorrect the user returns to the welcome screen.

If the details entered are appropriate a menu appears offering the following services:

1) View Scoreboard (showung averages for all players)
2) Help - a screen (or screens) to explain the game rules.
2) Play Chicago dice game
4) Exit

The relevant choice is then carried out. After this choice has been completed the user is returned to the menu screen. This process is repeated until the user chooser option 4.

If the user enters option 4 updated player details will be saved to the permanent data file before the program closes.

The Game

There are two players (human vs computer)

To play: There are eleven rounds numbered 2 - 12. In each round the player tries to roll and score the number of the round, the numbers being combinations possible with 2 dice.

If a player throws the correct number for that round they score 1 point. If they throw any other number they dont score. The highest total after 11 rounds wins the game.

On completion of the game the players number of games played and average points scored statistics are updated in the tempory array of records. Ok from this and reading primitive documention i managed to get my hands on, i think i needed a second program to input the 10 usernames and passwords so i did this code:

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

/*create a permanent file for 10 user accounts and scores to be held*/

#define FileName "C:\\Users.dat"

struct rec
{
char Username[19];
char Password[8];
int Score;
};

struct rec GetRec();
void WriteRec(FILE *file_handle, struct rec User);

struct rec UserRec;
FILE *UserFile;
char Answer;


main()


{/*start of the main program*/

UserFile = fopen(FileName,"a");
do
/*loop starts*/

{/*start of loop*/
UserRec = GetRec();
WriteRec(UserFile, UserRec);

/*promt to add another record or quit*/
printf("Add another user to the file (Y/N)");

/*wait for entry y/n*/
Answer = getche();

/*loop ends*/
}/*end of loop*/

/*carry out the choice*/
while (toupper(Answer)=='Y');
fclose(UserFile);


}/*end of the main program*/



struct rec GetRec()
{
struct rec User;
clrscr();

/*welcome screen to add new users*/
printf("\n\nPlease enter the new user details:");

/*A prompt appears asking for the input of the user name*/
printf("\n\nPlease enter the new username:");

/*wait for key press*/
scanf("%s",User.Username);

/*Input the users password*/
printf("\n\nPlease enter the users password:");

/*wait for key press*/
scanf("%s",User.Password);

/*Add users starting points*/
printf("\n\nPlease enter the number of starting points:");

/*wait for key press*/
scanf("%d",&User.Score);
printf("\n");

return User;
}

void WriteRec(FILE *file_handle, struct rec User)
{
char Answer;

/*promt for validation to save record*/
printf("\OK to save this record (Y/N) ");

/*wait for entry of y/n*/
Answer = getche();

if (toupper(Answer) == 'Y')

/*the files are saved to the permanent file*/
fwrite(&User, sizeof(User), 1, file_handle);
printf("\n");
}


This works for the input fine, so below the program code (please ignore the fact it is the wrong game, this was for testing purposes and is not the problem):

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

#define FileName "C:\\Users.dat"

struct rec
{
char Username[19];
char Password[8];
int Score;
};

struct rec GetRec();


struct rec UserRec;
FILE *UserFile;

char choice, reply, username, password;
int result, die1, die2, i,score,loop1,loop2,numsin[6],ranInts[7];

void welcome();
void user();
void menu();
void getchoice();
void dochoice();
void scores();
void help();
void game();
void getnums();
void draw();
void compare();
void message();

void main()
{
UserFile=fopen(FileName,'r+');
fill_array();

do
{
welcome();
user();
menu();
getchoice();
dochoice();
}while (choice!='D');

}

void welcome()
{
clrscr();
gotoxy(24,14);
printf("Welcome to the game of Chicago!");
gotoxy(26,16);
printf("Please press a key to enter.");
getch();
}




void user()
{
clrscr();
printf("Please enter your username:\n\n");
scanf("%c",&username);
if(username==UserFile);
printf("\n\nPlease enter your password:\n\n");
scanf("%c",&password);
getch();
}




void menu()
{
clrscr();
gotoxy(12,10);
printf("MENU");
gotoxy(12,12);
printf("A:View Scoreboard.");
gotoxy(12,13);
printf("B:View help files.");
gotoxy(12,14);
printf("C:Play Chicago dice game.");
gotoxy(12,15);
printf("D:Exit.");

}

void getchoice()
{
gotoxy(12,17);
printf("Enter your choice ...");
choice = toupper(getch());
}

void dochoice()
{
switch (choice)
{
case 'A':scores();
break;
case 'B':help();
break;
case 'C':game();
break;
case 'D':printf("\n\nEXITING PROGRAM");
break;
default: gotoxy(12,19);
printf("\n\nInvalid choice");
gotoxy(12,21);
printf("Press a key to return to the menu");
getch();
}
}

void scores()



{
UserFile = fopen(FileName,"r");
clrscr();
printf("\nPlayers Scoreboard\n");

while (fread(&UserRec, sizeof(UserRec),1,UserFile))
{
printf("\n%12s%4d",UserRec.Username,UserRec.Score);
}
printf("\n\nEnd of players Scoreboard");
fclose(UserFile);

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

}

void help()



{
clrscr();
printf("\n\nThe Game explained:");

printf("\n\nChicago is a two player game, this consists of you vs the computer.");



printf("\n\nThere are eleven rounds 2-12. In each round the player tries to roll and score the number");


printf(" of the round, the numbers being the combinations possible with two dice.");


printf("\n\nIf you or the computer throw the correct number in in any round then the score");

printf(" increases by one point, if any other number is thrown the score remains the same.");


printf("\n\nThe player with the highest total after the eleven rounds wins the game.");

printf("\n\nOn completion of the game, your total number of games played, and your");

printf(" average points scored are updated.");

printf("\n\nPress a key to return to the main menu.");


getch();
}


void game()


{
randomize();


do{
getnums();
draw();
score=0;
compare();
message();

printf("\nDo you want to play again y / n : ");
reply = getch();
} while (reply !='n');



}


void getnums()
{
for (i=0;i<=5;i++)

{

do
{
printf("\n\n\nPlease enter your choice (1-49) for ball %d " , i+1, "\n");
scanf("%d",&numsin[i]);
}
while ((numsin[i] < 1) ^ (numsin[i] >49));



}

}




void draw()
{


for (i=0;i<=6;i++)

{
ranInts[i]=1+random(49);
}

clrscr();
printf("LOTTERY DRAW MADE");

for (i=0;i<=5;i++)
{
printf("\n\nBall number %d is %d", i+1,ranInts[i]);
}

printf("\n\nBonus Ball is %d",ranInts[6]);
printf("\n\nPress any key to continue...");
getch();
}

void compare()
{
for (loop1=0;loop1<=5;loop1++)
{
for (loop2=0;loop2<=5;loop2++)
{
if (numsin[loop1] == ranInts[loop2])
{
score++;
}
}/*end loop2*/
}/*end loop1*/
}/*end compare*/


void message()
{

clrscr();
if (score >2)
{
printf("\n\nCONGRATULATIONS - YOU HAVE WON !!!!!");
}

if (score < 3)
{
printf("\n\nSORRY - YOU LOSE AGAIN.........");
}

getch();
}

I am having problems in the way of the users(); as i cannot work out how to compare with what is in the permanent file, or how to get it into the programs memory, also it does not allow for a password to be entered, and if you select anything other than a,b,c or d it does not return to the menu, but to the welcome screen, i am probably over looking lots as i have been doing it for the last 2 days lol.

omgDAN!
08-12-2006, 07:54 AM
Try the coding forum?

Tomm
08-12-2006, 08:20 AM
Good look finding anyone who knows C here. I recommend you try a specialist C programming forum.

Want to hide these adverts? Register an account for free!