C
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";
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";