Code:
#include<stdio.h> #include<stdlib.h>
#include<windows.h>
#include<math.h>
#include<string.h>
struct user
{
char username[10];
long password;
int *money = NULL;
int i;
};
void create(char *file);
int find(struct user *person, char *file);
int main(int argc, char *argv[])
{
int flag1 = 0, flag2, mode = 0;
struct user person;
FILE *fptr;
char file[10] = "admin.dat";
if (argc > 1)
{
strcpy(file, argv[1]);
}
fptr = fopen(file, "ab");
if (fptr == NULL)
{
printf("\n\nThere was a problem opening the file, please restart the program\n\n");
exit(0);
}
fclose(fptr);
while (flag1 == 0)
{
printf("Enter 1 to create new account, 2 to log in, 3 to exit.\n");
scanf("%d", mode);
switch (mode)
{
case 1: {int i = 0;
create(file); }
break;
case 2: find(&person, file);
break;
case 3: {flag1 = 1; }
break;
}
int betnumber, betmoney;
int m = 0, n = 36;
int r;
int *money = NULL;
int i;
char board[3][13] = { { ' ', 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36 },
{ 0, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35 },
{ ' ', 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34 } };
printf("Game start!");
int a, b;
for (a = 0; a<3; a++)
{
for (b = 0; b<13; b++)
printf("%d", board[a][b]);
printf("\n");
}
printf("Enter the number you choose.\n");
scanf("%d", &betnumber);
printf("Enter the money you use for betting.\n");
scanf("%d", &betmoney);
int acmoney;
acmoney = money[i];
while (betmoney>acmoney)
{
printf("You money is not enough. Your left money is:%d.\n", &acmoney);
printf("Please enter the money you use for betting.\n");
scanf("%d", &betmoney);
}
acmoney = acmoney - betmoney;
r = rand() % (n - m + 1) + m;
if (betnumber = r)
{
printf("You Win!\n");
acmoney = acmoney + 37 * betmoney;
i = i++;
acmoney = money[i];
}
else if (betnumber != r)
{
printf("You lose.\n");
i = i++;
acmoney = money[i];
}
printf("Continue or exit the game(0/1)?");
scanf("%d", &flag1);
while (money == 0)
{
i = 1;
printf("You have used all of your money.\n");
printf("Enter 1 to finished the game.\n");
}
}
}
void create(char *file)
{
struct user person;
char username[10];
long password;
FILE *fptr;
int *money = NULL;
int i=0;
fflush(stdin);
printf("Please enter the user name(less than 10 letters)\n");
scanf("%s", &username);
while (username == 0 || strlen(username)>10)
{
printf("Please make sure the name has less than 10 characters.\n");
printf("Please enter the user name(less than 10 letters)\n");
}
fflush(stdin);
printf("Please enter the password(has to be 6 numbers).\n");
scanf("%li", &password);
while (password == 0)
{
printf("The password has to be 6 numbers).\n");
}
fflush(stdin);
while (i == 0){
money[1] = 1000;
}
while (i != 0)
{
money[i];
}
fwrite(&person, sizeof(struct user), 1, fptr);
fclose(fptr);
}
int find(struct user *person, char *file)
{
FILE *fptr;
char username[10];
long password;
int *money = NULL;
int i;
fflush(stdin);
printf("Please enter your user name.\n");
scanf("%s", &username);
printf("Please enter your password.\n");
scanf("%d", &password);
while (username == 0 || strlen(username)>10 || password == 0)
{
printf("Please try again and make sure the names are seperated by a space and both are less than 10 characters long.\n");
fflush(stdin);
system("cls");
printf("Please enter the username and password.\n");
}
fflush(stdin);
fptr = fopen(file, "rb");
if (fptr == NULL)
{
printf("\n\nThere was a problem opening the file, please restart the program\n\n");
exit(0);
}
else
{
while (fread(person, sizeof(struct user), 1, fptr) == 1 && ((strcmp(person->username, username))))
{
continue;
}
}
fclose(fptr);
if (!(strcmp(person->username, username)))
{
return 0;
}
else
{
return 1;
}
}