By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

take input char from keyboard

P: n/a
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;
}
Dec 5 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a

<j.*******@googlemail.comwrote in message
news:3f**********************************@v4g2000h sf.googlegroups.com...
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
Dec 5 '07 #2

P: n/a

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.netwrote:
<j.smit...@googlemail.comwrote in message

news:3f**********************************@v4g2000h sf.googlegroups.com...
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
Dec 5 '07 #3

P: n/a
"j.*******@googlemail.com" <j.*******@googlemail.comwrites:
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.
Dec 5 '07 #4

P: n/a
On Dec 5, 2:00 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
"j.smit...@googlemail.com" <j.smit...@googlemail.comwrites:
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

Dec 5 '07 #5

P: n/a
j.*******@googlemail.com 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().
Dec 5 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.