0

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.

5
  • Do you know the number of numbers per line in advance? Commented Sep 15, 2014 at 0:04
  • It will always be 2 lines of numbers, but it would be nice to return an error if the user inputs 3 lines Commented Sep 15, 2014 at 0:05
  • 1
    That's easy. My question actually was about the number of numbers per line (that is: the “columns” per line), not the number of lines. Commented Sep 15, 2014 at 0:06
  • The max number of integers per line is 101, 100 because array[100] and 1 for the single integer from the first integer in the line. Commented Sep 15, 2014 at 0:09
  • Is the first number by any chance the number of elements in the line? If so then it would be very easy to read with scanf. Commented Sep 15, 2014 at 1:02

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Well if getline() returns a string, and I use sscanf to read over that string, how would I read two lines instead of one line? Maybe a better question is: does getline() return "6 0 2 3 1 3\n5 9 2 1 3" or "6 0 2 3 1 3"? Because in the former, I can just check for the '\n' and act accordingly.
It will return one line of input or "6 0 2 3 1 3\n" in your example. If you are done processing it, call getline again to fetch the next line. There is no need for you to scan for \n manually.
Thanks for answering my comments, the getline should work now :)
0

Actually I ended up using scanf, here is the working code below. It really helped to read some of these comments and also refer to K&R

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

#define ARRAY_SIZE  100

void shiftArrayBackByOne(int a[]){
    for(int i = 1; i <= ARRAY_SIZE; i++){
        a[i - 1] = a[i];
    }
}

void printArray(int a[], int n){
    for(int i = 0; i < n; i++){
        printf("%d ", a[i]);
    }
    putchar('\n');
}

int main(){
    int isLineTwo = 0;
    int countOne = 0;
    int countTwo = 0;
    int inputNum;
    int num1;
    int num2;
    int array1[ARRAY_SIZE];
    int array2[ARRAY_SIZE];

    char ch;

    while(scanf("%d%c", &inputNum, &ch) > 0){

        //Puts the input into different arrays depeding
        //on value of isLineTwo
        if (isLineTwo){
            array2[countOne] = inputNum;
            countOne++;
        } else {
            array1[countTwo] = inputNum;
            countTwo++;
        }

        //Increment isLineTwo if ch is a 'newline'
        if (ch == '\n')
        {
            isLineTwo++;
        }

        //Check if user inputs more than 2 lines
        if (isLineTwo > 1){
            printf("Hey, no more than 2 input lines!\n");
        }

    }
    printArray(array1, countOne);
    printArray(array2, countTwo);

    num1 = array1[0];
    num2 = array2[0];

    shiftArrayBackByOne(array1);
    shiftArrayBackByOne(array2);

    printf("num1 = %d\n", num1);
    printf("num2 = %d\n", num2);

    printArray(array1, countOne);
    printArray(array2, countTwo);
}

1 Comment

regarding this line: if (ch == '\n') this will not work with windows/dos as that OS uses a two char end of line marker.
0

Look at my code below. Maybe it helps you.

Generally, if you know how many numbers will be inputted, you can read numbers one by one using scanf("%d", ...) and use fflush() when the expected amount is met to clear any other numbers in the buffer. This example assumes that the first two numbers are the respective lengths of each line. The input could look like:

// example input:
// 4
// 3
// 1 2 3 4
// 5 6 7
    int main()
    {
        int it;
        int it1 = 0;
        int it2 = 0;
        int line1[100];
        int line2[100];

        scanf("%d", &it1); // amount of line 1 numbers
        scanf("%d", &it2); // amount of line 2 numbers

        it = 0;
        do
        {
            scanf("%d", &line1[it]);
        } while (++it < it1);

        fflush(stdin); // clear input buffer

        it = 0;
        do
        {
            scanf("%d", &line2[it]);
        } while (++it < it2);

        return 0;
    }

1 Comment

this line: scanf("%d", &it2); // amount of line 2 numbers will get the second number on the first line, not the first number on the second line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.