2

I have this program show, which allows me to display a menu with 3 choices. If you choose anything except exit (0) then the program continues to loop.

However, when I try to call a function from inside the switch statement, once the function is finished, the loop exists completely. I want to go back to the menu and continue unless I select exit.

The program works, without the function call. It also works if I select 2, triangle, it then stays in the loop.

Why is this happening, and how can I fix it,

many thanks.

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

int rows;

void xmasTree(void)
{ 
 printf("Enter Rows:\n>");
 scanf("%d",&rows);
}

int main (int argc, char ** argv)
{
    int flag = 1;
    while (flag)
    {
        //Print menu
        printf("1: Create xmas tree\n");
        printf("2: Create triangle\n");
        printf("0: exit\n");
        printf("Enter choice :");
        //read input
        char buffer[10];
        fgets(buffer, 10, stdin);
        //convert to number
        int number = atoi (buffer);

        //work on input
        switch (number)
        {
            case 1:
                printf("Building your xmas tree\n");
                xmasTree();
                break;
            case 2:
                printf("creating your triangle\n");
                break;
            case 0:
                printf("Exiting...\n");
                flag = 0;
                break;
            default:
                printf("INVAID INPUT\n");
                break;
        }
    }   
    return 0;
}   
  • Does it print "Exiting..."? Are you running your program from within Eclipse? – user253751 Feb 03 '20 at 13:29
  • 2
    That's because of the scanf. The newline is not consumed so you'll `atoi('\n')`. See https://stackoverflow.com/questions/3744776/simple-c-scanf-does-not-work – Mickael B. Feb 03 '20 at 13:30
  • 1
    I suspect the `scanf("%d",&rows);` having datas keps in the buffer, intercepted by `fgets(buffer, 10, stdin);` which will provide 0 to `int number = atoi (buffer);` (because `atoi()` returns 0 if it can't convert to int – Cid Feb 03 '20 at 13:30
  • regarding: `int rows;` and `scanf("%d",&rows);` Since `rows` is never used, this results in a warning from the compiler. – user3629249 Feb 05 '20 at 04:49
  • regarding: `int main (int argc, char ** argv)` since the parameters are not used, the compiler outputs two warning messages. To avoid the warning messages, Suggest using the other valid signature for `main()`: `int main( void )` – user3629249 Feb 05 '20 at 04:51
  • when calling functions like: `scanf()` and `fgets()`, always check the returned value (not the parameter values) to assure the operation was successful. – user3629249 Feb 05 '20 at 04:52

2 Answers2

4

The problem is, that here

scanf("%d",&rows);

you read a number from stdin, but you leave the trailing newline inside the stream!

Then, in the next loop iteration

fgets(buffer, 10, stdin);

reads the (empty) line and

int number = atoi (buffer);

sets number to 0, causing your program to exit.

One possible fix would be to read in rows with fgets() and atoi() as you do it with number.

Ctx
  • 18,090
  • 24
  • 36
  • 51
1

The problem is this call in the function xmasTree

scanf("%d",&rows);

After it there is stored the new line character '\n' in the buffer. So the next call of the function fgets reads an empty string. You should remove this new line character as for example

scanf("%d",&rows);
scanf( "%*[^\n]" );
scanf( "%*c" );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335