Previous in Forum: Getting Started With Programming   Next in Forum: What Does Refresh Do?
Close
Close
Close
11 comments
Member

Join Date: Oct 2006
Posts: 7

"C" Input Overloading

12/02/2006 5:45 PM

Just started learning "C" language in university on my electronics corse.

my assignment program is going to be heavly tested, and as far as I know its all "fool-proof" apart from one thing....

I know that when it comes to a user input, the testers will be leaving their fingers on a key for 2 minutes, and letting the program crash as it exceeds the buffer limit.

ive searched and searched the net for a solution, but sadly any I have found have been attached to a far too complex explation which I dont understand.

so please anyone help me :)

Lachine

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.
Guru
India - Member - Sensors Technology Popular Science - Cosmology - Dream, Think and Act United Kingdom - Member - New Member United States - Member - New Member Canada - Member - New Member

Join Date: Aug 2006
Location: AM-51, Deen Dayal Nagar, Gwalior, Madhya Pradesh, MP 474001, India
Posts: 3395
Good Answers: 32
#1

Re: "C" Input Overloading

12/03/2006 11:37 PM

Use the function kbhit() to sense if key was pressed and then read the key hit using getch() function. finally store the chanracter into a very large array pointed by your pointer and increment counter. You can store as big as gegabyte character array. If you know nothing about C or C++ then get a small book on it and read few examples.

__________________
Prof. (Dr.) Shyam, Managing Director for Sensors Technology Private Limited. Gwalior, MP474001, India.
Register to Reply
Power-User

Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 136
#2

Re: "C" Input Overloading

12/04/2006 12:28 AM

I'm going back many years, but...

Welcome to the world of 90% of the "security holes" in commercial software.

Put your input - using getch() into a fixed length array, one character at a time. When your pointer reaches the array size DO NOT INCREMENT it any more. So typing the alphabet into an 8-character array results in abcdefgz. Alternately you can be clever and start the pointer over at 0 when it reaches it's max, so your above array would read yzstuvwx. Being really smart you could push everything down one once the end was reached and your array would hold the last n characters typed. The example would be stuvwxyz.

NEVER let anyone talk you into making the input area 'large enough for expected worst cases' without a finite limit and pointer control. Let me repeat that: NEVER!!!

Register to Reply
Guru
Australia - Member - New Member Fans of Old Computers - H316 - New Member Hobbies - Model Rocketry - New Member

Join Date: Jun 2006
Location: Port Noarlunga, South Australia, AUSTRALIA (South of Adelaide)
Posts: 3048
Good Answers: 75
#3

Re: "C" Input Overloading

12/04/2006 3:48 AM

Another way is to look for duplicated characters. If the only thing you are trying to trap is a single key being held down then all the characters will be the same. Check the last n characters and if they are identical ditch the input after the first of the replicated characters. You will need to set n to allow for the sort of know duplicated characters that you will normally see. Also don't forget about blank spaces etc. when calculating the value for n as they may occur in large numbers.

__________________
An elephant is a mouse built to government specifications.
Register to Reply
Anonymous Poster
#4

Re: "C" Input Overloading

12/04/2006 5:30 AM

You should start by posting which compiler you use and which OS you use it on. There is a world of a difference in programming for DOS than Windows XP or the likes.

I agree with the rest, there are good but complex ways to do this, but kbhit() and getch() are an easy way out. kbhit() is a function wich will let you know when the keyboard is hit and getch() will get a character from the keyboard buffer, thus keeping it clean. Try it and come back if you need more answers.

Regards.

Alex. (also from electronics)

Register to Reply
Guru
India - Member - Sensors Technology Popular Science - Cosmology - Dream, Think and Act United Kingdom - Member - New Member United States - Member - New Member Canada - Member - New Member

Join Date: Aug 2006
Location: AM-51, Deen Dayal Nagar, Gwalior, Madhya Pradesh, MP 474001, India
Posts: 3395
Good Answers: 32
#5
In reply to #4

Re: "C" Input Overloading

12/04/2006 7:00 AM

another good way is to keep two pointers and a round buffer or end to end joined memory. pointer one at the start of the buffer and pointer two at the filled position. if you overload the buffer then move the start pointer, which will eat away the old data. This way you will keep the last data you entered. This is like forgetting your previous months time table and keeping only current one. If data is less then you will have all of it.

__________________
Prof. (Dr.) Shyam, Managing Director for Sensors Technology Private Limited. Gwalior, MP474001, India.
Register to Reply
Member

Join Date: Oct 2006
Posts: 7
#6
In reply to #5

Re: "C" Input Overloading

12/04/2006 11:52 AM

Thanks for the replies, tho sadly I dont understand about pointers yet, as im only a few weeks into the corse, so im a little confused still.

Here is a sample of my prog so far...

do
{
system("cls");
printf("\n\nPlease select the first frequency in Hz : ");
scanf("%s",bufferfreq1);
fflush(stdin);
freq1 = (float)atof(bufferfreq1);

}
while (freq1 <= 0 || freq1 >= 1000000001);

ect-ect-..

could someone possibly show me as a example how do use the functions suggested?

I have books and books on "C" tho the way they all explain their funactionality is too differcult for me to grasp yet.

Im using "bloodshed" Dev-C++ as the writer+complier, and im on XP.

Thanks again.

Register to Reply
The Architect
Engineering Fields - Software Engineering - S/W Architect Popular Science - Evolution - Fascinating! Fans of Old Computers - TRS-80 - A fine computer United States - US - Statue of Liberty - NY

