Little help?

This is a discussion on Little help? within the C Programming forums, part of the General Programming Boards category; I just started a program today, and only got the first function finished when I ran into problems. Just my ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Question Little help?

    I just started a program today, and only got the first function finished when I ran into problems. Just my luck. Anyway, I'm not exactly sure what the problem is, because I have just recently learned about arrays. Well...here's what I have(like I said, its the very beginning):

    /*Include Statements*/
    #include <stdio.h>


    /*Function Prototypes*/
    void getnames(char pl1, char pl2);

    /*Main Function*/
    int main(void)
    {
    /*Variable Declaration*/
    char player1[20], player2[20];



    getnames(player1, player2);

    printf("%c %c", player1, player2);


    return 0;
    }

    /*Function to Get the Players' Names*/
    void getnames(char pl1[20], char pl2[20])
    {
    printf("Player 1, Please Enter Your Name: ");
    scanf(" %c", &pl1);
    printf("Player 2, Please Enter Your Name: ");
    scanf(" %c", &pl2);

    return;
    }

    The error I get is this:
    "error C2664: 'getnames' : cannot convert parameter 1 from 'char [20]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast"

    I am using The Microsoft Visual C++ compiler (v6.0)...sometimes I get errors with it that I won't with Turbo C++. Anyway, I tried using getnames(&player1[0], &player2[0]), though they (to my knowledge) do the same thing. I really dunno what's wrong, all I want to do is get and store the 2 players' first names (I added the printf to see if it was working right, little did I know that I wouldn't even get that far) into the arrays.

    But please remember, I'm new to arrays, so don't be too harsh if its a glaring error...I won't pick up on those for some time yet... =P

    Any help would be much appriciated. = )

    - Pat

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    >>void getnames(char pl1, char pl2);

    make this void getnames(char *pl1, char *pl2);

    or

    void getnames(char pl1[20], char pl2[20]);

    >>printf("%c %c", player1, player2);

    make this printf("%s %s\n", player1, player2); because %c is for char's, and %s is for string arrays.

    >>>>>>>
    printf("Player 1, Please Enter Your Name: ");
    scanf(" %c", &pl1);
    printf("Player 2, Please Enter Your Name: ");
    scanf(" %c", &pl2);
    <<<<<<<

    make that....

    printf("Player 1, Please Enter Your Name: ");
    scanf(" %s", &pl1);
    printf("Player 2, Please Enter Your Name: ");
    scanf(" %s", &pl2);

    for reasons already mentioned.


    Hope that helps.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Ugh...

    Thanks for all the help, it got rid of the error. =P My teacher is big on forgetting to tell us what placeholder goes with what variable type (a friend of mine was using %d for float for about a week). Now I have a new problem...ugh. Here's the code:

    /*Include Statements*/
    #include <stdio.h>


    /*Function Prototypes*/
    void getnames(char pl1[20], char pl2[20]);

    /*Main Function*/
    int main(void)
    {
    /*Variable Declaration*/
    char player1[20], player2[20];



    getnames(player1, player2);

    printf("%s %s \n", player1, player2);


    return 0;
    }

    /*Function to Get the Players' Names*/
    void getnames(char pl1[20], char pl2[20])
    {
    printf("Player 1, Please Enter Your Name: ");
    scanf(" %s", &pl1);
    printf("Player 2, Please Enter Your Name: ");
    scanf(" %s", &pl2);

    return;
    }


    And here is the output:


    Player 1, Please Enter Your Name: Bob
    Player 2, Please Enter Your Name: John
    8_e 8_e
    Press any key to continue


    If I'm having problems this early, I hope the whole project isn't doomed.... =P

    - Pat

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    void getnames(char pl1[20], char pl2[20]) 
    { 
    printf("Player 1, Please Enter Your Name: "); 
    scanf(" %s", &pl1); 
    printf("Player 2, Please Enter Your Name: "); 
    scanf(" %s", &pl2); 
    
    return; 
    }
    pl1 and pl2 are already char *s. Don't scan into &pl1 and &pl2, just scan into pl1 and pl2.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    d'oh! Can't beleive I missed that...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21