Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > take input char from keyboard

Reply
Thread Tools

take input char from keyboard

 
 
j.smith2c@googlemail.com
Guest
Posts: n/a
 
      12-05-2007
Hi everyone,

I wanted to run the following loop,
and enter y everytime (I expected that it will ask
me to enter y for 5 time), but just after 3 times program terminates.
what is the error??


#include <stdio.h>

int main ()
{
char ch;
int j=0;
for (j ; j<5 ; j++)
{
printf("Please enter the input character :\n\n");
scanf("%c", &ch);
printf("You have entered : %c \n\n" , ch);

if (ch == 'y' || ch == 'Y')
printf("HIIIIIIIII \n");
}
return 0;
}
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      12-05-2007

<> wrote in message
news:3fd84ee8-d816-4379-98bc-...
> Hi everyone,
>
> I wanted to run the following loop,
> and enter y everytime (I expected that it will ask
> me to enter y for 5 time), but just after 3 times program terminates.
> what is the error??
>
>
> #include <stdio.h>
>
> int main ()
> {
> char ch;
> int j=0;
> for (j ; j<5 ; j++)
> {
> printf("Please enter the input character :\n\n");
> scanf("%c", &ch);
> printf("You have entered : %c \n\n" , ch);


Change the above line to:

printf("You have entered: %c (decimal %d) \n", ch, (int)ch);

and run it again. Look carefully at the results.

>
> if (ch == 'y' || ch == 'Y')
> printf("HIIIIIIIII \n");
> }
> return 0;
> }


-Mike


 
Reply With Quote
 
 
 
 
j.smith2c@googlemail.com
Guest
Posts: n/a
 
      12-05-2007

Now it gives following output, but it is not the expected output.
I still could not find what the problem is.

Please enter the input character :

y
You have entered: y (decimal 121)
HIIIIIIIII
Please enter the input character :

You have entered:
(decimal 10)
Please enter the input character :

y
You have entered: y (decimal 121)
HIIIIIIIII
Please enter the input character :

You have entered:
(decimal 10)
Please enter the input character :

y
You have entered: y (decimal 121)
HIIIIIIIII




On Dec 5, 1:29 am, "Mike Wahler" <mkwah...@mkwahler.net> wrote:
> <j.smit...@googlemail.com> wrote in message
>
> news:3fd84ee8-d816-4379-98bc-...
>
>
>
> > Hi everyone,

>
> > I wanted to run the following loop,
> > and enter y everytime (I expected that it will ask
> > me to enter y for 5 time), but just after 3 times program terminates.
> > what is the error??

>
> > #include <stdio.h>

>
> > int main ()
> > {
> > char ch;
> > int j=0;
> > for (j ; j<5 ; j++)
> > {
> > printf("Please enter the input character :\n\n");
> > scanf("%c", &ch);
> > printf("You have entered : %c \n\n" , ch);

>
> Change the above line to:
>
> printf("You have entered: %c (decimal %d) \n", ch, (int)ch);
>
> and run it again. Look carefully at the results.
>
>
>
> > if (ch == 'y' || ch == 'Y')
> > printf("HIIIIIIIII \n");
> > }
> > return 0;
> > }

>
> -Mike


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-05-2007
"" <> writes:

> Now it gives following output, but it is not the expected output.
> I still could not find what the problem is.


This is question 12.18b of the C FAQ:

http://c-faq.com/stdio/scanfc.html

> Please enter the input character :
>
> y
> You have entered: y (decimal 121)
> HIIIIIIIII
> Please enter the input character :
>
> You have entered:
> (decimal 10)
> Please enter the input character :

<snip similar>

PS. Please snip your replies to include on the parts you are
commenting on.

--
Ben.
 
Reply With Quote
 
j.smith2c@googlemail.com
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 2:00 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> "j.smit...@googlemail.com" <j.smit...@googlemail.com> writes:
> > Now it gives following output, but it is not the expected output.
> > I still could not find what the problem is.

>
> This is question 12.18b of the C FAQ:
>
> http://c-faq.com/stdio/scanfc.html
>
> > Please enter the input character :

>
> > y
> > You have entered: y (decimal 121)
> > HIIIIIIIII
> > Please enter the input character :

>
> > You have entered:
> > (decimal 10)
> > Please enter the input character :

>
> <snip similar>
>
> PS. Please snip your replies to include on the parts you are
> commenting on.
>
> --
> Ben.


Hello all,

Still I could not solve the problem mentioned.
Do anyone can tell "if I want to run for loop from 1 to 5, everytime
it ask user to enter a character , and if that
character is same with y, then print "Hello" ".

Thank you.

smith

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      12-05-2007
wrote:

What part of

>> PS. Please snip your replies to include on the parts you are
>> commenting on.


didn't you understand?

> Still I could not solve the problem mentioned.


Then you didn't read the FAQ carefully enough.

> Do anyone can tell "if I want to run for loop from 1 to 5, everytime
> it ask user to enter a character , and if that
> character is same with y, then print "Hello" ".


That's a trivial task, but made much harder by your decision
to use scanf().

Try reading the whole of http://c-faq.com/stdio/index.html - question
12.20 is especially relevant.

Then consider rewriting your code without scanf().
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57