Hi Guys,

I need to be able to print something like:

Player 2: Enter a word (1-20 char,blank line to quit):

and then right after I have entered the word the output should look like :

Player 2: Enter a word (1-20 char,blank line to quit): apple

My printf looks like the below:

printf("Player 2: Enter a word (1-20 char,blank line to quit): \n");

So I wonder what do I need to do to be able to keep the cursor at the end of the output of my printf and also what do I need to do to print the contents of WORD within double quotes as below:

"apple" is not in the dictionary .

This is the printf for the output display before:

printf("%s is not in the dictionary . Player 2 wins! \n",WORD));

Thanks guys.

Best regards.

harbry

Note: WORD is defined as char WORD[20];

Don't use a newline charecter (\n) and then when you get input, the cursor will stay on the same line.

To print out quotes, you'll need escape charecters for quotes:

printf("This is a \"fun\" test.\n");
// should print out:
// This is a "fun" test

Don't use a newline charecter (\n) and then when you get input, the cursor will stay on the same line.

To print out quotes, you'll need escape charecters for quotes:

printf("This is a \"fun\" test.\n");
// should print out:
// This is a "fun" test

Hi there,

Thanks for your quick response. It works. Thanks very kind of you.

Actually I was going to ask something else. If I have the below printf:
printf("%s is not in the dictionary . Player 1 wins! \n",WORDP);

how can I get rid of the new line. What I mean is everytime I execute the below printf the output looks like:

vast
is not in the dictionary
.

printf("%s is not in the dictionary . Player 1 wins! \n",WORDP);

So it looks like the WORD variable is displaying the new line character.

Thanks.!!!!!

With what command do you get WORDP from the user?
Please post the code.

Your input is storing the ENTER you typed at the end of the word in the variable. You just need to remove it immediately after the input line. Use strlen() to help find the end of the value and replace the '\n' with '\0'

With what command do you get WORDP from the user?
Please post the code.

regards Niek

Hi

Sorry for the delay here is the code,

printf("Player 1: Enter a word (1-20 char,blank line to quit): ");
fgets(WORDP1,sizeof(WORDP1),stdin);

Thanks.

The cause of the problem has already being mentioned by Walter:

Your input is storing the ENTER or the newline character you typed at the end of the word in the variable. You just need to remove the newline at the end . Use strlen() to help find the end of the value and replace the '\n' with '\0'

Hi Guys,

What would be the best/simple way to remove the '\n' at the and of each string. I'm trying something like:

   i=strlen(line) ;
        while ( i!=0) {
     if (line[i]=='\n') { line[i]='\n');}
          i--;
     }

but it doesn't work. Any ideas. thanks.

harby

if (line=='\n') { line='\n');}

Read to me what you see. Then see if you can see the problem now. ;)

If you're sure that the newline is always the last charecter you could do

line[strlen(line)-1] = '\0';

If you're sure that the newline is always the last charecter you could do

line[strlen(line)-1] = '\0';

It will always be the last character, if it's there. It may not in fact have been read. Always put an if testing to make sure it's there.


Also, harby, you need to use code tags. Read the words on the background where you type these messages.... They were put there for a reason.

It will always be the last character, if it's there. It may not in fact have been read. Always put an if testing to make sure it's there.

I meant in more of a general sense of removing newlines from a string, because the function that hariza was trying to write was trying to take out ALL newlines, not just looking for one at the end.

And yes, an if() statement would be a good idea. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.