Thread: Game log in problem.

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes - it's all spelled out in post #12 (where each compiler warning was addressed). If there's something there you do not understand, feel free to ask for clarification.

  2. #17
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    What is this post #12 and where is it?

  3. #18
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Never mind, I figured out what you meant by post 12.

  4. #19
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    I still do not know how to get fix these two warnings

    Code:
    C:\Users\**********\Documents\Summer Programming in C\Homework4\Homework_4.2.c:52:25: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[20]' [-Wformat=]
                             printf("Welcome %s!!!\n\n",mem_id);
                             ^
    C:\Users\**********\Documents\Summer Programming in C\Homework4\Homework_4.2.c:53:31: warning: assignment from incompatible pointer type [enabled by default]
                             idptr = mem_id;
                                   ^

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    >> warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[20]

    Applying what I said before...

    Quote Originally Posted by Matticus
    When reading a string, you don't need the '&'
    Means you would change this:

    scanf("%s", &tempid);

    ...to this:

    scanf("%s", tempid);

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Ruger06 View Post
    I still do not know how to get fix these two warnings

    Code:
    C:\Users\**********\Documents\Summer Programming in C\Homework4\Homework_4.2.c:52:25: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[20]' [-Wformat=]
                             printf("Welcome %s!!!\n\n",mem_id);
                             ^
    C:\Users\**********\Documents\Summer Programming in C\Homework4\Homework_4.2.c:53:31: warning: assignment from incompatible pointer type [enabled by default]
                             idptr = mem_id;
                                   ^
    You seem to lack some basic understanding of pointers, arrays, and types. It's pretty obvious that you have to get this done for homework, so below is some advice for this program. Once you finish this assignment however, I strongly recommend you go back and spend some serious time with more basic examples and problems (start with 1-d arrays only, and basic pointers). The internet abounds with sample problems to practice these skills. Really work on the fundamentals of arrays and pointers. Not just being able to finish a program using them, but to really understand them, their relationship, how they work and how to use them. Also, commit to memory that for any type foo, different levels of indirection result in different types. That is to say, a foo object, a pointer to foo (foo *), and a pointer to pointer to foo (foo **), etc. are all different types which are not compatible.

    For the first error:
    mem_id is a 2-d char array. Thus, when you use it by itself, with no brackets [ ] for indexing, you effectively have a char ** type. But the %s format specifier for printf takes a single pointer to char, i.e. a char *. What you probably want is to print a single element from mem_id, such as mem_id[0] or mem_id[i].

    For the second error:
    Again, idptr is a pointer to char, but mem_id without [ ] brackets is a char **. You can not safely assign between the two, you need to make idptr point to the right thing. Again, you probably want something like idptr = mem_id[0]; or idptr = mem_id[i].

  7. #22
    Registered User
    Join Date
    Jul 2014
    Posts
    34
    Sorry I did not get back here sooner. I did get all of the bugs out. Here is what I ended up with. And again thank you to all that gave input.

    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[i]);
                            idptr = mem_id[i];
                            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);
            }
        }
    }

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Good work on getting most of the kinks worked out.

    Can we talk about your "for()" loop? Because what it appears you want it to do, and what it actually does, seem to be different.

    It appears that you want to cycle through all of the stored ID/passwords and compare them with the current "log in" input. However, regardless of the outcome, you break out of the loop after the first iteration. So you're only really checking the input with the first ID/password stored by your program.

    You can test this by selecting "Sign in" twice, making two separate accounts, and then trying to "log in" with the second.

    This logic should be re-structured. Consider your "else" clause within that loop. If you want to check the current input with all stored data, you shouldn't print an "incorrect" message when one doesn't match, because the match might be further down the array. It's only when all of the checks fail that you want to print an error message.

    Correcting this is very easy, if you choose to use a new variable as a flag - for instance, a variable called "fail_login" or something like that. Before the loop, you set this to true. In the loop, if a match is found, you set this variable to false. After the loop ends, if the variable is still true, you know to print out your error message.

    --------

    A few other points, if you're interested.

    Your "while()" loop in "main()" terminates when "choice == 3". However, when the user enters 3, the corresponding "if()" statement breaks out of the loop. You would achieve the same result with an infinite loop (i.e. "while(1)") (though I'm not advising you do so).

    It might be better to use a "do-while()" loop for this kind of logic. This way, the code within the loop is guaranteed to execute at least once before checking the condition. And you wouldn't need code that says, "if choice == 3, break".

    Also, "scanf()" can be fragile if not used properly. For instance, run your program and at the first prompt, enter 'a'. You enter an infinite loop. There are things you can do to make the "scanf()" call safer, or alternatives that are more sturdy - I'll leave it to you to ask about these if you're interested.

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