How to read 2 lines of integer input in C?

Keith Yong

I'm doing a project for my algorithms class and I'm having a lot of trouble with inputs. I'm trying to read an input like this:

6 0 2 3 1 3
5 9 2 1 3

The integers will need to go to

int num1; // num1 = 6
int num2; // num2 = 5
int array1[100]; // array1 = {0, 2, 3, 1, 3, 0, 0, ...}
int array2[100]; // array2 = {9, 2, 1, 3, 0, 0, ...}

The input will come from standard input, in the form of a file. So in terminal running the program would look like this:

cat input.txt | ./a.out

Where input.txt contains the two lines of integers.

Here is my flawed attempt so far:

while(scanf("%d%c", &temp, &ch) > 1){
    if (ch != '\n'){
        one[count] = temp;
    } 
    else if (ch == '\n'){
        count = 0;
        two[count] = temp;

    }
    one[count] = temp;
    count++;
    if (ch != ' ')
    {
        printf("Invalid input. Please do int + space.\n");
        return -1;
    }
    if ((temp >= 100) || (temp <= -100))
    {
        printf("Input is too big, must be between -100, 100.\n");
        return -1;
    }
    if (one[0] < 1){
        printf("Input for n cannot be smaller than one!");
        return -1;
    }
}

I think the main issue is that I'm just not sure how to deal with multiple lines of input. One line of input is fine by me but multiple lines is what trips me over.

5gon12eder

You could fetch an entire line of input using the getline function and then iterate over that line, scanning one number at a time using the strtol function.

From the example in your question I assume that you want all remaining entries in the two arrays to be zero so don't forget to zero them out (either manually or using the memset function.).

And also don't forget to free() the buffer getline gave you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read input in C

From Dev

How to read an integer and a char with read() function in C?

From Dev

How to ignore empty lines while getting integer input in python 2.7

From Dev

C programming read a integer from command line input string

From Java

How do i read multiple lines of input via the BufferedReader?

From Dev

How to read input lines with newline characters from command line?

From Dev

How to read an integer input from the user in Rust 1.0?

From Dev

How to read 3 Integer values in single line of input in Java?

From Dev

how i read new line in integer type input

From Dev

How can i read more than 2 lines in a text file? c#

From Dev

Read 2 bytes as integer

From Dev

Read() integer in c

From Dev

Checking if input is an integer in C

From Dev

How to read / parse input in C? The FAQ

From Dev

How to read hexadecimal numbers from input in C?

From Dev

How to input in an integer array

From Dev

How can I read the last 2 lines of a file in batch script

From Dev

How can I read lines from bottom up using C?

From Dev

How to read all lines of an csv file from HttpContent in C#

From Dev

How to print out specific lines of user input to console (C++)

From Dev

How to get integer input in an array using scanf in C?

From Dev

How to take input 128 bit unsigned integer in c++

From Dev

How to double a float/integer within a user input string in C?

From Dev

How to take input 128 bit unsigned integer in c++

From Dev

How to get Integer and float input without `scanf()` in c?

From Dev

How to split an input string in an integer array(c++)

From Dev

What could cause my program to only read 2 lines of a 3 line input

From Dev

Read multiple input lines using javascript

From Dev

Read any number of lines from standard input

Related Related

  1. 1

    How to read input in C

  2. 2

    How to read an integer and a char with read() function in C?

  3. 3

    How to ignore empty lines while getting integer input in python 2.7

  4. 4

    C programming read a integer from command line input string

  5. 5

    How do i read multiple lines of input via the BufferedReader?

  6. 6

    How to read input lines with newline characters from command line?

  7. 7

    How to read an integer input from the user in Rust 1.0?

  8. 8

    How to read 3 Integer values in single line of input in Java?

  9. 9

    how i read new line in integer type input

  10. 10

    How can i read more than 2 lines in a text file? c#

  11. 11

    Read 2 bytes as integer

  12. 12

    Read() integer in c

  13. 13

    Checking if input is an integer in C

  14. 14

    How to read / parse input in C? The FAQ

  15. 15

    How to read hexadecimal numbers from input in C?

  16. 16

    How to input in an integer array

  17. 17

    How can I read the last 2 lines of a file in batch script

  18. 18

    How can I read lines from bottom up using C?

  19. 19

    How to read all lines of an csv file from HttpContent in C#

  20. 20

    How to print out specific lines of user input to console (C++)

  21. 21

    How to get integer input in an array using scanf in C?

  22. 22

    How to take input 128 bit unsigned integer in c++

  23. 23

    How to double a float/integer within a user input string in C?

  24. 24

    How to take input 128 bit unsigned integer in c++

  25. 25

    How to get Integer and float input without `scanf()` in c?

  26. 26

    How to split an input string in an integer array(c++)

  27. 27

    What could cause my program to only read 2 lines of a 3 line input

  28. 28

    Read multiple input lines using javascript

  29. 29

    Read any number of lines from standard input

HotTag

Archive