Click here to Skip to main content
15,891,646 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I'm trying to give a string as input.



When I give for example "Hello world" as input , I receive "Hello" only without "World".Why is this happening and how can I sove it???

Thanks...

What I have tried:

C++
int main(void)
{   
    char *A = "Player_1";
    char *B = "Player_2";
    char *word_A = malloc(sizeof(char) * 50);
    word_A = get_word(A,B);
    printf("Word_A => %s\n" , word_A);
}



char *get_word(char *player_A , char *player_B)
{
    char *word = malloc(50);
    printf("%s give a word without %s looking at it: " , player_A , player_B);
    scanf("%s" , word);
    return word;
}


When I give for example "Hello world" as input , I receive "Hello" only without "World".Why is this happening and how can I sove it???

Thanks...
Posted
Updated 8-Sep-20 2:12am
v3
Comments
Richard MacCutchan 8-Sep-20 9:02am    
Why have you changed all your code from the original? The posted solutions now make no sense.

And the answer to your question can be found by studying the documentation for scanf.
Nick_is_asking 8-Sep-20 9:47am    
Yes,I changed it ,cause I wanted to do something else I had in my mind.Never mind.Anyone can see my new above code without any trouble/miss part of program.So,any help???

The clue is in the error message. It says you passed it a "char **". That's what you should pass to it so the code should look like this :
C++
int rand_choice_data_words(char ** for_data_words)
{
    int length = sizeof(for_data_words) / sizeof(for_data_words[0]);
    int pos = (rand() % (length - 1) + 1);
    return pos;
}

int main(void)
{
    srand(time(NULL));
    char *data_words[] = {"knowledge","fantasy","inspiration","practice","construction","creation"};
    int position = rand_choice_data_words(data_words);
    printf("%s\n\n" , data_words[position]);
    return 0;
}
Have you noticed that the only reason you passed in the array is so you could figure out how big it is? I think a better option is to figure that outside the function. Here is a way you can do this by counting :
C++
int random_choice( int listSize )
{
    int pos = rand() % listSize;
    return pos;
}

int main(void)
{
    char *words[] =
    {
        "knowledge"
        , "fantasy"
        , "inspiration"
        , "practice"
        , "construction"
        , "creation"
        , NULL
    };

    int count = 0;
    int n;

    srand( time(NULL) );
    for( int n = 0; words[ n ] != NULL; ++n )
        ++count;
    int position = random_choice( count );
    printf("%s\n\n" , words[ position ] );
    return 0;
}
This uses a NULL at the end of the list to act as a terminator. I think the random number algorithm was a little off too. If there are five items in a list indexes into can range from 0 to 4. The modulo operator with five will give results ranging from 0 to 4 also so there is no need to subtract from the number of words. You don't want to add one either or you will never use item 0 and item 5 is invalid.
 
Share this answer
 
Comments
CPallini 8-Sep-20 2:06am    
5.
Quote:
int length = sizeof(for_data_words) / sizeof(for_data_words[0]);
Inside a function, it is a logic error, because of array decay. See, for instance: https://medium.com/@prafullkumar77/array-decay-in-c-c-ways-to-prevent-it-485e86839008[^].
I would stay simple, e.g.
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>



int main(void)
{
    srand(time(NULL));
    const char * data_words[] = {"knowledge","fantasy","inspiration","practice","construction","creation" };
    const size_t ITEMS = sizeof(data_words) / sizeof(data_words[0]);
    int position = rand() % ITEMS;
    printf("%s\n\n" , data_words[position]);
    return 0;
}
 
Share this answer
 
Comments
Rick York 8-Sep-20 11:52am    
That was informative to me. I have never heard of array decay. That is a better explanation for why the sizeofs are a bad idea.
CPallini 9-Sep-20 2:04am    
Yes, it is a good-to-know C/C++ quirk.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900