1

as a homework assignment for my computing 1 college course, my professor has given me the task of having the user input a string of characters into the terminal, taking that string, adding it into an array, then printing the array and printing the array backwards. I think that I know of a way to print the array backwards, however, I cannot come up with a way to read from the terminal and add the characters from the terminal to an array. I have tried doing the following:

char ch;
for (int i = 0; i <= 80 || str[i] == '\n'; ++i) {
scanf_s("%c", &str[i]);
}

I am wondering if someone could explain to me why this section of code does not operate as expected, and if someone could give me some other ideas to try. Thank you.

  • 1
    For starters it does not work because you don't have a main function. Please fix a [mre] – klutt Nov 14 '19 at 19:54
  • 1
    After leaving your loop` do you expect `str` to be a *nul-teminated* string? If so, how? What does `char ch;` have to do with the snippet you posted? Why use `scanf` in this case anyway? If you are reading a character at a time `str[i] = getchar();` will do the same thing. – David C. Rankin Nov 15 '19 at 04:34
  • One will find that `scanf` is not really suited for interactive input, see http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html, and `scanf_s` is non-standard. – Neil Nov 15 '19 at 04:52
  • strongly suggest using `fgets()` to input lines of data. See the MAN page for `fgets()` for the details of that function. – user3629249 Nov 16 '19 at 01:08
  • how is `str[]` declared? – user3629249 Nov 16 '19 at 01:18
  • @user3121023, No, the loop increment has not been incremented before the call to `scanf()`. the 'step' parameter of a `for()` statement is applied at the end of the loop, not before – user3629249 Nov 16 '19 at 01:23
  • regarding: `for (int i = 0; i <= 80 || str[i] == '\n'; ++i) {` The expression: `str[i] == '\n'` will end the loop as soon as a NON '\n' character is entered. Suggest: `for (size_t i = 0; i < 80 && str[i] != '\n'; ++i) {` Note: both of the 'comparison' conditions must be TRUE to stay in the loop. Suggest using `size_t` rather than `int` as the index can never be less than 0 – user3629249 Nov 16 '19 at 01:26

3 Answers3

3

You are using scanf_s with %c specifier incorrectly.

Please take notice of compiler warnings, there is a size argument missing.

Microsoft's scanf_s is not a direct replacement for scanf.

Unlike scanf ... scanf_s ... requires 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.

scanf_s("%c", &str[i], 1);

You might also want to filter out any newline which may have been left in the buffer, with

scanf_s(" %c", &str[i], 1);

notice the added space.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
2

Why your code is showing this type of behaviour...

  1. use scanf instead of scanf_s

  2. the conditions you have provided in the for loop are wrong

    #include <stdio.h>
    int main()
    {
    char ch;
    char str[1000];
    int i;
    for (i = 0; i <= 80 ; i++)
    {
    
        scanf("%c", &str[i]);
        if(str[i]=='\n')
        {
            str[i]='\0';
            break;
        }
    }
    printf(str);
    }
    

I could show you the same task in simple manner. I have tried to answer your question in your way. That's why it may seem complicated.

Kazi Ziaul Hassan
  • 280
  • 1
  • 4
  • 17
  • regarding: `for (i = -1; i <= 80 && str[i] != '\n' ; ) { ++i; scanf("%c", &str[i]);` This is a bad idea. Suggest: `for ( size_t i = 0; i < sizeof( str ) && str[i] != '\n' ; i++) { scanf("%c", &str[i]);` however, checking the contents of `str[]` before it has been initialized is undefined behavior as the `str[]` will contain what every trash in on the stack at the location(s) of `str[]` Therefore suggest declaring `str[]` via: `char str[1000] = {'\0'};` – user3629249 Nov 16 '19 at 01:14
  • regarding: `using namespace std;` This is a C++ language statement. in C, the code will fail to compile – user3629249 Nov 16 '19 at 01:20
  • I have edited @user3629249. By the way your suggested code is not going to work. I checked it. I made a mistake I ran my previous code on .cpp file. Thanks for mentioning the mistakes. – Kazi Ziaul Hassan Nov 16 '19 at 01:53
0
#include <stdio.h>
#define MAX 25
int main()
{
    char buf[MAX];
    fgets(buf, MAX, stdin);
    printf("%s\n", buf);

    return 0;
}

fgets- Reads until new line character encountered or maximum limit of character array.