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;
}