0

(I am using VS community, that's the reason why i used "scanf_s") I am trying to complete an assignment and i came across an issue that i couldn't solve no matter what i did. My code is =

char char_array[3];


inputchar_1D(char_array, 3);
outputchar_1D(char_array, 3);

And the functions are :

void inputchar_1D(char arr[], int size) 
{
    printf("\n\ninput : \n");
    for (int i = 0; i < size; i++) 
    {
        printf("\nEnter your character (%d) : ", (i));
        scanf_s("%c", &arr[i]);

    }
}

void outputchar_1D(char arr[], int size) 
{
    printf("output : \n");
    for (int i = 0; i < size; i++) 
    {
        printf("%c...\t", arr[i]);

    }
}

output :

input :



Enter your character (0) :
Enter your character (1) : s

Enter your character (2) : output :

...     s...
...

As you can see it is skipping the first scanf and going to 2nd one. There it asks me to write a character and once i do that it skips the 3rd one and goes to the 2nd function. Once there it prints my inputted 2nd character as the first one. The 2nd character is empty, and the 3rd character is for some reason this "╠" weird symbol. QUESTIONS :

  1. Why is it skipping the scanf
  2. Why is my character input at arr[1] printed as the arr[0]
  3. Why did it pass to the new line even though there is no \n

Thank you for sparing your time to help me with this...

Can Oezlemis
  • 169
  • 1
  • 10
  • 1
    `char_array` has size 1 - only space for a single element. You're using it for 3 elements. The result is undefined behaviour. – kfx Nov 10 '18 at 20:03
  • You have used `scanf_s("%c", . . .)` incorrectly, because it requires a **size** argument: *"Unlike `scanf` and `wscanf`, `scanf_s` and `wscanf_s` require the buffer size to be specified for all input parameters of type `c`, `C`, `s`, `S`, or string control sets that are enclosed in `[]`. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable."* – Weather Vane Nov 10 '18 at 20:05
  • 1
    There is another error with `&arr[i - 1]` because on the first loop iteration you are out of array bounds. – Weather Vane Nov 10 '18 at 20:06
  • @kfx I gave char_array[3]. This solved the issue with the output of one coming up as "╠". But it still doesn't fix the 1st,2nd,4th questions – Can Oezlemis Nov 10 '18 at 20:06
  • @WeatherVane After reading your comment i went and checked out what you meant by it and as if i was reading it i noticed a note that was the solution for all of my question. The solution is as easy as writing scanf(" %c", &arr[i]) rather than scanf("%c", &arr[i]). (adding a space before %c).... Thank you for your help. And I fixed the &arr part too. Thank you for help! – Can Oezlemis Nov 10 '18 at 20:26
  • Ah ok, then please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Nov 10 '18 at 20:34
  • @WeatherVane yes exactly that is the post i saw, once again, thank you for your help – Can Oezlemis Nov 10 '18 at 20:39
  • Aside: in future please don't remove errors from the posted code that have been commented on, unless it was an error in posting. Otherwise comments make no sense, and we have to look back through the question edits to see what they refer to. – Weather Vane Nov 10 '18 at 20:41
  • @WeatherVane Oh! I am sorry, this is the first time i am actually using stackoverflow. Noted! won't happen again. – Can Oezlemis Nov 10 '18 at 20:49

0 Answers0