Thread: Game log in problem.

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    34

    Game log in problem.

    Hell All;
    The problem that I am working on is the log in page for playing a game.
    It is an assignment not a real game page. The complete instructions are too long to list. The problem I am having at this point is that after a new member signs up the program ends even though I have it inside a while loop whose conditions for termination have not been met. Here is what I have:

    Programevelop User Management Program ver. 1.0. Once the program starts, twooptions are displayed like below. Once user chooses the sign up option, usercan sign up with ID and PASS. Once user choose log in option, user can log in withvalid ID and Pass. UI (user interface) is up to developer.

    Start Program
    1. Log in
    2. Sign up
    3. Exit

    What do you want to do?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void game_choice();
    
    
    int main(void)
    {
        char id [10][20], passw [10][20];
        char temp1[20], temp2[20];
        int numMem = 0;
        int choice = 0;
        int curr_user = 0;
        int i, j, jj, k, kk, l, ll;
    
    
         
    
    
        while (choice != 3)
        {
            printf("Start Program\n");
            printf("1. Log in\n");
            printf("2. Sign up\n");
            printf("3. Exit\n");
            printf("What do you want to do? \n\n");
            scanf("%i", &choice);
            int num_users = 0;
    
    
            switch (choice)
            {
                case 1:
                    printf("Please input user ID:");
                    scanf("%s", temp1);
                    printf("Please input password:");
                    scanf("%s", temp2);
    
    
                    for (i = 0; i < 10; i++)
                        {
                            if ((strcmp(id[i], temp1) == 0) && (strcmp(passw[i], temp2) == 0))
                            {
                                curr_user = id[i];
                                printf("Welcome %s.",curr_user);
                                game_choice();
                            }
                            else
                            {
                                printf("ID or Password is incorrect. Please try again.");
                            }
                        }
                    printf("Thank you!!!");
    
    
                case 2:
                    for (i= num_users; i <= num_users; i++)
                    {
                       printf("Please input user ID:");
                       scanf("%s",id[i]);
                    }
                    for (i = num_users; i <= num_users; i++)
                    {
                        printf("Please input password:");
                        scanf("%s",passw[i]);
                    }
                    printf("Thank you!!!");
                    num_users++;
    
    
    
    
                case 3:
                    exit(0);
                    break;
            }
        }
    
    
    
    
        return 0;
    }
    
    
    void game_choice()
    {
        int choice = 0;
        
        printf("1. Rock, paper, scissor game\n");
        printf("2. Blackjack game\n");
        printf("3. Log out\n");
        
        switch (choice)
        {
            case 1:
                
            case 2:
                
            case 3:
                printf("Good Bye %s", curr_user);
                return o;
                
            
                
            
        }
        
    }
    I would be grate full for any and all help.
    Ruger06
    Last edited by Ruger06; 07-09-2014 at 09:55 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to add break to each case
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    OK, I added the break, now it goes to the next step and asks what do I want to do, but when I enter id and password it just keeps going in a loop. It never enters the if statement on line 44.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void game_choice();
    
    
    int main(void)
    {
        char mem_id [10][20], mem_passw [10][20];
        char tempid[20], temppw[20];
        int num_Mems = -1;
        int choice = 0;
        int curr_user = 0;
        int i, j, jj, k, kk, l, ll;
        int login_mem_id = -1;
    
    
        while (choice != 3)
        {
            printf("Start Program\n");
            printf("1. Log in\n");
            printf("2. Sign up\n");
            printf("3. Exit\n");
            printf("What do you want to do? \n\n");
            scanf("%i", &choice);
    
    
    
    
            switch (choice)
            {
                case 1:
                    tempid[20], temppw[20];
                    printf("Please input user ID:");
                    scanf("%s", tempid);
                    getchar();
                    printf("Please input password:");
                    scanf("%s", temppw);
                    getchar();
    
    
                    for (i = 0; i < num_Mems; i++)
                        {
                            if (strcmp(tempid, mem_id[i]) == 0 && strcmp(temppw, mem_passw[i]) == 0)
                            {
                                printf("Welcome %s.",mem_id);
                                curr_user = i;
                                login_mem_id = i;
                                break;
                            }
                            else
                            {
                                printf("ID or Password is incorrect. Please try again.");
                            }
                        }
                    printf("Thank you!!!");
    
    
                    if (login_mem_id!=-1)
                    {
                       game_choice(mem_id);
                    }
                    login_mem_id=-1;
                    break;
    
    
                case 2:
                    printf("Please input user ID:");
                    scanf("%s",mem_id[num_Mems]);
                    getchar();
    
    
                    printf("Please input password:");
                    scanf("%s",mem_passw[num_Mems]);
                    getchar();
    
    
                    printf("Thank you!!!");
                    num_Mems++;
                    break;
    
    
    
    
                case 3:
                    exit(0);
                    break;
            }
        }
        return 0;
    }
    
    
    void game_choice(int mem_id)
    {
        int choice = 0;
    
    
        printf("1. Rock, paper, scissor game\n");
        printf("2. Blackjack game\n");
        printf("3. Log out\n");
    
    
        switch (choice)
        {
            case 1:
    
    
            case 2:
    
    
            case 3:
                printf("Good Bye %s", mem_id);
                return 0;
    
    
    
    
    
    
    
    
        }
    
    
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    since you intialize your num_Mems = -1; - it means first time you choose 2 - you are writing to index -1 - which is access out of bound of your array
    While debugging your application - you should think about adding some debug outputs like
    Code:
    printf("Thank you!!!\n");
    #ifdef _DEBUG
    printf("Slot %d User[%s] Pwd[%s]\n", num_Mems, mem_id[num_Mems], mem_passw[num_Mems]);
    #endif
    to verify your code is doing what you intend it to do
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Thank you VART;
    My instructor has us doing files only so I cant use the debug feature in code blocks. Can you tell me where I can find out more about the debug features you listed in your comment? I have made several changes in my code now and almost everything works except that if you enter an incorrect ID or Password it should output "ID or Password is incorrect. Please try again." but it skips right over the else statement that does this. any suggestions.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void game_choice(void);
    
    
    
    
    int main(void)
    {
        char mem_id [10][20], mem_passw [10][20];
        char tempid[20], temppw[20];
        int num_Mems = -1; // wrong
        num_Mems=0;
        int choice = 0;
        int curr_user = 0;
        int i, j, jj, k, kk, l, ll;
        int login_mem_id = -1;
    
    
    
    
        while(1) // while loop is better
        {
    
    
    
    
            printf("Start Program\n");
            printf("1. Log in\n");
            printf("2. Sign up\n");
            printf("3. Exit\n");
            printf("What do you want to do? \n\n");
            //scanf("%i", &choice); // wrong placeholder
            scanf("%d", &choice);
    
    
    
    
            //switch (choice)
            //{
                //case 1:
            if(choice==1)
            {
                printf("Please input user ID:");
                scanf("%s", tempid);
                getchar();
                printf("Please input password:");
                scanf("%s", temppw);
                getchar();
    
    
                for (i = 0; i < num_Mems; i++)
                {
                    if (strcmp(tempid, mem_id[i]) == 0 && strcmp(temppw, mem_passw[i]) == 0)
                    {
                        printf("Welcome %s.\n",mem_id);
                        curr_user = i; // wrong
                        login_mem_id=i;
    
    
                    }
                    else if (strcmp(tempid, mem_id[i]) != 0 || strcmp(temppw, mem_passw[i]) != 0)
                    {
                        printf("ID or Password is incorrect. Please try again.");
                    }
                     break;
                }
                printf("Thank you!!!\n");
    
    
                if (login_mem_id!=-1)
                {
                   //game_choice(mem_id); // you don't know how to pass 2D char array yet. so implement game_choice procedure here not using function.
                   game_choice();
                }
                login_mem_id=-1;
            }
    
    
                //case 2:
    
    
            else if(choice==2)
            {
    
    
    
    
                printf("Please input user ID:");
                scanf("%s",mem_id[num_Mems]);
                getchar();
    
    
                printf("Please input password:");
                scanf("%s",mem_passw[num_Mems]);
                getchar();
    
    
                printf("Thank you!!!");
                num_Mems++;
    
    
            }
                //case 3:
                    //exit(0);
            else if(choice==3)
            {
                break;
            }
        }
            //}
        return 0;
    }
    
    
    //void game_choice(int mem_id) // mem_id is not integer
    void game_choice()
    {
        //int choice = 0;
        while (1)
        {
            int choice = 0;
            printf("1. Rock, paper, scissor game\n");
            printf("2. Blackjack game\n");
            printf("3. Log out\n");
    
    
            printf("What do you want to do?");
            scanf("%d", &choice);
    
    
            if (choice ==1)
            {
                printf("That game is not available at this time.\n");
                break;
            }
    
    
            if (choice == 2)
                {
                printf("That game is not available at this time.\n");
                break;
            }
    
    
            if (choice == 3)
            {
                return 0;
            }
    
    
           // switch (choice) // do not use switch
           // {
               // case 1:
    
    
               // case 2:
    
    
                //case 3:
         //           printf("Good Bye %s", mem_id); // mem_id is integer
        //            return 0;
    
    
    
    
    
    
    
    
           // }
        }
    
    
    
    
    }
    Thank you.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Ruger06 View Post
    if you enter an incorrect ID or Password it should output "ID or Password is incorrect. Please try again." but it skips right over the else statement that does this.
    you shold think a little more about your logic here...

    When shoudl try again happen?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Hello VART;
    I guess what I should have said is that if a person has not signed up yet tries to log in, the statement "ID or Password is incorrect. Please try again." never gets displayed. The (else if ) statement doesn't run, it just jumps back to the opening screen with the initial choices. Do yo have any suggestions on how to correct this.
    Respectfully,
    Ruger06

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I take from the code posted you are only going to support a single user.
    I say this because the only user that will work is the first one in your list with the code posted.

    The above is a hint on the problem/solution.

    A more direct hint is check all the user/password data before saying it is incorrect.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    The for loop in the if choice == 1 does check all user ID's and passwords.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Ruger06 View Post
    The for loop in the if choice == 1 does check all user ID's and passwords.
    Please read what the break command does; then look at the location of the break in the for loop.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Ok, I got everything working except that I need to pass the users ID to the game_choice function and I do not know how to do this. I have tried to use a pointer but I am not doing something write.

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void game_choice(char *);
    
    
    int main(void)
    {
        char mem_id [10][20], mem_passw [10][20];
        char tempid [20], temppw [20];
        int num_Mems = 0;
        int choice = 0;
        int curr_user = 0;
        int i, j, jj, k, kk, l, ll;
        int login_mem_id = -1;
        char *ID = mem_id;
    
    
        printf("Start Program\n");
    
    
        while (choice != 3)
        {
            printf("1. Log in\n");
            printf("2. Sign in\n");
            printf("3. Exit\n\n");
            printf("What do you want to do?  ");
            scanf("%d", &choice);
            printf("\n\n");
    
    
    
    
                if (choice == 1)
                {
                   if (num_Mems == 0)
                    {
                        printf("You must sign in first. Please try again.\n\n");
                        continue;
                    }
    
    
                   printf("Please input user ID:");
                   scanf("%s", &tempid);
                   getchar();
                   printf("Please input password:");
                   scanf("%s", &temppw);
                   getchar();
    
    
                   for (i = 0; i < num_Mems; i++)
                    {
                        if (strcmp(tempid, mem_id[i]) == 0 && strcmp(temppw, mem_passw[i]) == 0)
                        {
                            printf("Welcome %s.\n\n",mem_id);
                            login_mem_id=i;
                            continue;
                        }
                        else
                        {
                            printf("ID or Password is incorrect. Please try again.\n\n");
    
    
                        }
                    }
    
    
                    if (login_mem_id!=-1)
                    {
                       game_choice(*ID);
                    }
                    login_mem_id=-1;
                }
    
    
                else if(choice==2)
                {
                    printf("Please input user ID:");
                    scanf("%s",mem_id[num_Mems]);
                    getchar();
    
    
                    printf("Please input password:");
                    scanf("%s",mem_passw[num_Mems]);
                    getchar();
    
    
                    printf("Thank you!!!\n\n");
                    num_Mems++;
    
    
                }
    
    
                else if(choice==3)
                {
                    break;
                }
        }
    
    
        return 0;
    }
    
    
    void game_choice(char *ID)
    {
        int choice = 0;
    
    
        while (choice != 3)
        {
            printf("1. Rock, paper, scissor game\n");
            printf("2. Blackjack game\n");
            printf("3. Log out\n\n");
            printf("What do you want to do? ");
            scanf("%d", &choice);
            //3
            printf("\n");
    
    
            if (choice == 1)
            {
                printf("That choice is not available now.\n\n");
                continue;
            }
            else if (choice ==2)
            {
                printf("That choice is not available now.\n\n");
                continue;
            }
            else
            {
               printf("Good Bye %s", ID);
               return 0;
            }
        }
    }

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should be compiling with maximum warnings:

    Code:
    /*
    main.c||In function 'main':|
    main.c|18|warning: initialization from incompatible pointer type|
    main.c|46|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'|
    main.c|49|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'|
    main.c|72|warning: passing argument 1 of 'game_choice' makes pointer from integer without a cast|
    main.c|6|note: expected 'char *' but argument is of type 'char'|
    main.c|16|warning: unused variable 'll'|
    main.c|16|warning: unused variable 'l'|
    main.c|16|warning: unused variable 'kk'|
    main.c|16|warning: unused variable 'k'|
    main.c|16|warning: unused variable 'jj'|
    main.c|16|warning: unused variable 'j'|
    main.c|15|warning: unused variable 'curr_user'|
    main.c||In function 'game_choice':|
    main.c|137|warning: 'return' with a value, in function returning void|
    ||=== Build finished: 0 errors, 12 warnings ===|
    */


    >> warning: initialization from incompatible pointer type

    I get the sense you just added that variable in an attempt to get your program to work, without understanding the details. If you're not yet comfortable with mixing pointers and multidimensional arrays, I would advise against using them without a lot of study. Fortunately, this is not needed to solve your problem - you don't need a separate pointer variable at all.

    I need to pass the users ID to the game_choice function and I do not know how to do this.
    You have a 2D array (an array of arrays, or an array of strings in the context of your code), and you have a variable that tracks the current ID string. So why not just do:

    Code:
    game_choice(mem_id[login_mem_id]);


    >> warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]

    "scanf()" expects a pointer to a variable. That's why when you use "scanf()" to, say, read an integer, you normally see the address-of operator ('&') before the variable. When reading a string, you don't need the '&' - in this context, using the array name by itself acts as a pointer to that array.



    >> warning: passing argument 1 of 'game_choice' makes pointer from integer without a cast

    If you take the advice from the first part of this post, you won't need this variable and the problem goes away.

    Just for explanation, though, you're dereferencing the pointer in the function call. To pass a pointer to a function, you don't need the '*' before the variable name.



    >> warning: 'return' with a value, in function returning void

    If you want to return from a void function, you can just do a return (with no return value):

    Code:
    void function(void)
    {
        // ...
    
        return;
    }
    Last edited by Matticus; 07-15-2014 at 08:47 AM.

  13. #13
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    I want to thank everyone for all their help. I finally got this to work the way I needed it to. Here is the finished code.

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void game_choice(char *);
    
    
    int main(void)
    {
        char mem_id [10][20], mem_passw [10][20];
        char tempid [20], temppw [20];
        int num_Mems = 0;
        int choice = 0;
        int i;
        int login_mem_id = -1;
        char *idptr;
    
    
        printf("Start Program\n");
    
    
        while (choice != 3)
        {
            printf("1. Log in\n");
            printf("2. Sign in\n");
            printf("3. Exit\n\n");
            printf("What do you want to do?  ");
            scanf("%d", &choice);
            printf("\n\n");
    
    
    
    
                if (choice == 1)
                {
                   if (num_Mems == 0)
                    {
                        printf("You must sign in first. Please try again.\n\n");
                        continue;
                    }
    
    
                   printf("Please input user ID:");
                   scanf("%s", &tempid);
                   getchar();
                   printf("Please input password:");
                   scanf("%s", &temppw);
                   getchar();
    
    
                   for (i = 0; i < num_Mems; i++)
                    {
                        if (strcmp(tempid, mem_id[i]) == 0 && strcmp(temppw, mem_passw[i]) == 0)
                        {
                            printf("Welcome %s!!!\n\n",mem_id);
                            idptr = mem_id;
                            login_mem_id=i;
                            break;
                        }
                        else
                        {
                            printf("ID or Password is incorrect. Please try again.\n\n");
                            break;
                        }
                    }
    
    
                    if (login_mem_id!=-1)
                    {
                       game_choice(idptr);
                    }
                    login_mem_id=-1;
                }
    
    
                else if(choice==2)
                {
                    printf("Please input user ID:");
                    scanf("%s",mem_id[num_Mems]);
                    getchar();
    
    
                    printf("Please input password:");
                    scanf("%s",mem_passw[num_Mems]);
                    getchar();
    
    
                    printf("Thank you!!!\n\n");
                    num_Mems++;
    
    
                }
    
    
                else if(choice==3)
                {
                    break;
                }
        }
    
    
        return 0;
    }
    
    
    void game_choice(char *idptr)
    {
        int choice = 0;
    
    
        while (choice != 3)
        {
            printf("1. Rock, paper, scissor game\n");
            printf("2. Blackjack game\n");
            printf("3. Log out\n\n");
            printf("What do you want to do? ");
            scanf("%d", &choice);
    
    
            if (choice == 1)
            {
                printf("It is not available now.\n\n");
            }
            else if (choice ==2)
            {
                printf("It is not available now.\n\n");
    }
            else
            {
               printf("Good Bye %s!!!\n\n", idptr);
            }
        }
    }

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    /*
    main.c||In function 'main':|
    main.c|45|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'|
    main.c|48|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'|
    main.c|57|warning: assignment from incompatible pointer type|
    ||=== Build finished: 0 errors, 3 warnings ===|
    */
    You still have the issues with your "scanf()" calls.

    And you didn't really fix the pointer problem, you just re-dressed it.

  15. #15
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Do you have any suggestions on how to solve this problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. A little problem with my game
    By lalorobot in forum Game Programming
    Replies: 8
    Last Post: 06-11-2012, 02:33 PM
  3. Game problem
    By brookemay in forum Game Programming
    Replies: 6
    Last Post: 05-29-2011, 10:36 AM
  4. problem with my 2. game constructor problem I think
    By military genius in forum Game Programming
    Replies: 6
    Last Post: 10-10-2009, 11:56 AM
  5. Problem with my game
    By porkandbeans in forum C Programming
    Replies: 3
    Last Post: 05-25-2008, 09:13 AM