9 years, 9 months ago.

Reading variable length strings from a keyboard.

Hallo there chaps (and chapettes)!

I realise this is a very basic question, but I seem to be in a bit of a pickle with reading variable length strings from the keyboard. Here is what I'm trying to do:

  1. Read a variable-length string from the keyboard, e.g. 'a', 'ab', 'abc'... In general something I can input by typing alphanumerical characters followed by the enter key.
  2. Use the resulting string in order to cause the program to take some action.

Here is how I envisaged this would work:

Example of code

char c[8]; //Declare holder variable.

pc.gets(c,sizeof(c)); //User input prompt.

if(c == "a"){
    //Code for this case.
}
if(c == "ab"){
    //Code for this case.
}

Problem is that the mBED seems to react by capturing groups of 7 characters (the 8th presumably being the string terminator)... Any tips on how I might go about achieving my goal?

Thanks!

Update: I can now insert the string via the very helpful code I found at:

Repository: RPC2

The problem now is that even though I can reproduce the string I just typed (via printf) my if statements will simply not recognise it so the various code variants are never executed... My guess is that something strange is happening with the terminator or newline characters... I'll keep trying to figure this out.

posted by Nano Group 07 Jul 2014

Aha! I've got it! It seems to work if the code is modified to read:

char c[8]; Declare holder variable.

pc.gets(c,sizeof(c)); User input prompt.

if(!strcmp(c"a")){ Code for this case. } if(!strcmp(c,"ab")){ Code for this case. }

posted by Nano Group 07 Jul 2014

1 Answer

9 years, 9 months ago.

The code you found for reading input from the keyboard includes any new line characters in the string it returns which would throw your string comparisons.

#define _MaxLen_ 8
char c[_MaxLen_];

int count = 0;
while (count < (_MaxLen_ - 1)) {
c[count] = pc.getc();
if ((c[count] == 0x0a) || (c[count] == 0x0d)) // end on a carriage return or a line feed.
break;
count ++;
}

c[count] = 0; // ensure that the result ends in 0 so that string functions will work.

Does effectively the same but without including the new lines in the end result.

As you noticed if you want to compare strings you need to use strcmp, if you try to do something like

if (c == "ab")

then what you are doing is checking if the character pointer c is equal to the character array 'a','b','\0'

In c (and c++) an array name on it's own is a pointer to a block of memory not the contents of the array, so you are checking if the memory address used to store the array c rather than the contents of the array matches the pattern you are looking for. That is always going to be false.

What strcmp is effectively doing is:

bool stringComparison (char *string1, char *string2) {
  int count = 0;
  while (string1[count] != 0) {
    if (string1[count] != string2[count])
      return false;
    count++;
  }
  return true;
}

Accepted Answer

Thanks for the clarification! I think the evidence now clearly points towards the classical mistake of forgetting to dereference pointers...

With respect to the new-line issue, I suspect that because of the `break' statement on line 12, the code I found is also avoiding the trap of swallowing the newline character. When tested on the mBED it seems to be working. Line 28 properly terminates the string and lines 29-30 introduce an elegant line change as I understood it.

Once again thanks for the help!

posted by Nano Group 07 Jul 2014