Q:

Simple C code not working properly when input.txt file is passed to it

So I have written a simple C code which generates N members of Fibonacci then writes every member into a linked list node then writes it all out.
I code in windows in Geany, but I've tested the code in ubuntu cause gcc works weird in windows. I've written the code, it compiles builds and runs, when I input everything it works good, everything is fine. 
For example the input is 1 5 2 3. 
1 is the menu choice for entering the N number of members, 
5 is the number of members the user chooses, 
2 is for generating the nodes and writing it in the nodes and 
3 is for printing it all out. 
It all works well and good there but the problem is, when I take an in.txt file with 1 5 2 3 written in it and pass it to the code with the 
cat in.txt | ./fibonacci > out.txt command in Ubuntu, my out.txt file is HUGE, with loads of the repeated outputs. If i just enter number 3 directly I get a message saying "The list is empty", which is fine. 
But when I enter only a number 3 into my in.txt file and run the command I get 150 MB of "the list is empty" in my out.txt. So when I turn in my assignement into some kind of automated assignement checker we have on my college the same thing happens and the assignment gives me a big red no-no cross. I don't really understand what the problem is, why do I have a perfectly functioning program when it's run directly but when an input file is passed to it with one single number 3 I get infinite "the list is empty" messages as my output in my out.txt file.
Here is the code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#ifndef DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#endif

typedef struct Node {
    struct Node *next;
    int val;
} node;

int entry()
{
    int n;
    scanf("%d", &n);
    if(n <= 0) return -1;

    return n;

}
int make_list ( int val, node **head )
{
    node *new, *current;
    new=(node*) malloc(sizeof(node));
    if(!new) return -1;

    new->val = val;
    new->next=NULL;

    if(!*head){
        *head = new;
    }else {
        for(current=*head; current->next != NULL; current= current->next);
        current->next = new;
    }

return 0;
}

int fibonacci ( int n, node **head ){
    int i, f1=0, f2=1, next=0;
    if ( n <= 0 ) return -1;
    for(i=1; i<n; i++){
    if(i==1){
        make_list(f1, head);
        continue;
    }
    if(i==2){
        make_list(f2, head);
        continue;
    }
    next = f1+f2;
    f1=f2;
    f2=next;
    make_list(next, head);
}
return 1;
}

int print( node *head){
node *current;

if(!head) return -1;

for(current=head; current!=NULL; current=current->next){
    printf("%d ", current->val);
}
return 0;
}

int main(int argc, char **argv)
{
node *head=NULL;
char menu_choice;
int retval, n;

do {
    DEBUG("\n(1) Enter number of Fibonacci members \n(2) Generate N numbers             of Fibonacci \n(3) Print out list \n(e) Exit\n");

    scanf(" %c", &menu_choice);
    switch (menu_choice) {
        case '1':
            retval = entry();
            n = retval;
            if (retval == -1) printf("You entered negative number\n");
            break;
        case '2':
            retval = fibonacci(n+1, &head);
            if (retval==1) printf("Numbers are generated\n");
            if (retval==-1) printf("Not possible to generate numbers\n");
            break;
        case '3':
            retval = print(head);
            if (retval==-1) printf("List is empty\n");
            break;
    }
} while(menu_choice!='e');

return 0;

}

A:

Your input doesn't contain the program terminating command 'e'. In addition you are not checking the result of scanf. So these two in conjunction are making your program to loop infinitely with the stale value in the menu_choice which is '3' in your case, writing the corresponding output to the file.

