Thread: Menu problem

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    1

    Menu problem

    I have a menu for a project that takes a string input and does whatever option you specify. It works perfectly EXCEPT when it goes through the while-loop (while you didn't say quit the menu reloads). It will spew out all the text in the menu like a bulimic teenager. Then it reloads properly and asks you for an input like nothing happened. While this is not a fatal error (code still functions), it is irksome!

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    double aa=0, ab=0, ac=0, sa=0, sb=0, sc=0;
    char a1[13], o1[]="AAS\n", o2[]="ASA\n", o3[]="SSA\n", o4[]="SAS\n", o5[]="SSS\n", o6[]= "quit\n";
    void AAS()
    {
        puts("Please enter Angle C");
        scanf("%lf", &ac);
        puts("Please enter Angle A");
        scanf("%lf", &aa);
        puts("Please enter Side C. Length is a maximum of 200");
        scanf("%lf", &sc);
        if (ac >= 180 || aa >= 180 || aa+ac >= 180 || aa <= 0 || ac <= 0 || sc > 200 || sc <= 0)
        {
            puts("Error, you have entered an invalid triangle");
            AAS(); /*Loops you back to the start of Angle-Angle-Side to enter a valid triangle*/
        }
        else
        {
            ab= 180-ac-aa;
            sb= sin(ab)*sc/sin(ac);
            sa= sin(aa)*sc/sin(ac);
            printf ("Angle A is %6.3lf Angle B is %6.3lf Angle C is %6.3lf\n Side A is %7.3lf Side B is %7.3lf Side C is %7.3lf \n \n", aa, ab, ac, sa, sb, sc);
        }
        return;}
    
    void ASA()
    {
        puts("Please enter");
        return;}
    
    void SSA()
    {
        puts("Please enter");
        return;}
    
    void SAS()
    {
        puts("Please enter");
        return;}
    
    void SSS()
    {
        puts("Please enter");
        return;}
    
    void menu()
        {
            puts("       C\n      / \\ \n     /   \\ \n  b /     \\ a \n   /       \\ \n  /         \\ \n A-----------B \n       c");
            puts("Please type the abbreviation of the method you would like to use (i.e. SSA, ASA, AAS)");
            fgets(a1, 13,stdin);
            
            if(strcmp(a1, o1)==0) /*I am using the strcmp function to see what the user inputs.*/
            {
                AAS();
                return;
            }
            else if(strcmp(a1, o2)==0)
            {
                ASA();
                return;
            }
            else if(strcmp(a1, o3)==0)
            {
                SSA();
                return;
            }
            else if(strcmp(a1, o4)==0)
            {
                SAS();
                return;
            }
            else if(strcmp(a1, o5)==0)
            {
                SSS();
                return;
            }
            else if(strcmp(a1, o6)==0) /*This is the "quit" option*/
            {
        return;
            }
            else if(strcmp(a1, o1)!=0 && strcmp(a1, o2)!=0 && strcmp(a1, o3)!=0 && strcmp(a1, o4)!=0 && strcmp(a1, o5)!=0 && strcmp(a1, o6)!=0)
            {
            puts("Sorry, I didn't understand you. Please use all caps for a method or type 'quit' to exit.\n");
            return;}
        }
    
    
    
    int main (void)
    {
         while(strcmp (a1,o6) != 0) 
         {
            menu();
        }
        
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ArriFerrari View Post
    It will spew out all the text in the menu like a bulimic teenager.
    That's pretty insensitive and rude, especially since there may be people with eating disorders here. Having known several people with eating disorders, it is not something you should joke about.

    Quote Originally Posted by ArriFerrari View Post
    I have a menu for a project that takes a string input and does whatever option you specify. It works perfectly EXCEPT when it goes through the while-loop (while you didn't say quit the menu reloads). It will spew out all the text in the menu <snipped>. Then it reloads properly and asks you for an input like nothing happened. While this is not a fatal error (code still functions), it is irksome!
    Your problem is that you're mixing line-based and formatted input methods (i.e. scanf and fgets+stdin). The scanf leaves the newline in the input buffer, so the next call to fgets sees just the newline, thinks the user entered an empty line, reads it and considers it invalid input. I suggest you always use fgets to read the input. I see you realize fgets keeps the newline if the user pressed enter, but you don't need to press enter to transmit input, you could send an EOF (unlikely, but possible), in which case your code would break. It's better to strip the newline using a method like the strchr trick here. Then, if you can use sscanf to scan the input string (that's what the extra 's' in the front signifies) and pull out numeric data (though I find strtol and strtod superior in terms of functionality and error handling). Make sure you check the value of scanf/sscanf/strtol/strtod to ensure the user entered valid numbers.

    All in all your program seems off to a good start. Some other notes/tips:

    • Global variables are evil. As a newbie, you should avoid them at all costs. Declare variables in the appropriate function, and pass them around as needed.
    • Recursion should only be used when it provides a significant benefit over the iterative version. In your case, using it to repeat code in the case of invalid input is not a good solution. If the user keeps entering bad input (imagine your teacher automates testing with a script), your program could crash.
    • There's no need for all those return; statements in menu(). When the program reaches the end of a void function, it automatically returns. Besides, mid-function returns should be used sparingly, as it makes reasoning about the code a bit more difficult.
    • Your final else-if in menu() (on line 84) could just be an else. If it's not any of o1 through o6 (which were covered by the previous if/else if conditions), then it must be an error.
    • Don't use magic numbers. #define constants MAX_INPUT_LEN, MAX_ANGLE and MAX_SIDE_LEN. You could define MIN_ANGLE and MIN_SIDE_LEN too, instead of using just plain 0.
    • 13 is not very much space for the input. If the user types more than that, your program will run into trouble. Use something big like 1024, there is plenty of memory. Also, #define a constant, e.g. MAX_INPUT_LEN, Call fgets like so: fgets(a1, sizeof(a1), stdin); That will ensure that you will always give fgets the right size, even if you change MAX_INPUT_LEN or change the definition of a1 all together (e.g. to use a different size like BUFSIZ).
    • You should make your notation in your diagram consistent with your input prompts. Specifically, line 13 uses an uppercase 'C' for a side length, but the diagram uses lowercase letters for sides. Seems pointless, but you would be surprised at just how bad users can screw things up.
    • A function should do one thing and do it well. Generally it's poor form to mix calculations and input/output in a function. If it were me, I would separate AAS() into do_AAS() and calc_AAS(), calc_AAS() would take the 3 inputs from the user, calculate the 3 outputs and pass them back. do_AAS() would get the input, call calc_AAS and print out the results. That makes your code more modular, so you could use calc_AAS later, should there be an assignment that requires you to read input from a file instead of the user.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,791
    Additional note/tip: sin, cos, etc have radians as the input not degrees as your code seems to be assuming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu problem.
    By And3rs in forum C++ Programming
    Replies: 25
    Last Post: 10-12-2008, 10:48 AM
  2. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 01:32 PM
  3. Menu Problem
    By vopo in forum Windows Programming
    Replies: 4
    Last Post: 10-06-2007, 07:57 PM
  4. MDI and MENU Problem
    By Marc in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2004, 06:59 PM
  5. Menu problem
    By morbuz in forum Windows Programming
    Replies: 5
    Last Post: 09-09-2001, 06:10 AM

Tags for this Thread