473,803 Members | 3,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of getchar

Cam
Hi everyone,

Before I answer to a (hopefully) helpful reply to this post, I have been
rapped over the knuckles for 'top-posting' and I do not wish to be a learner
poster who observes poor netiquette as I am a firm believer in consideration
.... to this end, could somebody please tell me how I reply to a message
without top-posting? Do I need to have my original post selected when I hit
the 'reply' button?

Here's my question (and please remember that I'm teaching myself as I go and
initially learning what I need to complete assignments although it is my
intention to obtain some sort of proficiency at this code ..)

Previously, I was using the conio.h header and the getche function to read
keyboard input one character at a time to place individual characters into
an array.

As the code is required to compile on a Unix system, I have changed to the
non-platform specific stdio.h header and am using getchar as suggested on
this NG.

The user inputs two 8 bit numbers which then have arithmetic performed on
them. I do this by calling a KeyboardInput() function twice and having the
function modify two arrays by using pointers.

The problem is that the first call of the KeyboardInput() function works
fine. When the function is called a second time (for key2[7] to key2[0]) the
values from the first first call are placed into the key2 array as if the
user had entered them.

Advice is greatly appreciated.

Kind regards,

Cam

code follows:

#include <iostream>
#include <stdio.h> // Re-coded to use ISO standard header (conio.h only used
by Microsoft and Borland)

using namespace std;

void KeyboardInput() ; // Function prototype

int key[8], *key_ptr = key, key1[8], *key1_ptr = key1, key2[8], *key2_ptr =
key2;

....

int main()
{
....
KeyboardInput() ; // key1[7] to key1[0]
for (counter = 0; counter < 8; counter ++)
{
origkey1[counter] = key_ptr[counter];
key1_ptr[counter] = key_ptr[counter];
addkey1_ptr[counter] = key1_ptr[counter];
} // Get key1 array

KeyboardInput() ; // key2[7] to key2[0]
for (counter = 0; counter < 8; counter ++)
{
origkey2_ptr[counter] = key_ptr[counter];
key2_ptr[counter] = key_ptr[counter];
addkey2_ptr[counter] = key_ptr[counter];
} // Get key2 array
....
return 0;
}

....

void KeyboardInput () // Accepts an 8 bis binary input from the user
terminated by ENTER
{
restart:
int ch, counter = 0;
cout << "\nEnter a 2's complement 8 bit binary number: ";

for ( counter = 7; (counter > -1) && ((ch = getchar()) != EOF) && (ch !=
'\n'); counter -- )
key_ptr[counter] = (char)ch - 48; // Subtract 48 from ASCII number to
give integer value

cout << "\n";
Jul 22 '05 #1
3 2245
"Cam" <retsigerymmuda thotmaildotcom> wrote in message
news:40******@d uster.adelaide. on.net
Hi everyone,

Before I answer to a (hopefully) helpful reply to this post, I have
been rapped over the knuckles for 'top-posting' and I do not wish to
be a learner poster who observes poor netiquette as I am a firm
believer in consideration ... to this end, could somebody please tell
me how I reply to a message without top-posting? Do I need to have my
original post selected when I hit the 'reply' button?
It is simply a question of where you choose to type your reply.
Here's my question (and please remember that I'm teaching myself as I
go and initially learning what I need to complete assignments
although it is my intention to obtain some sort of proficiency at
this code ..)

Previously, I was using the conio.h header and the getche function to
read keyboard input one character at a time to place individual
characters into an array.

As the code is required to compile on a Unix system, I have changed
to the non-platform specific stdio.h header and am using getchar as
suggested on this NG.

The user inputs two 8 bit numbers which then have arithmetic
performed on them. I do this by calling a KeyboardInput() function
twice and having the function modify two arrays by using pointers.

The problem is that the first call of the KeyboardInput() function
works fine. When the function is called a second time (for key2[7] to
key2[0]) the values from the first first call are placed into the
key2 array as if the user had entered them.

