0

Im suppose to have this prompt me via stderr to enter two 32-bit keys. I use fgets tofrom stdin to read up the line the user inputs. Then im suppose to parse the line into tokens using strtok_r that converts them to unsigned longs. I already have a function strToULong.c that converts them. Display error messages and reprompt on any invalid input. If the user input Ctrl+D its to exit the program. If i get 2 good keys, then just pass them back to the keys[] referenced by the keys[] parameter.

This is what i have right now. I would appreciate any help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "mycrypt.h"

void getKeys( unsigned long keys[])
{
  char keystring[BUFSIZ] = {'\0'};
  char *savPtr;
  int slen = 0;
  int slen2 = 0;
  char *keyPtr, *keyPtr2;
  unsigned long key1, key2;
  int error = 0;
  char one[], two[];

  (void) fprintf(stderr, "Enter the two 32-bit keys "
                "[octal, decimal, or hax]\n"
                "\tseparated by a space or tab: ");
  if( fgets( keystring, sizeof(keystring), stdin) == NULL)
  {
    // error mesaage and exit Ctrl+D is pressed
    fprintf(stderr, "\n");
    exit(1);
  }

  slen = strlen(keystring);

  while(error != 0 )
  {
  // Be sure to parse the string
  keyPtr = strtok_r(keystring, WHITE_SPACE_CHARS, &savPtr);
  keyPtr2 = strtok_r(NULL, WHITE_SPACE_CHARS, &savPtr);

  slen = (unsigned) strlen(keyPtr);
  slen2 =(unsigned) strlen(keyPtr2);
  if( strlen(slen) == NULL && strlen(slen2) == NULL)
  {
    fprintf(stderr, "\n\tNo keys entered. You must enter 2 keys.\n");
    error = 1;
  }


  if(strlen(slen2) == NULL)
  {
    fprintf(stderr, "\n\tOnly 1 key entered. You must enter 2 keys.\n");
    error = 1;
  }

  // Need to utilize your strToULong
  key1 = strToULong(keyPtr, 0);
  key2 = strToULong(keyPtr2, 0);

  }//end while loop


  // If everything checks out, then store value into keys array
  keys[0] = key1;
  keys[1] = key2;

}

These are the instructions they gave us? This is my first C class. Any help would be appreciated. Thanks in advance.

Edited by Brasa619: forget something

2
Contributors
1
Reply
5
Views
5 Years
Discussion Span
Last Post by DeanMSands3
0

OK, explain this bit.

slen = (unsigned) strlen(keyPtr);
      slen2 =(unsigned) strlen(keyPtr2);
      if( strlen(slen) == NULL && strlen(slen2) == NULL)
      {
        fprintf(stderr, "\n\tNo keys entered. You must enter 2 keys.\n");
        error = 1;
      }


      if(strlen(slen2) == NULL)
      {
        fprintf(stderr, "\n\tOnly 1 key entered. You must enter 2 keys.\n");
        error = 1;
      }

You're getting a strlen from a number that's already the string length of your keys? I'm assuming you started on one approach then changed your mind and started with a new one but forgot to clean out the old code. It's cool. We all do that now and then.

      slen = (unsigned) strlen(keyPtr);
      slen2 =(unsigned) strlen(keyPtr2);
      if((slen == 0) && (slen2 == 0))
      {
        fprintf(stderr, "\n\tNo keys entered. You must enter 2 keys.\n");
        error = 1;
      }


      if(slen2 == 0)
      {
        fprintf(stderr, "\n\tOnly 1 key entered. You must enter 2 keys.\n");
        error = 1;
      }

And why are you using NULL instead of 0? NULL is 0 cast as a void pointer. I mean you can, and it'll probably work, but I wouldn't.

I'm going to assume this is not all and there is another lingering question somewhere.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.