Why is this not working

//Character Manipulation
#include <stdio.h>

int main()
{
int x;
//for loops
int counter=0;
//for input
char character;
char sentence[20];
printf("Press 1 to use getchar \n Press 2 to use gets \n Press 3 to use sscan \n ");
scanf("%d",&x);
switch(x)
{
case 1:
    printf("Enter something");
    while((character=getchar())!= '\n')
    {
      sentence[counter++]=character;
    }
    puts(sentence);
    break; 

}


}

Recommended Answers

All 6 Replies

It's working as expected. The next character is '\n', so the loop terminates immediately. You never end your string with a '\0' character, and the program prints garbage.

Iam not sure about this solution..
every string is ended with null character or \n.try this block...
do
{character=getchar();
sentence[counter++]=character;
}while(character!='\n');
sentence[counter]='\0';
puts(sentence);

u have to use '\0' in place of \n
the string ends with a '\0'
so
use this
while(character=(getchar()!='\0')

it will work

'\0'indicated end of the String So when you will use this null character then after single string only your loop will terminate and when you will use '\n' it should terminate after the end of the line.

Well as Narue suggested..
scanf function leaves '\n' into buffer, so when it reaches

while((character=getchar())!= '\n')

reads '\n' from buffer and loop terminates.
after this you are using

puts(sentence);

it will print some garbage value on screen bcoz your string is not terminated by a '\0' char.
to overcome these problems after using scanf function you need to flush input stream and after also terminate your string with '\0'.
Try this:

//Character Manipulation
#include <stdio.h>

void jsw_flush(FILE *in) // Function copied from older post of Narue
{
    int ch;

    do
        ch = getc(in);
    while (ch != '\n' && ch != EOF);

    clearerr(in);
}

int main()
{
int x;
//for loops
int counter=0;
//for input
char character;
char sentence[20];
printf("Press 1 to use getchar \n Press 2 to use gets \n Press 3 to use sscan \n ");
scanf("%d",&x);
jsw_flush(stdin); // flush input buffer
switch(x)
{
case 1:
    printf("Enter something\n");
    while((character=getchar())!= '\n')
    {
      sentence[counter++]=character;
    }
    sentence[counter] = '\0'; 
    puts(sentence);
    break; 

}


}

Firstly, I suggest always checking the return value of scanf. If you don't check the return value, and someone enters "gibberish" instead of "42", then you end up with garbage in your variable. You'd want to tell the user they entered incorrectly, wouldn't you?

Same thing with your getchar return value. If the user closes stdin (by pressing CTRL+Z in Windows or ^d in *nix), how do you think that will impact upon getchar? You should store the result of getchar into an int so you can tell the difference between EOF and an actual character.

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.