![]() |
|
#11
|
|||
|
|||
|
Quote:
Here's an example: CPP / C++ / C Code:
Regards, Dave |
|||
|
#12
|
||||
|
||||
|
Quote:
Yup. getch is still supported by MS; it is in the same library - conio.h. Best regards, Luci __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#13
|
||||
|
||||
|
I was playing with Dave's code using getline() and you can really bang on the keyboard without messing it up. Well, maybe not messing it up is the wrong phrase but this was what I learned.
CPP / C++ / C Code:
I was having a hard time figuring out how to find out how long the actual input was. When trying to compile I would get illegal to compare a pointer to an int or empty char until I finally started looking for '\0'. Now it works great. Hammer on the keyboard and it plays show and tell. Example output with just a return: Code:
Example output with random key presses: Code:
Now, is where things got weird. Example output typing in abcd(backspace)abcd(backspace)abcd: Code:
Now I know that the backspace key code is in [4] and [9] and that it doesn't print so I appear to have nothing (like the return at [14] ) but how can I check for these non-alphanumeric key presses? Tab does something similar as well. Is this because of my console (bash) or would this same thing happen in other situations. I ask because it is common to backspace if the user makes a typing mistake while entering at the prompt. My guess is to check the value of each char and if it doesn't fall into range of a-z, A-Z, 0 - 9, or !@#$%^&*()_-+={[]}\|:;'"/?.>,<`~ or backspace (to correct for user correction) I could save the cleaned array and work from there. Thanks for the getline example, I do think I finally understand how to use it for input. (except if the cat does some tapdancing on the keyboard) __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#14
|
|||
|
|||
|
Quote:
You want weird? I'll give you weird! I copied your code and compiled it and ran it with the same input abcd[backspace]abcd[backspace]abcd Here's my output: Quote:
In other words the operating system filtered the backspace characters before sending it to the C++ iostream (I think).. I got the same results with Borland bcc32, Microsoft Visual C++ 6.0, and GNU g++ on my Windows box and with g++ on my Linux box. If you want to see what the blank spaces are in your output you can do another loop with the characters cast as (int). (Or you could use printf() with %d or %x specifiers --- Yes, regardless of what certain Important and Frequent Contributors to this board seem to think, the world will not stop spinning if you use printf() in a C++ program. It's guaranteed legal by the C++ standard.) Sticking to cout<<, you could do something like CPP / C++ / C Code:
Regards, Dave |
|
#15
|
||||
|
||||
|
OK. Using Dave's new int example I have the following:
CPP / C++ / C Code:
For now, enter anything that is printable 32 thru 127 and a copy of the input_buffer is created. It tells you some info about the input and copy as well as what type of character was pressed. Here is an example of the output: Code:
All that is left is to add the code to adjust when the user hits backspace and make the changes to the copy of the input. I think I got it. Thanks for the code snips Dave. They were most helpful. Quote:
This is weird. Guess it just goes to show that all computers are not equal. Code:
This is with w2k, CygWin and GNU g++. So each of those codes should be able to captured and cleansed. If they don't exist it doesn't matter. Oh yeah, I had better put some error checking on current_index. ascii chart from asciitable.com : ascii-full.gif __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs Last edited by cable_guy_67 : 18-Nov-2004 at 18:34.
Reason: shorten long output line
|
|
#16
|
||||
|
||||
|
For those that don't know, the reason you don't get the backspaces is because the stream input is buffered. IOW, all characters typed go into a buffer owned by the operating system which translates backspaces into actions, not characters (same with all other 'action' keys like ENTER). Then when the ENTER is pressed, that buffer is moved into (or designated as) the 'stream' buffer. So it has been pre-edited before the ENTER was pressed.
Arrow keys are not 'action' keys to the OS. They are combo keys. In Windows they are 2-characters: NULL+LETTER. Same with all function keys. In Linux, they are cursor-movement keys, ESC+[+LETTER which the output (screen) interprets as a cursor control sequence. You can find these code by searching for ANSI Cursor Control or something like that. ANSI is a key word. You will find sequences for clearing screen, clearing to end of line, moving to a specific location, etc. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#17
|
||||
|
||||
|
Thanks Walt, I didn't know that's what I was looking at but I do now.
I can get the same results Dave did if i compile without using the CygWin dll. This confirms for me what Walt stated above for me. The program still operates the same but the size of the old vs new lengths is now equal. Under *nix you see the extra codes so you get a longer char array. Either way, by reading up to the first non space in the new char array you have a clean (buffered by hand) input. First Test Compile: g++ input.solid.cpp -s -o input.solid For me this is the CygWin version that needs to be cleaned. Code:
Second Test Compile: g++ input.solid.cpp -mno-cygwin -s -o input.solid For me this is the windows version that relies and should run on a windows box just fine. Code:
I usually check with cygcheck [filename] since I sometimes forget that I compile my tools to run under CygWin but executables for others for without CygWin installations. It is a nice program to tell you just what it depends on. Solid input code: CPP / C++ / C Code:
Any suggestions on ways I could improve this please let me know. I would like the answer in the form of a question or at the very least a vague inference. Thanks. I attached my work version in case anyone is unsure what is meant when people tell you to "sprinkle in some couts". I usually just comment them out as I am happy with a section of code. If anyone finds any errors I missed or any commentary on my new guy coding style, I would love to hear it. Thanks Dave and Walt, I got to learn something here, lucky me! Now at least I won't have to control-c to get my console program to stop spinning. Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs |
|
#18
|
||||
|
||||
Slight improvements and additionsI have been playing around with the ideas from the last post and came up with a better filtering method for the input that will be generated by the user through the GUI of GIM contacts. Sometimes I want just a range of characters to be valid. Using various cctype methods this should be a snap. As a day finder it still needs bounds checking for the month, day and year. The first day that it returned the dayname for was a Friday the 13th, kind of weird.
If anyone could look this over and make any suggestions on the code as it stands I would appreciate it. I still am pretty new at this so sometimes I haven't learned the obvious yet. At least it is a bit more elegant than the last attempt. The input seems pretty solid but the dayname needs to grow into a bit more. Well, enough learning for today. I will share some links to references that I found usefull while learning about time.h and how it works. Not a perpetual calandar but I have a contact list to get back to. CPP / C++ / C Code:
This is my favorite one, great index at the bottom of the page. The Evil Empire, This is another good reference site with example code. No list would be complete without the granddaddy... Mark __________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work." --Thomas Alva Edison "Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety." --Benjamin Franklin "A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes." --Hugh Downs Last edited by cable_guy_67 : 07-Dec-2004 at 22:00.
Reason: Add the code Homer ... Do'h!
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Having problem in calling functions inthe main | harsha | C Programming Language | 1 | 13-Oct-2004 01:05 |
| Structure compilation problem | nkhambal | C Programming Language | 1 | 03-Aug-2004 09:16 |
| Problem after converting int to char | ise152 | C Programming Language | 0 | 16-Jul-2004 00:03 |
| (read/write file) newbie need help plz | momotx | C Programming Language | 6 | 28-Jan-2004 14:40 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The