Thread: questions about character input and ignoring certain characters

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    41

    questions about character input and ignoring certain characters

    hey the program that i am modding displays a letter(only the first and ignores all others) i made a minor mod on the program so that whitespaces are not read and ignored include tab spaced. i want the program so read only a-z, A-Z, or ; anything else it should just say there wasnt input
    i appreciate all the advice/help i can get!

    Code:
    #include <stdio.h>
    
    int skipspace(void);
    int skiprest(void);
    
    int main()
    {
      int com;
    
            com = skipspace();  
            while (com != EOF)
            {
                    printf("The command is: %c\n", com);
                    skiprest();
                    com = skipspace();   
            }
    }
    
    int skipspace(void)
    {
      int c;
    
            c = getchar();
            while (c == ' ' || c == '    ') 
                    c = getchar();
            return c;
    }
    
    int skiprest(void)
    {
      int c;
    
            c = getchar();
            while (c != '\n')
                    c = getchar();
            return c;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    char legalchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
    int GetLegalInput( char *allowed )
      { int ch;
         do 
          ch = getchar();
         while(! strchr(allowed,ch));
         return ch; }
    
    
    // call as...
    input = GetLegalInput(legalchars);
    The allowed string can be any string of characters appropriate to the input you are processing.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    I've noticed lots of descriptions for programs, read like episode scripts from "The Twilight Zone".
    Now ain't that the truth!

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    CommonTater thanks for the reply.
    so the new code looks like this with ur function implemented
    but " input = GetLegalInput(legalchars); " i dont know exactly where i should put this.

    Code:
    #include <stdio.h>
    
    
    int skipBlanks(void);
    int skipOverRestOfCommand(void);
    int GetLegalInput( char *allowed );
    
    
    int main()
    {
      int com;
    char legalchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
    
            com = skipBlanks();  /* declare com */
    	
            while (com != EOF)
            {
                    printf("The command is: %c\n", com);
                    skipOverRestOfCommand();
                    com = skipBlanks();
        
            }
    
    
    }
    
    
    int skipBlanks(void)
    {
    /*
    skipBlanks() ignores white spaces
    */
      int c;
            c = getchar();
    	/* letters spaced or tabbed are read */
            while (c == ' ' || c == '	') 
                    c = getchar();
            return c;
    	
    
    
    }
    
    
    int skipOverRestOfCommand(void)
    {
    /*
    skipOverRestOfCommand() skips all letters and words after the first letter entered
    */
      int c;
      
            c = getchar();
            while (c != '\n')
                    c = getchar();
            return c;
    }
    
    
    int GetLegalInput( char *allowed )
      { int ch;
         do 
          ch = getchar();
         while(! strchr(allowed,ch));
         return ch; }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought Tater would be back, but OK.

    You can delete your other code selection functions, and just use Tater's function ( as a separate function, not in main() ). Just have the return from it, go into your char array[]. Say you want 5 legal letters:

    Code:
    for(i=0;i<5;i++) {
       myCharArray[i] = GetLegalInput(allowed);
    }
    And myCharArray[] will end up with 5 legal char's.

    SkipBlanks and SkipOverRest you can delete.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is why scoop and poop coding is a REAL BAD IDEA... you wind up with all this stuff on your page and you don't even know how any of it works...

    You've dropped that code into your source and you don't even call it...
    Not only that but you don't need skipoverblanks or skiprest anymore since those characters are consumed in the GetLegalInput function.

    What the OP needs to do is to stop borrowing code and actually sit down with a textbook and learn C programming.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    that makes more sense but i havent used arrays or i dont even know how to use them but i have tidy the program a bit and here is what i ended up with

    Code:
    #include <stdio.h>
    
    int GetLegalInput( char *allowed );
    
    int main()
    {
      int cmd;
    
            cmd = GetLegalInput();
            while (cmd != EOF)
            {
                    printf("The command is: %c\n", cmd);
            
        
            }
    }
    
    int GetLegalInput( char *allowed )
      { 
    int ch, i;
    char legalchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for(i=0; i<51;i++)
        {
            Char[i] = GetLegalInput(allowed);
         do
          ch = getchar();
         while( strchr(allowed,ch));
         return ch; 
      }
    Quote Originally Posted by Adak View Post
    I thought Tater would be back, but OK.

    You can delete your other code selection functions, and just use Tater's function ( as a separate function, not in main() ). Just have the return from it, go into your char array[]. Say you want 5 legal letters:

    Code:
    for(i=0;i<5;i++) {
       myCharArray[i] = GetLegalInput(allowed);
    }
    And myCharArray[] will end up with 5 legal char's.

    SkipBlanks and SkipOverRest you can delete.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ecsx00 View Post
    that makes more sense but i havent used arrays or i dont even know how to use them but i have tidy the program a bit and here is what i ended up with

    Code:
    #include <stdio.h>
    
    int GetLegalInput( char *allowed );
    
    int main()
    {
      int cmd;
    
            cmd = GetLegalInput();
            while (cmd != EOF)
            {
                    printf("The command is: %c\n", cmd);
            
        
            }
    }
    
    int GetLegalInput( char *allowed )
      { 
    int ch, i;
    char legalchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for(i=0; i<51;i++)
        {
            Char[i] = GetLegalInput(allowed);
         do
          ch = getchar();
         while( strchr(allowed,ch));
         return ch; 
      }
    nope... still not going to work... In fact I doubt it would compile...
    Your cmd != EOF ain't happening because it's not on the legal characters list and will not be returned by the function.
    As soon as you enter 1 keystroke the program is going into an infinite loop.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int GetLegalInput( char *allowed )
      { int ch;
         do 
          ch = getchar();
         while(! strchr(allowed,ch));
         return ch; }
    
    
    int main (void)
      { 
         char *legalchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!";
         int cmd;
         
         do 
            {  cmd = GetLegalInput(legalchars);
                printf ("Command = %c\n", cmd); }
         while (cmd != '!');
    
         return 0; 
    }

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    CommonTater, thanks man now I have an idea how the function works. ill come back in a few hours if i cant get a print statement to show up but thanks for the big help!

    Quote Originally Posted by CommonTater View Post
    nope... still not going to work... In fact I doubt it would compile...
    Your cmd != EOF ain't happening because it's not on the legal characters list and will not be returned by the function.
    As soon as you enter 1 keystroke the program is going into an infinite loop.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int GetLegalInput( char *allowed )
      { int ch;
         do 
          ch = getchar();
         while(! strchr(allowed,ch));
         return ch; }
    
    
    int main (void)
      { 
         char *legalchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!";
         int cmd;
         
         do 
            {  cmd = GetLegalInput(legalchars);
                printf ("Command = %c\n", cmd); }
         while (cmd != '!');
    
         return 0; 
    }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Now do yourself a favour... and don't just copy what I wrote... use the principles I showed you and write a version of the code that works best in your situation. Copy and paste... Scoop and poop usually leaves you with poop!

    My point is simply this: You need to learn C Programming... not Code Theft.

    (sorry if that's blunt, but it's true)

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    CommonTater yea thanks for the tip/info. i am right now remaking and tweaking the code to get it to do what i want it to do overall.
    the only thing is if lets say i enter an "illegal" character i want an error message to display the mod code is below

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    tweaked code a little bit with an added nonworking print statement

    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main (void)
    {
    char *legalchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int cmd, flag;
    
    do
    { cmd = GetLegalInput(legalchars);
    printf ("Command = %c\n", cmd); }
    while (flag != EOF);
    
    do
    { cmd != GetLegalInput(legalchars);
    printf("Error: %c is not a letter\n", cmd);
    }
    while (cmd != GetLegalInput(legalchars));
    }
    
    
    int GetLegalInput( char *allowed )
    { int ch, flag;
    do
    flag = ch = getchar();
    while(! strchr(allowed,ch));
    return ch; 
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Are you testing this stuff? You can't be...

    You have placed functions after main() with no prototypes... your compiler should be complaining like crazy.

    Line 12 ... you are not going to get EOF back from GetLegalInput... it's not a legal character in the set you've defined, so that loop is officially enless.

    Lines 14 to 19 will never happen, first because of line 12 causing an eternal loop and secondly because you will never get an illegal character back from GetLegalInput.

    line 25 ... flag does nothing.

    Ok... time to stop guessing and start learning.
    Get a good C Programming textbook or tutorial... start on page 1, read every word, repeat as necessary. Type up the examples, play with the code, break it, fix it, improve it, learn what does and does not work... now turn to page 2 and repeat the process... page by page until you finish the whole thing.

    In fact you can start right ... HERE
    Last edited by CommonTater; 10-18-2011 at 09:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ignoring specific characters in a string
    By seanksg in forum C Programming
    Replies: 11
    Last Post: 05-02-2011, 06:08 PM
  2. Ignoring certain characters in input
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 03-27-2010, 11:06 PM
  3. Ignoring New Line Characters
    By Spencer Wallace in forum C Programming
    Replies: 2
    Last Post: 03-27-2006, 01:23 AM
  4. Ignoring input.
    By Vicious in forum C++ Programming
    Replies: 11
    Last Post: 10-17-2004, 04:31 PM
  5. Ignoring my input (as a bad thing)
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 03-11-2002, 02:53 PM