Advice is greatly appreciated.

Kind regards,

Cam

code follows:


Since you are keen on newsgroup etiquette, a couple more pointers. Supply
compileable code, i.e., compile it yourself and then copy and paste it
exactly as is. The code you have given below is missing the definition of
some variables, making it difficult to figure out where things have gone
wrong.

The quickest way to diagnose most problems is to run the code through a
debugger. If the code won't compile and people have to guess at missing
code, then this process is tedious and inaccurate.

A couple of comments:

1. All of the pointer variables you define seem redundant. You can just use
the array names directly.

2. Expressions like:

for ( counter = 7; (counter > -1) && ((ch = getchar()) != EOF) && (ch
!='\n'); counter -- )

are wonderfully succinct but impossible to debug. Separate out the various
pieces so you can see how they are working.

In as far as I can reproduce your code, given the many omissions, I think
that the problem is that, when you press Enter after the first number, a
'\n'
is placed in the input stream. This is never cleared, so it is the first
character read the second time around, causing the KeyboardInput function to
immediately exit. You can solve the problem by adding

while (ch != '\n')
ch = getchar();

after the for loop.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
Cam
Hi John,

Thankyou very much. I thought that the problem may have been due to a
pointer within the string that the helpfile was talking about but I couldn't
understand how to rectify the problem.

As my code is about 400 lines (I'm probably not as succinct a programmer as
I should be) I wasn't sure as to how much I should post. I tried to just
include the salient lines of code to illustrate the problem.

I hope that I can be in a position to return the favour ... :o)

Cheers,

Cam
Jul 22 '05 #3
"Cam" <retsigerymmuda thotmaildotcom> wrote in message
news:40******@d uster.adelaide. on.net
Hi John,

Thankyou very much.
You're welcome.
I thought that the problem may have been due to a
pointer within the string that the helpfile was talking about but I
couldn't understand how to rectify the problem.

As my code is about 400 lines (I'm probably not as succinct a
programmer as I should be) I wasn't sure as to how much I should
post. I tried to just include the salient lines of code to illustrate
the problem.

A lot of people do the same for the same reason. However, it is a good
practice to try to reproduce the problem in the simplest possible
compileable example in order to isolate the problem. I this do often with my
own code. I sometimes cut and paste the code into a new project and then
delete most of it just to isolate the problem. The less code I have to deal
with, the easier it is to find the problem.

--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
639
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets (data,5,stdin); the_number = atoi (data);
12
4096
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may have missed it). I was wondering wether it was just a 'common' behaviour of most systems, and wether the hit of the ENTER key was mandatory. Thanks for having read me.
1
3723
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if (!isspace(input)) j++;
5
8759
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
10
6607
by: john | last post by:
What does the standard say about getchar()? Do you have to press return to "send" the char to the program, or is it implementation defined? I read in a book that on some systems the function returns after you just typed the char, thus, it has no input buffer. Is that correct?
6
5281
by: Alan | last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4 When I run this program and enter a character at the prompt, I have to press the ENTER key as well. This gives me 2 input characters - 'a' and '\n' (Hex 61 and 0a) It seems as though the getchar() function needs ENTER to terminate reading stdin. I am trying to get the program to respond when I press one key only (ie
11
2505
by: shekhardeodhar | last post by:
The program compiles properly (most of it is from k&r) but the second function (here character_count) gives wrong answer. Can someone please explain why ? #include<stdio.h> #define IN 1 #define OUT 0 int word_count();
25
5452
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
3
12508
by: mahiapkum | last post by:
Hello, I have a code which uses getchar(). #include<stdio.h> int main() { char ch; ch = getchar(); if(ch == 'Y') { printf("Beautiful World\n");
22
3611
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then compared against the integer constant EOF, the comparison may never succeed, because sign-extension of a variable of type char on widening to integer is implementation-defined" while( EOF != (ch = getchar()) ) ....
0
9703
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...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
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.