menu problems with functions

This is a discussion on menu problems with functions within the C Programming forums, part of the General Programming Boards category; with the following program instaled on an old machine with a pc based control running DOS, i am trying to ...

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    Question menu problems with functions

    with the following program instaled on an old machine with a pc based control running DOS, i am trying to get some data out of these functions. dont worry about the numbers you enter. the problem is that after it runs one of the functions and asks for a new choice, i have to enter the choice twice in order for it to be picked up. or press ENTER and then the choice. sort of like my grand father that never hears the first time

    thnx in advance..



    // Milling
    #include <stdio.h>
    #define PI 3.14


    char get_choice(void);
    char get_first(void);
    float sfm(void);
    float rpm(void);
    float ipm(void);
    float fpt(void);
    //float hp(float DepthOfCut, float WidthOfCut, float feed, float MachinabilityConstant);

    int main(void)


    {
    int choice;

    while ((choice = get_choice()) != 'q')
    {
    switch (choice)
    {
    case 'a' : sfm();
    break;
    case 'b' : rpm();
    break;
    case 'c' : ipm();
    break;
    case 'd' : fpt();
    break;
    // case 'e' : hp();
    // break;
    default : printf("Program error!\n");
    break;
    }
    }
    printf("Bye.\n");
    return 0;
    }



    char get_choice(void) // menu function
    {
    int ch;

    printf("a: SFM having cutter Dia and RPM.\n");
    printf("b: RPM having SFM and cutter Dia.\n");
    printf("c: IPM having Feed per Tooth, Number of Teeth and RPM.\n");
    printf("d: FPT having IPM Number of Teeth and RPM.\n");
    //printf("e: HP having Depth of Cut, Width of Cut and feed.\n\n");
    printf("ENTER CHOICE: ");
    ch = get_first();

    while ((ch < 'a' || ch > 'e') && ch != 'q')
    {
    printf("choose from the above numbers or Q to quit.");
    ch = get_first();
    }
    return ch;
    }


    char get_first(void)
    {
    int ch;

    ch = getchar();
    while (getchar() != '\n')
    continue;
    return ch;
    }


    float sfm(void)
    {
    float doc_; // depth of cut
    float rpm_; // rpm
    float sfm_; // surface feet per minute

    printf("Enter Diameter of Cutter ");
    scanf("%f", &doc_);
    printf("Enter RPM: ");
    scanf("%f", &rpm_);

    sfm_ = (PI * doc_ * rpm_) / 12;
    printf("SFM is equal to %.2f.\n\n\n",sfm_);

    return 0;
    }


    float rpm(void)
    {
    float sfm_; // surface feet per minute
    float cd_; // cutter diameter
    float rpm_; // rpm

    printf("Enter SFM: ");
    scanf("%f", &sfm_);
    printf("Enter Cutter Diameter: ");
    scanf("%f", &cd_);

    rpm_ = (sfm_ * 12) / (PI * cd_);
    printf("RPM is equal to %.2f.\n\n\n",rpm_);

    return 0;
    }


    float ipm(void)
    {
    float fpt_; // feed per tooth
    float not_; // number of teeth
    float rpm_; // rpm
    float ipm_; // inches per minute

    printf("Enter Feed per Tooth: ");
    scanf("%f", &fpt_);
    printf("Enter Number of Teeth: ");
    scanf("%f", &not_);
    printf("Enter RPM: ");
    scanf("%f", &rpm_);

    ipm_ = (fpt_ * not_ * rpm_);
    printf("IPM is equal to %.2f.\n\n\n",ipm_);

    return 0;
    }


    float fpt(void)
    {
    float ipm_; // inches per minute
    float not_; // number of teeth
    float rpm_; // rpm
    float fpt_; // feed per tooth

    printf("Enter ipm: ");
    scanf("%f", &ipm_);
    printf("Enter Number of Teeth: ");
    scanf("%f", &not_);
    printf("Enter RPM: ");
    scanf("%f", &rpm_);

    fpt_ = (ipm_) / (not_ * rpm_);
    printf("FPT is equal to %.2f.\n\n\n",fpt_);

    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    while (getchar() != '\n')
    continue;
    There is no need for the word 'continue'.

    while( getchar( ) != '\n' );

    That'll work just as well.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    RE: Continue

    i actually pasted that from another program. i bet if i typed instead of being lazy, i would not have put it there

    thnx..
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i have to enter the choice twice in order for it to be picked up.
    The problem is exactly what you try to avoid in your get_first() function. This works just fine as it reads the correct character and then flushes the input buffer as designed. The problem is that your further calls to input inside of the other functions leave extraneous data in the stream which is then used by get_first(). This data is not what the program expects so it prints the warning and promts for correct input. At this point though, get_first() has already flushed the stream so the next read works as intended. This can all be fixed by flushing before continuing your driver loop, like so:
    Code:
    int main(void)
    { 
      int choice; 
      
      while ((choice = get_choice()) != 'q') 
      { 
        switch (choice) 
        { 
        case 'a' : sfm(); 
          break; 
        case 'b' : rpm(); 
          break; 
        case 'c' : ipm(); 
          break; 
        case 'd' : fpt(); 
          break; 
          // case 'e' : hp(); 
          // break; 
        default : printf("Program error!\n"); 
          break; 
        }
        while ( getchar() != '\n' );
      } 
      printf("Bye.\n"); 
      return 0; 
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  2. Problems with my menu
    By Olidivera in forum Windows Programming
    Replies: 2
    Last Post: 07-07-2005, 01:44 PM
  3. Menu problems
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2002, 10:41 PM
  4. Problems with audio functions, tray icons + more!
    By face_master in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2002, 11:35 PM
  5. problems with functions
    By catalyst in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 06:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21