Re: correct way to use getchar function?

  • From: ashish rohtagi <ashishrohtagi1969@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Fri, 15 Apr 2011 16:22:45 +0530

hi Prateek, thanks for the explanation. but I do not deserve the
praise about that site. anyway you are now a successful software
developer, web designer and seo expirt. you have made your place, and
I wish you good luck for future. this list is full of successful
programmers, and I am really glad to see you here. take care, regards.
ashish

On 4/15/11, prateek aggarwal <prateekagarwal99@xxxxxxxxx> wrote:
> Respected ashish sir,
> Its privilege finding you here.
> For the benefit of list mates, I’d like to inform that ashish sir is
> one of the pioneers in blind community who have started developing
> community specific portals.
> His portal about blindness related information has been a great hit in
> India, and we all miss that now as he had to shut  the same for some
> reasons.
>
> I’d like to openly confess that ashish sir is one of my inspirational icons.
> Coming on the answer now:
>
> Sir, As you know, the function getchar() is used to get or read the
> input (i.e a single character) at run time. Certainly it solves the
> concern where you want the program to wait for your input.
> To give you a clear picture, let me try to show you a few examples I
> could come up with:
>
> Example 1:
>
>
> void main()
> {
> char ch;
> ch = getchar();
> printf("Input Char Is :%c",ch);
> }
>
>  programme explaination:
> Here,declare the  variable ch as  char data type, and then get a value
> through getchar() library function and store it in the variable ch.And
> then,print
> the value of variable ch.
> During the program execution, a single character is get or read
> through the getchar(). The given value is displayed on the screen and
> the compiler wait
> for another character to be typed. If you press the enter key/any
> other characters and then only the given character is  printed through
> the printf function.
>
> Example 2:
>
>
>       #include <stdio.h>
>       
>       main()
>       {
>               int  i;
>               int ch;
>               
>               for( i = 1; i<= 5; ++i ) {
>                       ch = getchar();
>                       putchar(ch);
>               }
>       }
>
>       Program explaination:
> The program reads five characters (one for each iteration of the for
> loop) from the keyboard. Note that getchar() gets a single character
> from the keyboard, and putchar() writes a single character (in this
> case, ch) to the console screen.
>
> For understanding it better, here is an example of  a  simple
> typewriter, where Every sentence is echoed, once ENTER has been
> pressed until a dot (.) is included in the text.
>
> Example 3:
>
> #include <stdio.h>
>
> int main ()
> {
>   char c;
>   puts ("Enter text. Include a dot ('.') in a sentence to exit:");
>   do {
>     c=getchar();
>     putchar (c);
>   } while (c != '.');
>   return 0;
> }
>
> in case you are well versed with case statement, I’d like to write
> another example that might help you developing an even better
> understanding.
>
> Please checkout here this example program on how to ask the user with
> getchar.
>
> Example 4:
>
> #include <stdio.h>
>
> int main() {
> int retry;
> int key;
>
> do {
> retry = 0;
> printf("Shall I say hello (y/n)? ");
> key = getchar();
> switch(key) {
> case 'y':
> printf("Hello!\n");
> break;
> case 'n':
> break;
> default:
> retry = 1;
> printf("Please enter y or n!\n");
> }
> } while(retry == 1);
>
> return 0;
> }
>
> kindly revert in case you want any further explaination on this.
> Hope it helped somewhat.
>
> Regards,
> Prateek agarwal.
> Director,
> Daedal technovations pvt. Ltd.
> www.daedaltechnovations.com
>
>
>
> On 4/15/11, ashish rohtagi <ashishrohtagi1969@xxxxxxxxx> wrote:
>> hi Chris, that worked. I am using bloodshed as suggested by some
>> members of this list. and Jackie, I already said that the code was
>> compiling but the last message was not staying on screen. Chriss
>> solved this problem. thanks alot. Chriss or anyone else will you
>> please tell me why it happened? and should I use getchar in this way
>> always? take care, regards. ashish
>>
>> On 4/15/11, QuentinC <quentinc@xxxxxxxxxxx> wrote:
>>> Jackie McBride wrote : It is a good idea to flush stdin.
>>> Never flush stdin, it is an undefined behavior
>>>
>>>
>>> __________
>>> 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
>>
>>
> __________
> 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: