GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 17-Nov-2004, 16:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by leitz
I tryed to use "getline();" but I get this error then:
Error: test.cpp(145,15):Call to undefined function 'getline'

Here's an example:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main(void)
{
  char line_buffer[BUFSIZ];
  cout<<"Enter anything that you've a mind to: " << endl;
  cin.getline(line_buffer, BUFSIZ);
  cout<<"Here's what I saw:<" << line_buffer << ">" << endl;

  return 0;
}


Regards,

Dave
  #12  
Old 18-Nov-2004, 02:59
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by davekw7x
Anyone out there using .NET? -- I know you're there; I can hear you breathing.--- Is there a getch() for .NET compilers?


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  
Old 18-Nov-2004, 14:36
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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:
#include <iostream>

using namespace std;

int main(void){

    unsigned int last_char_index = 0;
    unsigned int array_length = 0;

    char line_buffer[BUFSIZ];  // defined in stdio.h as 1024

    cout<<"Enter anything that you've a mind to: " << endl;
    cin.getline(line_buffer, BUFSIZ);
    cout<<"Here's what I saw:<" << line_buffer << ">" << endl;

    for (int i=0; i <= BUFSIZ; i++){
        if ( line_buffer[i] == '\0' ){
            last_char_index = i - 1;  // char array is 0 to last_char_index
            array_length = i;         // Number of chars in the array not including return
            break;
        }
    }

    cout << "Input length = " << array_length << endl;


  for (int i=0; i <= BUFSIZ; i++){
      cout << "line_buffer [" << i << "] is : " << line_buffer[i] << endl;
      if ( line_buffer[i] == '\0' ){
          cout << "line_buffer holds " << i << " characters." << endl;
          break;
      }
  }

   return 0;
}

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:
$ input Enter anything that you've a mind to: Here's what I saw:<> Input length = 0 line_buffer [0] is : line_buffer holds 0 characters.

Example output with random key presses:
Code:
$ input Enter anything that you've a mind to: nf n3r30 v aalq3n Here's what I saw:<nf n3r30 v aalq3n> Input length = 17 line_buffer [0] is : n line_buffer [1] is : f line_buffer [2] is : line_buffer [3] is : n line_buffer [4] is : 3 line_buffer [5] is : r line_buffer [6] is : 3 line_buffer [7] is : 0 line_buffer [8] is : line_buffer [9] is : v line_buffer [10] is : line_buffer [11] is : a line_buffer [12] is : a line_buffer [13] is : l line_buffer [14] is : q line_buffer [15] is : 3 line_buffer [16] is : n line_buffer [17] is : line_buffer holds 17 characters.

Now, is where things got weird.
Example output typing in abcd(backspace)abcd(backspace)abcd:
Code:
$ input Enter anything that you've a mind to: abcabcabcd Here's what I saw:<abcabcabcd> Input length = 14 line_buffer [0] is : a line_buffer [1] is : b line_buffer [2] is : c line_buffer [3] is : d line_buffer [4] is : line_buffer [5] is : a line_buffer [6] is : b line_buffer [7] is : c line_buffer [8] is : d line_buffer [9] is : line_buffer [10] is : a line_buffer [11] is : b line_buffer [12] is : c line_buffer [13] is : d line_buffer [14] is : line_buffer holds 14 characters.

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  
Old 18-Nov-2004, 15:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by cable_guy_67
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.



Now, is where things got weird.
Example output typing in abcd(backspace)abcd(backspace)abcd:
Code:
$ input Enter anything that you've a mind to: abcabcabcd Here's what I saw:<abcabcabcd> Input length = 14 line_buffer [0] is : a line_buffer [1] is : b line_buffer [2] is : c line_buffer [3] is : d line_buffer [4] is : line_buffer [5] is : a line_buffer [6] is : b line_buffer [7] is : c line_buffer [8] is : d line_buffer [9] is : line_buffer [10] is : a line_buffer [11] is : b line_buffer [12] is : c line_buffer [13] is : d line_buffer [14] is : line_buffer holds 14 characters.


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:
Enter anything that you've a mind to:
abcabcabcd
Here's what I saw:<abcabcabcd>
Input length = 10
line_buffer [0] is : a
line_buffer [1] is : b
line_buffer [2] is : c
line_buffer [3] is : a
line_buffer [4] is : b
line_buffer [5] is : c
line_buffer [6] is : a
line_buffer [7] is : b
line_buffer [8] is : c
line_buffer [9] is : d
line_buffer [10] is :

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:
  // first your loop
  for (int i=0; i <= BUFSIZ; i++){
      cout << "line_buffer [" << i << "] is : " << line_buffer[i] << endl;
      if ( line_buffer[i] == '\0' ){
          cout << "line_buffer holds " << i << " characters." << endl;
          break;
      }
  }

  // now the loop with (int) cast 
  for (int i=0; i <= BUFSIZ; i++){
      cout << "line_buffer [" << i << "] is : " << (int)line_buffer[i] << endl;
      if ( line_buffer[i] == '\0' ){
          cout << "line_buffer holds " << i << " characters." << endl;
          break;
      }
  }

Regards,

Dave
  #15  
Old 18-Nov-2004, 18:22
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
OK. Using Dave's new int example I have the following:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

int main(void){

    unsigned int last_char_index = 0;
    unsigned int current_index = 0;
    unsigned int array_length_original = 0;
    unsigned int array_length_clean = 0;

    char line_buffer[BUFSIZ];  // BUFSIZ 1024 defined in stdio.h
    char line_buffer_clean[BUFSIZ];

    cout<<"Enter anything that you've a mind to: " << endl;
    cin.getline(line_buffer, BUFSIZ);

    for (int i=0; i <= BUFSIZ; i++){

        cout << "line_buffer [" << i << "] is : " << line_buffer[i] << " and ascii code is "
             << (int)line_buffer[i] <<endl;

        if( ( (int)line_buffer[i] >= 48 ) && ( (int)line_buffer[i] <= 57 ) ){// numbers
            cout << "You have entered a number." << endl;
            line_buffer_clean [current_index] = line_buffer[i];
            current_index ++;
        }
        else if( ( (int)line_buffer[i] >= 65 ) && ( (int)line_buffer[i] <= 90 ) ){// capital
            cout << "You have entered a capital letter." << endl;
            line_buffer_clean [current_index] = line_buffer[i];
            current_index ++;
        }
        else if( ( (int)line_buffer[i] >= 97 ) && ( (int)line_buffer[i] <= 122 ) ){// lowercase
            cout << "You have entered a lowercase letter." << endl;
            line_buffer_clean [current_index] = line_buffer[i];
            current_index ++;
        }
        else if( line_buffer[i] == '\0' ){
            if(current_index > 0){  // don't set index to -1
                last_char_index = i - 1;  // char array is 0 to last_char_index
            }
            array_length_original = i;         // Number of chars in the array not including return
            array_length_clean = current_index;
            cout << "line_buffer holds " << i << " characters." << endl;
            break;
        }
        else if( ( (int)line_buffer[i] >= 32 ) && ( (int)line_buffer[i] <= 127 ) ){ //printable
            cout << "You have entered a printable character." << endl;
            line_buffer_clean [current_index] = line_buffer[i];
            current_index ++;
        }
        else{
            cout << "Get the cat off the keyboard already!" << endl;

            if( (int)line_buffer[i] == 8 ){
                if( current_index > 0 ){
                // do something
                }
            }
        }
    }
    cout << "Here's what I saw <" << line_buffer << ">" << endl;
    cout << "Length of original input is " << array_length_original << endl;
    cout << "High index of original input is " << last_char_index << endl;
    cout << "Here's what I made <" << line_buffer_clean << ">" << endl;
    cout << "Length of cleaned input is " << array_length_clean << endl;
    cout << "High index of cleaned input is " << ( current_index -1 ) << endl;
    return 0;
}

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:
$ input.mark Enter anything that you've a mind to: aB#1 45.23 hELp 78 line_buffer [0] is : a and ascii code is 97 You have entered a lowercase letter. line_buffer [1] is : B and ascii code is 66 You have entered a capital letter. line_buffer [2] is : # and ascii code is 35 You have entered a printable character. line_buffer [3] is : 1 and ascii code is 49 You have entered a number. line_buffer [4] is : and ascii code is 32 You have entered a printable character. line_buffer [5] is : 4 and ascii code is 52 You have entered a number. line_buffer [6] is : 5 and ascii code is 53 You have entered a number. line_buffer [7] is : . and ascii code is 46 You have entered a printable character. line_buffer [8] is : 2 and ascii code is 50 You have entered a number. line_buffer [9] is : 3 and ascii code is 51 You have entered a number. line_buffer [10] is : and ascii code is 32 You have entered a printable character. line_buffer [11] is : h and ascii code is 104 You have entered a lowercase letter. line_buffer [12] is : E and ascii code is 69 You have entered a capital letter. line_buffer [13] is : L and ascii code is 76 You have entered a capital letter. line_buffer [14] is : p and ascii code is 112 You have entered a lowercase letter. line_buffer [15] is : and ascii code is 32 You have entered a printable character. line_buffer [16] is : 7 and ascii code is 55 You have entered a number. line_buffer [17] is : 8 and ascii code is 56 You have entered a number. line_buffer [18] is : and ascii code is 0 line_buffer holds 18 characters. Here's what I saw <aB#1 45.23 hELp 78> Length of original input is 18 High index of original input is 17 Here's what I made <aB#1 45.23 hELp 78> Length of cleaned input is 18 High index of cleaned input is 17

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:
Originally Posted by davekw7x
In other words the operating system filtered the backspace characters before sending it to the C++ iostream (I think)..

This is weird. Guess it just goes to show that all computers are not equal. Since your input is actually what you wanted to enter it is ok. But in my case any key also stores the key-press and then any codes that go with it. For example hitting [space][right arrow][return] gives this result:

Code:
$ input.mark Enter anything that you've a mind to: line_buffer [0] is : and ascii code is 32 You have entered a printable character. line_buffer [1] is : and ascii code is 27 Get the cat off the keyboard already! line_buffer [2] is : [ and ascii code is 91 You have entered a printable character. line_buffer [3] is : D and ascii code is 68 You have entered a capital letter. line_buffer [4] is : and ascii code is 0 line_buffer holds 4 characters. Here's what I saw <> Length of original input is 4 High index of original input is 3 Here's what I made < [D> Length of cleaned input is 3 High index of cleaned input is 2

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  
Old 19-Nov-2004, 02:43
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
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  
Old 01-Dec-2004, 17:47
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
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:
$ cygcheck input.solid Found: .\input.solid input.solid C:\cygwin\bin\cygwin1.dll C:\WINNT\system32\ADVAPI32.DLL C:\WINNT\system32\NTDLL.DLL C:\WINNT\system32\KERNEL32.DLL C:\WINNT\system32\RPCRT4.DLL

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:
$ cygcheck input.solid Found: .\input.solid input.solid C:\WINNT\system32\msvcrt.dll C:\WINNT\system32\KERNEL32.dll C:\WINNT\system32\ntdll.dll

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:
// input.solid.cpp
//
// Released to public domain
//  December 2004
//  Mark Roth
//  Nescopeck, PA
//
// based on a post at GIDForums.com
// and responses by davekw7x
// compiled with g++ 3.3.3 <cygwin special>
// from bash with : g++ input.solid.cpp -s -o input.solid
// running on W2Ksp4
// Idea from finding out that my input stream when using cin
// was not buffered.  In order to parse the input reliably
// I remove all escape action codes, tabs and backspaces(with
// adjustment to new input).
// version 0.0.1
// future enhancements, simplify the routines that filter the 
// input using <string> member functions or similar standard
// library calls.  The cleaned input should not be able to
// "hang" the program.  It will only leave ascii 32 thru 126
// in the line_buffer_new char array and will write an ascii 0
// at the end.  Now just check for '\0' to find end.

#include <iostream>

using namespace std;

int main(void){

    const unsigned short int MAXSIZE = 1024; // This is the limit of the inputs in chars
    unsigned short int last_char_old = 0;
    unsigned short int last_char_new = 0;
    unsigned short int current_index_old = 0;
    unsigned short int current_index_new = 0;
    unsigned short int array_length_old = 0;
    unsigned short int array_length_new = 0;


    char line_buffer_old[MAXSIZE];   // Initialize all to 'x' to prepare for checking
    char line_buffer_new[MAXSIZE] = {'\0'};  // this is the scrubbed input stream

     /******************************************************************************/
    /*  I fill the buffer with the char 'x' to initialize the buffer              */
   /*  this way dec 0's not added by return will not give a false end of stream  */
  /******************************************************************************/
    for ( current_index_old = 0; current_index_old < MAXSIZE; current_index_old++ )
        line_buffer_old[current_index_old] = 'x';

    cout<<"Pound the keyboad and test my strength: " << endl;
    cin.getline(line_buffer_old, MAXSIZE);

    for ( current_index_old = (MAXSIZE - 1); current_index_old != 0; current_index_old--){
        if ( line_buffer_old[current_index_old] != 'x' ){   // Going backward from MAXSIZE gets real eol
            if ( current_index_old != 0 ) {
                array_length_old = current_index_old;
                last_char_old = current_index_old --;
            }
            current_index_old = 1;  // Now we can leave in peace when current_index_old--
        }
    }
    //Good to Here Now scrub the input of excess trash
   // Read line_buffer_old from index 0 forward.  If it is a valid character
  // set line_buffer_new to line_buffer_old increment both buffers

    if ( array_length_old == 0 ){
        cout << "Fine, I don't need you." << endl;
    }
    else{  // Start processing input
        while ( current_index_old < last_char_old){
            switch ( (int)line_buffer_old[current_index_old] ){
                case 8:  // If we find an 8 it is a delete
                        // User wanted to get rid of this so we get rid of last new char
                    if(current_index_new > 0){
                        current_index_new --;
                        line_buffer_new[current_index_new] = '\0';
                    }
                    current_index_old ++;  // Go to next old char
                    break;
                case 9:  // If we find a 9 it is a tab
                    current_index_old ++;  // Go to next char without storing
                    break;

                case 27:  // If we find a 27 it is an escape and things get exciting
                         // here are my rules to remove them
                /***********************************************************************/
                /*  if line_buffer_old[current_index_old] == 27 && c_i_o+4 == 126      */
                /*      set current_index_old =+ 5                                     */
                /*  if line_buffer_old[current_index_old] == 27 && c_i_o+3 == 126      */
                /*      set current_index_old =+ 4                                     */
                /*  if line_buffer_old[current_index_old] == 27 AND c_i_o+1&&+2 is 91  */
                /*      set current_index_old =+ 4              AND c_i_o+3 >=65 <=69  */
                /*      no change to current_index_new                                 */
                /*  if line_buffer_old[current_index_old] is 27 AND c_i_o+1 is 91      */
                /*      set current_index_old =+3               AND c_i_o+2 >=65 <=68  */
                /***********************************************************************/
                    if ( (int)line_buffer_old[current_index_old + 4] == 126 ){
                        current_index_old = current_index_old + 5;
                    }else if ( (int)line_buffer_old[current_index_old + 3] == 126 ){
                        current_index_old = current_index_old + 4;
                    }else if ( ((int)line_buffer_old[current_index_old + 1] == 91)
                            && ((int)line_buffer_old[current_index_old + 2] == 91) ){
                        current_index_old = current_index_old + 4;
                    }else if ( ((int)line_buffer_old[current_index_old + 1] == 91) ){
                        current_index_old = current_index_old + 3;
                    }else{
                        current_index_old ++;  // If it is just an escape by it's self
                    }
                    break;
                default:  // Now check for val of 32 to 126
                    if (((int)line_buffer_old[current_index_old] > 31)
                      && ((int)line_buffer_old[current_index_old] < 127)){
                        line_buffer_new[current_index_new] = line_buffer_old[current_index_old];
                        current_index_new ++;
                        line_buffer_new[current_index_new] = '\0';
                        current_index_old ++;
                    }else{
                        current_index_old ++;
                    }
                    break;
            }

        }
    } // End processing input New input now exists

    array_length_new = current_index_new;
    if( current_index_new > 0 ) last_char_new = current_index_new - 1;

    cout << "I Saw This      : <" << line_buffer_old << ">" << endl;
    cout << "Cleaned Input   : <" << line_buffer_new << ">" << endl;
    cout << "Old Length      : " << array_length_old << endl;
    cout << "New Length      : " << array_length_new << endl;
    cout << "New High index  : " << last_char_new << endl;


    return 0;
}

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
Attached Files
File Type: txt input.solid.troubleshoot.cpp.txt (7.1 KB, 6 views)
__________________
"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  
Old 07-Dec-2004, 21:56
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Slight improvements and additions


