473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("HIIIIII III \n");
}
return 0;
}
Dec 5 '07 #1
5 14454

<j.*******@goog lemail.comwrote in message
news:3f******** *************** ***********@v4g 2000hsf.googleg roups.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("HIIIIII III \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...@mkwah ler.netwrote:
<j.smit...@goog lemail.comwrote in message

news:3f******** *************** ***********@v4g 2000hsf.googleg roups.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("HIIIIII III \n");
}
return 0;
}

-Mike
Dec 5 '07 #3
"j.*******@goog lemail.com" <j.*******@goog lemail.comwrite s:
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...@goog lemail.com" <j.smit...@goog lemail.comwrite s:
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.*******@googl email.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
4816
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
2062
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
5198
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 for all invalid input. Just for example, if user inputs 'abc' an error is caught, but if user inputs '432' the program hangs. Any clarification appreciated. Thanks, Bill
7
10627
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 were a keyboard. I need to be able to identify input from the scanner and keyboard independently. I've looked at DirectX.DirectInput, and using user32.dll to hook into the keyboard messages, but neither method seems to allow for identification of...
1
3473
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 action that it would have done had there not been a defined character. Longer explanation: I've got a US keyboard and a pseudo Hungarian/German keyboard (I combined them). The extra keys on the European keyboard are entered via alt+ctrl+char...
2
2069
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 : http://thonix.tuxfamily.org/pub/0.0.1/src/
1
10556
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; ReadMode('cbreak');
9
2064
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 ("text.txt", ios::binary ); is.seekg (0, ios::end); len= is.tellg();
4
5059
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
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.