1,105,546 Community Members

Simple Problem with getchar() in switch

Member Avatar
Madawar
Newbie Poster
18 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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; 

}


}
Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

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.

Member Avatar
Arbus
Practically a Master Poster
615 posts since Dec 2010
Reputation Points: 25 [?]
Q&As Helped to Solve: 31 [?]
Skill Endorsements: 0 [?]
 
0
 

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);

Member Avatar
asterix15
Newbie Poster
7 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
lisaroy1
Light Poster
48 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

'\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.

Member Avatar
vinitmittal2008
Junior Poster
135 posts since Oct 2010
Reputation Points: 37 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 0 [?]
 
1
 

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; 

}


}
Member Avatar
plebbeh
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: