By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

problem with menu driven that adds data to existing data in c program

P: n/a
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.

Expand|Select|Wrap|Line Numbers
  1. #define MAX 200
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. void collectData(float * a, int n);
  7. void printData(float * a,int n);
  8.  
  9.  
  10. int main()
  11. {
  12.     int userChoice;
  13.     float array[MAX];
  14.     int counter = 0;
  15.     int size = 0;
  16.     char answer;
  17.     do 
  18.     {
  19.     printf("This program will perform the    following:\n");
  20.     printf("1. Enter data. \n");
  21.     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");
  22.     printf("3. Quit the Program\n");
  23.     printf("*********************************\n");
  24.     printf("Your choice:");
  25.     scanf("%d",&userChoice);
  26.  
  27.     switch (userChoice)
  28.     {
  29.     case 1: 
  30.     if (counter == 0)
  31.     {
  32.     collectData(array,MAX);
  33.     counter ++;
  34.     }
  35.     else if (counter > 0)
  36.     {
  37.     printf("Do you want to append data or start from the beginning (y/n).\n");
  38.     scanf("%c", &answer);
  39.     if (answer == 'y' || answer == 'Y')
  40.         {
  41.         collectData(array,MAX);
  42.         }
  43.         else if(answer == 'n' || answer == 'N');
  44.         {
  45.  
  46.         }
  47.  
  48.     }
  49.     break;
  50.  
  51.     case 2:    
  52.     printData(&array[0],MAX);
  53.     break;
  54.  
  55.     case 3: 
  56.     printf("Quitting program!\n");
  57.     break;
  58.     }
  59.  
  60.     }
  61.     while (userChoice !=3);
  62.  
  63.  
  64.     return;
  65. }
  66.  
  67. void collectData(float * a,int n)
  68.     {
  69.     int i = 0;
  70.      while (printf("Enter Stock price #%d :", i+1) && scanf("%f", &a[i])!=EOF)
  71.         ++i;
  72.  
  73.     }
  74.  
  75.     void printData(float * a,int n)
  76.     {
  77.     int i;
  78.     for(i=0; i<n; i++)
  79.     {
  80.     printf("%2.2f\n",a[i]);
  81.  
  82.     }
  83.     printf("\n");
  84.     }
  85.  
  86.  
  87.  
Nov 26 '10 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,059
You probably rethink about line 70 in the code
Nov 27 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.