Re: correct way to use getchar function?

  • From: Christopher Coale <ccoale427@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Thu, 14 Apr 2011 21:44:23 -0700

That shouldn't matter if the single quote is in a double quoted string literal. For example..

char *str = "Johnny's birthday -- it's in feb";

will work as expected.

What I suspect his problem is is that the getchar() isn't stalling the program because it is simply grabbing the next character from the buffer. So, to answer your question Ashish, you can do one of many things. Here's one thing you can do:

on the line immediately before your "getchar()", add this line:

fflush(stdin);


On 4/14/2011 9:37 PM, Jackie McBride wrote:
Ashish:

When u ask questions on list, if they pertain to an error message,
please include the error so we can better understand what's happening.

What I see a problem with is not your getchar();, but with the line
before, e.g.,
printf("%s's favorite color is %s\n",name,color);
This is something a beginner c programmer might not have encountered,
but an apostrophe has to be "escaped" with a backslash, like this:
printf("%s\'s favorite color is %s\n",name,color);

The reason for this is that apostrophes are used in C to denote a
character, like this:
ch = 'a';
Thus, u have to tell the compiler that this should literally be an apostrophe.

See if that doesn't help, ok?

On 4/14/11, ashish rohtagi<ashishrohtagi1969@xxxxxxxxx>  wrote:
hello friends, I tried to use getchar in this program, please show my
mistakes, as it is not working correctly. here is the code. #include
<stdio.h>

int main() {
char name[20]; char color[20];

printf("What is your name?"); scanf("%s",name);
printf("What is your favorite color?"); scanf("%s",color);
printf("%s's favorite color is %s\n",name,color);

                          getchar();

               return(0);
}
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind




__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: