Home > Software engineering >  How to input stdin strings into array and print them? C Programming
How to input stdin strings into array and print them? C Programming

Time:10-04

I am practicing how to code C programs, specifically reading stdin statements. The following is a code I wrote to take in the stdin, but I am having trouble inputting them into an array and printing out the correct values.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char* argv[])
{
    int count = 0;
    char* number = "-n";
    int result;
    char numArray[50];

    result = strcmp(argv[1], number);
    if (result == 0) {
        printf("Numbers Only\n");
        while (!feof(stdin)) {
            if (feof(stdin))
                break;

            for (int i=0; i < sizeof(numArray); i  ){
                scanf("%s", &numArray[i]);
            }
        }
    }
    for (int i=0; i< sizeof(numArray); i  ){
        printf("%d\n", numArray[i]);
    }
}

I am working step by step, so my final code has something to do with manipulating the array and outputting it. However, my question is focusing solely on inputting the stdin into the array first since that is the big step and I will work on manipulating the array later.

The 'Numbers Only' is what I was using to check something out, so do not worry about that at all.

I do not get any errors for the code, but it gives weird outputs. One output is the following:

1                  (1, 2, 3 are what I inputted into terminal)
2
3
49
50
51
0
0
0
0
0
32
-56
109
-63
6
127
0
0
-128
80
110
-63
6
127
0
0
20
0
0
0
0
0
0
0
64
0
0
0
0
0
0
0
0
0
-16
-67
6
127
0
0
4
0

Can anyone explain why it outputs those other numbers when my stdin stops after I input 1 2 3 ctrl D and how I can stop that from happening? I want my array to be the size of how many numbers I input, but I am also having trouble with that if anyone has hints!

Thanks!

CodePudding user response:

The innermost loop is not necessary, only one loop is enough. When converting with scanf, check its return value. Also, only print the number of elements actually converted. Fixing these, a first corrected version is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char* argv[])
{   
    int count = 0;
    char* number = "-n";
    int result;
    int numArray[50];

    int i = 0;

    if (argc != 2)
        return EXIT_FAILURE;

    result = strcmp(argv[1], number);
    if (result == 0) {
        printf("Numbers Only\n");
        while (1) { 

          int number = 0;
          int ret = scanf("%d", &number);
          if (ret == 1)
            numArray[i  ] = number;

          if (i == 50 || ret < 1)
            break;
        }
    }
    for (int j=0; j< i; j  ){
        printf("%d\n", numArray[j]);
    }
}   

Testing:

$ gcc main.c && ./a.out -n  
Numbers Only
1   
2   
3   
<CTRL   d here>
1   
2   
3

Edits from comments: Removed while (!feof(stdin)), expanded error checking to avoid infinite loop and added a check before accessing argv.

  • Related