Thread: Checking validity of input character

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    Checking validity of input character

    I'm writing a program for a university assignment, its on encryption,
    the program has to ask user for input (printable ascii characters only)
    with max 6 lines of input with 128 characters per line,
    then it ask for a number between 1 and 72 to use as an encryption key,
    and that number is used as part of an algorithm to encode the message.

    I have many problems with this, but im really struggling with one in particular, where it asks for the number, it has to check that that number is between 1 and 72, if they enter an invalid number it has to keep asking until it gets a valid one, here's the code i've got:

    Code:
    void getKey(int *key)
    {
       do
       {
         printf("\nEnter a number between 1 an 72\n");
          scanf("%d", &key);
             if (key < 1 || key > 72)
             {
                 printf("Invalid number entered\n");
                 key = 0;
             }
       } while(key=0);
    }
    error message is that i'm comparing between pointers and integers in the if statement, which i can see, but i cant find a solution. Can anyone help?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    key is pointer, so no need to pass the address to scanf. Change or check the number its pointing to by *key, and while(key=0) is assigning the key pointer to 0, should be (*key==0).
    Code:
    void getKey(int *key)
    {
        do
        {
             printf("\nEnter a number between 1 an 72\n");
             scanf("%d", key);
             if (*key < 1 || *key > 72)
             {
                   printf("Invalid number entered\n");
                   *key = 0;
             }
        } while(*key==0);
    }
    Last edited by Scarlet7; 04-14-2003 at 06:35 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    thanks heaps for that, i understand now

    one moer thing though, we have to input the characters into a multidimensional array, ie getMessage[j][i] where j=6 and i=128;

    they recommend we use the fgets command, but i havent been able to find anywhere how to do use the fgets command in a 2 dimensional array. Is it possible?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You could something like this...
    Code:
    char getMessage[6][128];
    int getMessageCount = 0;
    if( (in = fopen( "abc.txt", "r+" )) != NULL )
    {
        while( fgets( getMessage[getMessageCount], 128, in) != NULL )
        {
             getMessageCount++;
             if(getMessageCount > 6)
             {
                   printf("ERROR: TOO MANY LINES\n");
                   exit(1);
             }
        }
        fclose(in);
    }
    
    for(i=0; i < getMessageCount; i++)
         printf("%s", getMessage[i]);
    Last edited by Scarlet7; 04-14-2003 at 09:43 AM.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Or get stdin from fgets into a into a multidimensional array. Return only will quit while loop or when 6 line are entered.
    Code:
    #include <stdio.h>
    
    int main() 
    {
        char getMessage[6][128];
        int Count = 0;
    
        while( Count < 6 && *(fgets( getMessage[Count], 128, stdin)) != '\n' )
            Count++;
    
        printf("\n");
    
        for(int i=0; i < Count; i++)
            printf("%s", getMessage[i]);
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    you're really good at this, thankyou so much, you're more help than anyone.

    last thing though, since i have to encrypt the characters in the array, i'm probably going to have to have a pointer to the getMessage function, i have an encoding function here, with the encryption algorithm being: encMessage=(i- 32 +key)%95 +32.

    I'll also have to put this in the main function, because after the original message is input, the key is asked for, then the encode function is run, then is asks to input the original key again, then runs te decrypt, which will be pretty much the same as below:

    Code:
    void encode(char getMessage[][])
    {
      int i;
    
        for (i = 0; i < getMessage[][]; i++)
        {
            if (getMessage[i] != '\n')
            {
                  getMessage[i] = (getMessage[i] - 32 +key)%95 +32);
            }
            else
                  getMessage[i] = 0;
        }
    }
    also, where did you learn to program? You're very good at this, any tips for learning?

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Here's some examples for working with 2d arrays. Its probably best to pass the key as a parameter to the encode functions. Always best to check for the 0 at the end of the line if there's no end of line '\n' char. Its also its better to #define the array sizes 6 and 128.
    Code:
    void encodeLine(char *getMessage, int key)
    {
        int i=0;
    
        while( getMessage[i] != '\n' && getMessage[i] != 0)
        {
            getMessage[i] = (getMessage[i] - 32 +key)%95 +32;
            i++;
        }
        if(getMessage[i] == '\n') // if last char == '\n' 
            getMessage[i] = 0;
    }
    
    void encodeLine2(char *getMessage, int key) //same as above but using pointers
    {
        char *s = getMessage;
        while( *s != '\n' && *s != 0)
        {
            *s = (*s - 32 +key)%95 +32;
            s++;
         }
         if(*s == '\n') // if last char == '\n' 
            *s = 0;
    }
    
    void encodeAll2(char getMessage[][128], int lines, int key)
    {
        int i;
        for (i = 0; i < lines; i++)
            encodeLine(getMessage[i], key);
    }
    
    void encodeAll(char getMessage[][128], int lines, int key)
    {
        int i,k=0;
    
        for (i = 0; i < lines; i++)
        {
            while( getMessage[i][k] != '\n' && getMessage[i][k] != 0)
            {
                getMessage[i][k] = (getMessage[i][k] - 32 +key)%95 +32;
                k++;
             }
             if(getMessage[i][k] == '\n') // if the last char is \n
                getMessage[i][k] = 0;
        }
    }
    
    int main()
    {
        char getMessage[6][128];
        int Count = 0, i, key;
    
        while( Count < 6 && *(fgets( getMessage[Count], 128, stdin)) != '\n' )
            Count++;
    
         //Encode one line at a time
        for(i=0; i < Count; i++)
            encodeLine(getMessage[i], key);
    //OR
    
        //Encode all
        encodeAll(getMessage, Count, key);
    
    //OR
    
        //Another encode all
        encodeAll2(getMessage, Count, key);
    
        return 0;
    }
    >also, where did you learn to program? You're very good at this, any tips for learning?
    I started learning C as a hobby around 18 years ago then later C++, been doing it ever since.
    The best advice is to keep writing code. Start developing some small programming projects, such as useful utilities etc... Look at other peoples code and examples.

    Thanks...

    [edit]
    removed the extra ')' in:
    getMessage[i] = (getMessage[i] - 32 +key)%95 +32;
    [edit]
    Last edited by Scarlet7; 04-16-2003 at 09:35 AM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while (Count < 6 && *(fgets(getMessage[Count], 128, stdin)) != '\n')
    Personally, I wouldn't recommend that line of code. If fgets() returns NULL due to end of, or error in, input, the program will still attempt to deference it and will crash.

    >>getMessage[i] = (getMessage[i] - 32 +key)%95 +32);
    You have an extra ).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    As Hammer said... The following will check the return from fgets is !NULL before deferencing for the '\n'.
    Code:
    char getMessage[6][128];
    char *rc;
    int Count = 0;
    
    while( Count < 6 && (rc = fgets( getMessage[Count], 128, stdin)) && *rc != '\n' )
        Count++;
    Can or does the stdin reach end of ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 07-14-2009, 08:17 AM
  2. Error checking user input
    By theharbinger in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 09:57 AM
  3. Problems with character input
    By OmnipotentCow in forum C Programming
    Replies: 19
    Last Post: 06-20-2003, 03:39 PM
  4. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM
  5. how to exclude character input
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2002, 09:20 AM