I 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:
//  dayname.cpp
//  mktime example: weekday calculator  code from [url]www.cplusplus.com[/url]
//
//  December 2004
//  Mark Roth
//  Nescopeck, PA
//
//  compiled with g++ 3.3.3 <cygwin special>
//  from bash with : g++ dayname.cpp -s -o dayname
//  different date range if compiled with: g++ dayname.cpp -mno-cygwin -s -o dayname
//  running on W2Ksp4
//  Figures out what day of the week a given date is.

#include <iostream>
#include <string>
#include <ctime>
#include <cctype>

#define MAXSIZE 1024

using namespace std;

char   my_buffer[MAXSIZE];
char temp_buffer[MAXSIZE/2];

int clean_digit (void){
     //  This will strip all but the digits from your raw input buffer and leave the '\0' in place
    //  Other filters in cctype.h : isalpha, isalnum, isprint, isspace, isupper, islower, isxdigit,
   //  ispunct, isprint and returns non zero on true.
    unsigned short int index_new = 0;

    for ( unsigned short int index_my = 0; my_buffer[index_my] != '\0'; index_my++ ){
        if ( isdigit( my_buffer[index_my] ) ){

            temp_buffer[index_new] = my_buffer[index_my];

            if ( index_new < (MAXSIZE/2) ){
                temp_buffer[index_new + 1] = '\0';
                index_new ++;
            }
            else temp_buffer[index_new] = '\0';
        }
    }
    return ( atoi(temp_buffer) );
}

void set_date( unsigned short int month, unsigned short int day, unsigned short int year){
      //  get current timeinfo and modify it to user's choice      //
     //  This is were the monthday numbers etc will eventually be //
    const char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
                               "Thursday", "Friday", "Saturday"};
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    timeinfo->tm_year = year - 1900;
    timeinfo->tm_mon = month - 1;
    timeinfo->tm_mday = day;

    /* call mktime: timeinfo->tm_wday will be set */
    mktime ( timeinfo );
    cout << "That would be a " << weekday[timeinfo->tm_wday];

    return;
}


int main (void){

    unsigned short int month, day, year;

    cout << "To use todays date hit enter by itself or," << endl;
    cout << "enter a date between 12/13/01 and 1/18/2038." << endl;
    cout << "A date outside that range will return today's day." << endl << endl;

    cout << "Enter month : ";
    cin.getline(my_buffer,MAXSIZE);

    if ( my_buffer[0] != '\0' ){
        month = clean_digit();

        cout << "Enter day   : ";
        cin.getline(my_buffer,MAXSIZE);
        day = clean_digit();

        cout << "Enter year  : ";
        cin.getline(my_buffer,MAXSIZE);
        year = clean_digit();
    }
    set_date( month, day, year ); // gets current time and sets to user choices

    return 0;
}

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 GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.