take input char from keyboard

J

j.smith2c

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;
}
 
M

Mike Wahler

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
 
J

j.smith2c

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
 
B

Ben Bacarisse

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.
 
J

j.smith2c

This is question 12.18b of the C FAQ:

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




<snip similar>

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

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
 
M

Mark Bluemel

(e-mail address removed) wrote:

What part of

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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top