4

I'm completely new to programming (1st term in uni) and I can't keep up with my lecturer. At the moment I'm stuck on this exercise (for much more time than I'm willing to admit). I've tried to find help on the internet (in this site and others as well), but I can't, since our lecturer has us use a very simple form of c. I'm not asking necessarily for a complete answer. I'd really appreaciate even some hints about where I'm on the wrong. I understand that it might be really simple for some, that the question might seem ignorant or stupid and I feel bad for not getting what's wrong, but I need to try to understand.

So, what I'm trying to do is use scanf and a do while loop so the user can input characters in an array. But I don't understand why the loop won't stop when the user presses ENTER. There's more to the code, but I'm trying to take it slowly, step by step. (I'm not allowed to use pointers and getchar etc).

#include <stdio.h>
main()
{
      char a[50];
      int i;

      printf("Give max 50 characters\n");

      i=0;

      do
      {
            scanf("%c", &a[i]);
            i=i+1;
      }
      while((i<=50) && (a[i-1]!='\0'));

            for(i=0; i<50; i++)
                 printf("%c", a[i]);
 }
S. Kats.
  • 43
  • 5

3 Answers3

3

There aren't any nul-terminated strings here, but only string arrays.

So, when pressing enter, a[i-1] is \n not \0 (scanf with %c as parameter doesn't nul-terminate the strings, and ENTER is just a non-nul character with code 10 AKA \n)

Then don't print the rest of the string because you'll get junk, just reuse i when printing the string back:

#include <stdio.h>
main()
{
      char a[50];
      int i;

      printf("Give max 50 characters\n");

      i=0;

      do
      {
            scanf("%c", &a[i]);
            i=i+1;
      }
      while((i<sizeof(a)) && (a[i-1]!='\n'));  // \n not \0
      int j;
      for(j=0; j<i; j++)  // stop at i
            printf("%c", a[j]);  // output is flushed when \n is printed
 }

Also test with i<50 not i<=50 because a[50] is outside the array bounds (I've generalized to sizeof(a))

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Thank you very much for taking the time to answer. It worked and I can finally see what went wrong. Thank you very much! – S. Kats. Jan 04 '18 at 13:42
2

Here is another way you can do this.

#include <stdio.h>

// define Start
#define ARRAY_SIZE              50
// define End

// Function Prototypes Start
void array_reader(char array[]);
void array_printer(char array[]);
// Function Prototypes End


int main(void) {

    char user_input[ARRAY_SIZE];
    printf("Please enter some characters (50 max)!\n");
    array_reader(user_input);

    printf("Here is what you said:\n");
    array_printer(user_input);

    return 0;
}


// Scans in characters into an array. Stops scanning if
// 50 characters have been scanned in or if it reads a
// new line.
void array_reader(char array[]) {

    scanf("%c", &array[0]);

    int i = 0;
    while (
           (array[i] != '\n') &&
           (i < ARRAY_SIZE)
          ) {

        i++;
        scanf("%c", &array[i]);
    }

    array[i + 1] = '\0';
}


// Prints out an array of characters until it reaches
// the null terminator
void array_printer(char array[]) {

    int i = 0;
    while (array[i] != '\0') {
        printf("%c", array[i]);
        i++;
    }
}
Aniket
  • 449
  • 1
  • 3
  • 12
1

You may try with this code:

#include <stdio.h>

main()
{
  char a[50];
  int i;

  printf("Give max 50 characters\n");

  i=0;
  do {
    scanf("%c", &a[i]);
    i=i+1;
  } while(i<50 && a[i-1] != '\n');
  a[i] = 0;

  for(i=0; a[i] != 0; i++)
    printf("%c", a[i]);
}

The function scanf("%c", pointer) will read one character at a time and place it at the pointer location. You are looking for '\0', which is a valid string terminator, but the newline character you get when you press ENTER and that you should be looking for is '\n'.

Also, it is a good idea to terminate the string you have read by adding a '\0' at the end (really a zero). Then use it to stop printing or you may print the "rest" of the contents of an uninitialized char array.

Javier Elices
  • 2,066
  • 1
  • 16
  • 25
  • Thank you for answering. Every answer is useful to me, I'm still learning and it's good (and to me necessary) to know more than one way to solve something! Thank you. – S. Kats. Jan 04 '18 at 13:51