Thread: I'm confused about using pointer. Please help me!

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    Post I'm confused about using pointer. Please help me!

    I want to write a roulette game. The player can create an account. This account should be a structure tpye variable containing: username, passpword and available money(initial:$1000). Then player bets on a number(0~36) and use some monbey to bet. The computer generates a random number(0~36). If the numbers are the same, then player will win 37 times the amount he bets. All counts should be stored in a data file and accessed by the program next time.

    I'm confused about how to store the value of leftmoney in the account to make it available when user log in theri account next time. I want to user pointer but I just start learning it and still don't understand how to use it. There are too many errors and I don't know how to correct them. Help me please!


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

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Sakuma View Post
    There are too many errors and I don't know how to correct them. Help me please!
    This is the result of typing too much code at once without compiling. The trick is to write a little code, compile, and test - then move on to the next bit. Develop the program incrementally in little steps, testing along the way.

    After fixing one error, I got 18 warnings when I compiled your code. If you compiled more frequently, you'd get less errors at a time and fixing them would be easier.

    You should also work from a plan. This means first developing a list of logical steps needed to solve the problem, which are then translated into code.

    There are too many problems for me to pick through; some that stand out during a quick review include:


    • Code:
      fflush(stdin);
      Flushing the input buffer with fflush results in undefined behavior: FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    • Code:
      while (i == 0){
          money[1] = 1000;
      }
      Where is "i" updated in this loop? If "i" is not zero when the loop begins, the loop will never end.

    • Code:
          char username[10];
      
          scanf("%s", &username);
      
          while (username == 0 || strlen(username)>10)
      You should limit the input to the allowable size of the string ("%9s"). If the array is of size 10, then the longest the string can be is 9 chars long - strlen does not count the null character. If "strlen of username > 10", your buffer would be overflowed. When would "username" be equal to zero?


    I suggest you start over, following the advice here: A development process

    Break the problem down into small pieces, and solve one at a time. Maybe start a new side project to develop your "write to file" and "read from file" functions - when tested and complete, you can transfer them over to your actual project.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-08-2013, 12:50 PM
  2. Replies: 4
    Last Post: 02-28-2010, 10:35 PM
  3. getting confused with pointer to a pointer - 2d array
    By help_seed in forum C++ Programming
    Replies: 0
    Last Post: 12-02-2009, 09:48 PM
  4. Pointer confused... :-(
    By hanez in forum C Programming
    Replies: 5
    Last Post: 08-12-2005, 09:52 AM
  5. confused with scope&pointer
    By cman in forum C Programming
    Replies: 7
    Last Post: 03-23-2003, 03:39 AM

Tags for this Thread