Join Date: Dec 2004
Location: GlobalSpec, Troy NY
Posts: 386
Good Answers: 5
#7
In reply to #6

Re: "C" Input Overloading

12/04/2006 12:09 PM

It's all about this line: "scanf("%s",bufferfreq1); "

Somewhere bufferfreq1 is declared, maybe like this:

char bufferfreq1[100];

So, you could safely handle upload 100 (or sizeof(bufferfreq1)) chars of input and the terminating '\0'. You need some way to tell scanf not to put more than 100 chars into bufferfreq1.

You should read about scanf formats and you'll see that there are "width" options. You can use them to constrain the length of an input field. A google search on "scanf width" will lead you to the right place.

Good luck!

__________________
Mark Gaulin
Register to Reply
Guru
India - Member - Sensors Technology Popular Science - Cosmology - Dream, Think and Act United Kingdom - Member - New Member United States - Member - New Member Canada - Member - New Member

Join Date: Aug 2006
Location: AM-51, Deen Dayal Nagar, Gwalior, Madhya Pradesh, MP 474001, India
Posts: 3395
Good Answers: 32
#8
In reply to #7

Re: "C" Input Overloading

12/04/2006 12:27 PM

scanf() is not a good function as it requires <CR> to respond.

It also will have problem with 1234<BS><BS><BS><BS><CR>

actually I have pressed 9 keys and will get NULL from scanf() function. I think getch() is much better.

any way as he is just starting so

say

main()

{

char mydata[1000];

maydata << cin;

cout << mydata;

}

__________________
Prof. (Dr.) Shyam, Managing Director for Sensors Technology Private Limited. Gwalior, MP474001, India.
Register to Reply
Guru
Australia - Member - New Member Fans of Old Computers - H316 - New Member Hobbies - Model Rocketry - New Member

Join Date: Jun 2006
Location: Port Noarlunga, South Australia, AUSTRALIA (South of Adelaide)
Posts: 3048
Good Answers: 75
#10
In reply to #6

Re: "C" Input Overloading

12/05/2006 4:57 AM

The technique that Shyam described is used a lot and your bound to come across it so I thought I would explain it to you in more detail. For the explanation refer to the diagram below:

So here is how it works. First you assign a table that will be used as the buffer (represented by the boxes), in this case 16 locations ling. Now you have two pointers that point to locations in the buffer, a write pointer (blue arrow) and a read pointer (red arrow).

Every time a character is received it is placed in the location that the write pointer points to and the pointer value is incremented by 1. In this case the character I has been written to location 08 so the Write pointer will be incremented and the next character will go in location 09. Whenever a pointer gets to goes past location 15 the pointer resets and goes back to 00.

Now when you wish to read the data and process it you read it from the location the Read Pointer (red arrow) points to. In this situation location 04 which contains the character E. The read pointer is then incremented ready to read the contents of the next location. As with the write pointer when it gets to 15 it resets back to 00.

After each read cycle you check to see if the values of the pointers are equal and if they are then there are no further characters to read from the buffer.

As you can see the buffer can never overflow but if the write pointer catches up with the reads pointer then you will loose the 16 characters that havn't yet been read. Because of this it is vitally important that you calculate the size of the buffers so that in normal operation the write pointer never catches the read pointer otherwise you will loose data. I have seen this happen with printers buffers and the result can be very confusing as the end of a document ends up being printed before the beginning.

I doubt this type of buffer will help you in this situation but I can guarantee that you will both come across and use this type of buffer many times when programming.

__________________
An elephant is a mouse built to government specifications.
Register to Reply
Guru
India - Member - Sensors Technology Popular Science - Cosmology - Dream, Think and Act United Kingdom - Member - New Member United States - Member - New Member Canada - Member - New Member

Join Date: Aug 2006
Location: AM-51, Deen Dayal Nagar, Gwalior, Madhya Pradesh, MP 474001, India
Posts: 3395
Good Answers: 32
#11
In reply to #10

Re: "C" Input Overloading

12/05/2006 9:44 AM

Dear Masu

Nice explanation indeed.

The actual buffer is like this one when partially filled.

when fully filled, it is only one location away. thereafter both pointers move together.

__________________
Prof. (Dr.) Shyam, Managing Director for Sensors Technology Private Limited. Gwalior, MP474001, India.
Register to Reply
Guru
India - Member - Sensors Technology Popular Science - Cosmology - Dream, Think and Act United Kingdom - Member - New Member United States - Member - New Member Canada - Member - New Member

Join Date: Aug 2006
Location: AM-51, Deen Dayal Nagar, Gwalior, Madhya Pradesh, MP 474001, India
Posts: 3395
Good Answers: 32
#9

Re: "C" Input Overloading

12/04/2006 12:31 PM

I call all start up software engineers See-in (cin) and See-out (cout) technologymen.

These functions sometime work in funnt way.

Try Borland C++ compiler for these.

__________________
Prof. (Dr.) Shyam, Managing Director for Sensors Technology Private Limited. Gwalior, MP474001, India.
Register to Reply
Register to Reply 11 comments
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.
Copy to Clipboard

Users who posted comments:

Anonymous Poster (1); jdst (1); Lachine (1); masu (2); mgaulin (1); Shyam (5)

Previous in Forum: Getting Started With Programming   Next in Forum: What Does Refresh Do?

Advertisement