473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

take input char from keyboard

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
5 14439

<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

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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Smith | last post by:
so is there any alternative ( standard only ) to using in_avail()? I need to poll with given intervals the input stream, and if there is data to be read I want to read it. thank you /B
8
by: Les Coover | last post by:
Consider this source code /* str_test */ #include <stdio.h> /* standard header */ int main(void) { int id, number; char in_string= "2 blue 6";
2
by: bildad | last post by:
The following 'book example' of validating input seems to be incomplete. Since it is a beginner's book it may be intentional for simplicity. But I would like to know how to make this program work...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
1
by: Csaba Gabor | last post by:
Short version: if the user types an alt+ctrl+char combination which leads to a defined character, but s/he's not in a input(text)/textarea, then I'd like that keystroke combination to do the same...
2
by: pelletier.thomas | last post by:
Hello everybody. I'm trying to code a very little OS. But I have a problem with the keyboard input: It show thechar + "~" :s Can you help me ? The source is there :...
1
by: RAGHAVENDRAS | last post by:
Hi All, I am writing a script for getting the ASCII value for the keyboard input. The script: #!c:\perl\bin -w # sascii - Show ASCII values for keypresses use Term::ReadKey;...
9
by: Arks | last post by:
Hi coders, I need your help for a program that I'm doing! I need to parse a text file, so I open it, read it and put the text in a buffer: int len; char *buff; ifstream is; is.open...
4
by: Newbie | last post by:
Hello I need to enter a string of the form abc (a string of characters followed by EOF) #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 100 int main(void) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.