Now the program is hanging after returning to the main while loop. It is not getting stuck in an infinite loop, but it still does not respond to the scanf. For example, user first selects option 1 and enters a 3 or 4 data lines and then hits EOF (ctrl-z or ctrl-d), then it returns to the main while loop and prints
"This program will perform the following: 1,2,3
select 1,2,3"
but then after that it hangs, will not respond to the scanf:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINES 10
int main (void)
{
int counter, printer_counter=1;
float data[MAXLINES];
int array_index=0;
int x;
int menu_selection = 0;
int program_continue = 1;
while (program_continue == 1)
{
printf("This program will perform the following: 1,2,3\n");
printf("select 1,2,3\n");
scanf(" %d",&menu_selection);
while(getchar() != '\n');
switch (menu_selection)
{
case 1:
counter=1;
while ( (array_index <= MAXLINES-1) )
{
printf("enter data line #%d:",counter);
scanf(" %f",&data[array_index]);
array_index++;
counter++;
if ( ( x=getchar() ) == EOF)
{
array_index = MAXLINES+1;
}
}
if (counter > MAXLINES)
printf("\n\nNumber of data lines: %d\n\n\n",counter-1);
if (counter < MAXLINES)
printf("\n\nNumber of data lines: %d\n\n\n",counter-2);
array_index = 0;
break;
case 2:
printf("CASE 2 testing\n\n");
while ( (array_index < MAXLINES) || printer_counter < counter)
{
printf("data line #%d:",printer_counter);
printf("%f\n",data[array_index]);
array_index++;
printer_counter++;
}
array_index = 0;
printer_counter = 1;
printf("\n");
break;
case 3:
printf("goodbye!");
program_continue = 0;
break;
default: printf("That is not a valid entry.\n");
}
}
return 0;
}
Any suggestions?