The program is designed to collect and print data from user. It loops through the options. The user can input data, print the data to screen and then choose to input data again adding to the already exiting data or return to the menu. The problem is with appending the data. The program is not stopping for the user to enter yes or no in scanf under case 1 of the switch statement. It prints the printf statement that ask the user to append data, skips scanf and loops back to the menu.
Code:
#define MAX 200
#include <stdio.h>
#include <stdlib.h>
void collectData(float * a, int n);
void printData(float * a,int n);
int main()
{
int userChoice;
float array[MAX];
int counter = 0;
int size = 0;
char answer;
do
{
printf("This program will perform the following:\n");
printf("1. Enter data. \n");
printf("2. Display the data and the following statistics:the number of date item, the high and low values in the data,the mean, median, mode, variance and standard deviation.\n");
printf("3. Quit the Program\n");
printf("*********************************\n");
printf("Your choice:");
scanf("%d",&userChoice);
switch (userChoice)
{
case 1:
if (counter == 0)
{
collectData(array,MAX);
counter ++;
}
else if (counter > 0)
{
printf("Do you want to append data or start from the beginning (y/n).\n");
scanf("%c", &answer);
if (answer == 'y' || answer == 'Y')
{
collectData(array,MAX);
}
else if(answer == 'n' || answer == 'N');
{
}
}
break;
case 2:
printData(&array[0],MAX);
break;
case 3:
printf("Quitting program!\n");
break;
}
}
while (userChoice !=3);
return;
}
void collectData(float * a,int n)
{
int i = 0;
while (printf("Enter Stock price #%d :", i+1) && scanf("%f", &a[i])!=EOF)
++i;
}
void printData(float * a,int n)
{
int i;
for(i=0; i<n; i++)
{
printf("%2.2f\n",a[i]);
}
printf("\n");
}
Comment