menu selection trouble

This is a discussion on menu selection trouble within the C Programming forums, part of the General Programming Boards category; i am having trouble making a new selection from this menu of 4 items. it seems like when it asks ...

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

    Question menu selection trouble

    i am having trouble making a new selection from this menu of 4 items. it seems like when it asks for a selection i have to type it in twice, and when i want to quit it picks up the 'q' the second time.
    i suspect the get_first() function has something to do with it. somehow the char is not picked up with the first try.
    thanx in advance.

    Code:
    			/*  Oct, 02    rev38  */
    
    #include <stdio.h>
    #define PI 3.14
    #define STAR "*******************************************************"
    
    char get_choice(void);
    char get_first(void);
    float sfm(void);
    float rpm(void);
    float ipm(void);
    float fpt(void);
    
    
    int main(void)
    
    
    {
    int choice;
    	
    	while ((choice = get_choice()) != 'q')
    	{
    		switch (choice)
    		{
    		case '1' : sfm();
    			break;
    		case '2' : rpm();	
    			break;
    		case '3' : ipm();
    			break;
    		case '4' : fpt();
    			break;
    	
    		default  : printf("Program error!\n");
    			break;
    		}
    	}
    	printf("Bye.\n");
    	return 0;
    }
    
    
    
    char get_choice(void)   // menu function
    {
    	int ch;
    
    	printf("%s\n",STAR);
    	printf("1: SFM having cutter Dia and RPM\n");
    	printf("2: RPM having SFM and cutter Dia\n");
    	printf("3: IPM having Feed per Tooth, Number of Teeth and RPM\n");
    	printf("4: FPT having IPM Number of Teeth and RPM\n");
    	printf("%s\n",STAR);
        printf("   Q to quit\n");
    
    	printf("ENTER CHOICE: ");
    	
    	ch = get_first();
    	
    
    
    	while ((ch < '1' || ch > '4') && ch != 'q')
    	{
    		printf("choose from the above numbers or Q to quit.");
    		ch = get_first();	
    	}
    	return ch;
    }
    
    
    char get_first()
    {
    	int ch;
    
    	ch = getchar();
    	while (getchar() != '\n' && ch != 'q')
    	
    	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("\n 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("\n 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("\n 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("\n FPT is equal to %.2f\n\n\n",fpt_);
    
    	return 0;
    }

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > ch = getchar();
    > while (getchar() != '\n' && ch != 'q')
    Erm, what?
    Your variable, ch, is going to act as getchar() essentially. So, you're testing to see if somebody presses enter in your first if statement's condition with plain ol' getchar(). In the second condition, which ALSO has to be true at the same time, you're testing for the letter Q with your variable ch, which is acting as getchar().

    *scratches head* Eh..that seems to be not quite the best of selection menus. What are your requirements? Maybe you need to do a little bit more planning.

    Try building your program's code around a menu similar to this:
    Code:
    /*
    
    	Program:
    	Menu.c
    	
    	Purpose:
    	Demontrates a simple menu.
    
    */
    
    #include <stdio.h>
    
    int PrintMenu ( void );
    
    int main ( void )
    {
    	char choice;
    	while ( PrintMenu() == 0 )
    	{
    		choice = getchar();
    		switch ( choice )
    		{
    			case '1':
    			printf("To be, or not to be.\n\n");
    			printf("Press enter to continue..");
    			getchar();
    			break;
    			
    			case '2':
    			printf("Hello, world.\n\n");
    			printf("Press enter to continue..");
    			getchar();
    			break;
    			
    			case 27:
    			printf("Closing..");
    			return 0;
    			break;
    			
    			default:
    			break;
    		}
    	}
    	return 0;
    }
    
    int PrintMenu ( void )
    {
    	printf("Choose a message to print:\n\n");
    	printf("1.  To be, or not to be.\n");
    	printf("2.  Hello, world.\n\n");
    	printf("Escape exits.\n\n");
    	return 0;
    }
    Please use code tags
    The world is waiting. I must leave you now.

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Re: menu selection trouble

    Spectrum48k,

    "continue" is bad structure for the language, anyway, it's encourage ppl not to use continue, break, goto ... this code could make your whole of your program unstructure and hard to solve it if you wan to expand your program.

  4. #4
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    Shadow,

    i have some suggestion on your program, want to hear it? why not using <conio.h> to control your "escape" key?

    for eg :
    Code:
    #include <stdio.h>
    ..
    ..
    char key;
    ..
    ..
    printf ("Enter key > ");
    getch (key);
    
    switch (key) {
      case '1' : ....
    ..
    ..
      case '27' : ... //escape the programm
    }
    
    ..
    ..
    ..
    so when user just hit a key (eg : 'esc' key once, no need to hit enter again), the input entered will save into variable.

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Shadow,
    > i have some suggestion on your program, want to hear it?
    Certainly.

    > why not using <conio.h> to control your "escape" key?
    Because, conio.h isn't standard. The ASCII value for escape, is. With you're code, the user will have to enter the number 27 into the function getch() which will only accept ONE character before executing.

    > so when user just hit a key, the input entered will save into variable.
    That's what already happens, have you even compiled the code?

    > (eg : 'esc' key once, no need to hit enter again)
    But, there is no need to hit enter after escape because I'm testing for the ASCII value of escape. Try something like printf("%c", 60); - refer to an ASCII chart.

    And once again, I didn't use conio.h because it isn't standard. Only dos compilers have it and the fact that you have to hit enter after inputting your choice is good because then you'll get rid of quite a few accidental choices.
    The world is waiting. I must leave you now.

  6. #6
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    > Shadow,
    > i have some suggestion on your program, want to hear it?
    Certainly. // you're welcome!!

    ------------------------
    > why not using <conio.h> to control your "escape" key?
    Because, conio.h isn't standard. The ASCII value for escape, is. With you're code, the user will have to enter the number 27 into the function getch() which will only accept ONE character before executing.
    -------------------------
    [reply]
    actually, i agree and disagree about your comment.

    agree :
    --> conio.h isn't standard . that's why the book of C didn't teach us about <conio.h>, but at least we should know something.

    disagree :
    --> why not? user entered a key (hit esc key) and then save it to variable that declared as character. and the integer value of character can be known, isn't it? eg : 'esc' => 27 , 'a' == 62 ..
    --> you may set the variable with a lot of char (just like strings) with loop statement.


    > so when user just hit a key, the input entered will save into variable.
    That's what already happens, have you even compiled the code?

    > (eg : 'esc' key once, no need to hit enter again)
    But, there is no need to hit enter after escape because I'm testing for the ASCII
    value of escape. Try something like printf("%c", 60); - refer to an ASCII chart.

    And once again, I didn't use conio.h because it isn't standard. Only dos compilers
    have it and the fact that you have to hit enter after inputting your choice is good because then you'll get rid of quite a few accidental choices.
    no comments

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    switch (key) {
      case '1' : ....
    ..
    ..
      case '27' : ... //escape the programm
    }
    This code is invalid. '27' is the bad section of code. You're confusing characters with their decimal value:

    case 27: // the decimalvalue of the escape sequence
    case '1': //the character 1.
    case 1: //the decimal value "one"

    case '27': //compiler error.

    You cannot have more than one character in a single quotation set unless it is the "escape code" and the value to be escaped. Example:

    case '\n': //the character for newline
    case '\\': // the backslash character
    case '\r': //the character for carrage return

    Now for some bad ones:

    case 'ab'
    case '01'
    case '0x'

    All those are invalid becaus they're not valid "escaped characters".

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

  8. #8
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    no comments, post wrong word, i can't realise it

  9. #9
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    beely, your last 2 posts have really confused me.
    What exactly is your point?
    Bah, I should open up a translator or something.

    - Quzah
    My menu code was good enough, was it not?
    Tight, small, an works.
    The world is waiting. I must leave you now.

  10. #10
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    to shadows who are confusing :
    no comments, post wrong word, i can't realise it

    but somethings i just wan to mention is "esc" key. you shouldn't tell user to key 'esc'. coz, user hit 'esc' key and then they should have to hit the 'enter' key. isn't it ?

  11. #11
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    but somethings i just wan to mention is "esc" key. you shouldn't tell user to key 'esc'. coz, user hit 'esc' key and then they should have to hit the 'enter' key. isn't it ?
    Hitting escape will immediately close the menu.
    Anything else requires an enter after it.
    The world is waiting. I must leave you now.

  12. #12
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    really?
    for eg :

    --------------------------------------
    enter menu > <ESC><ENTER>

    thank you, you have exit this program
    --------------------------------------

    coz you are using scanf to get the input, user should have to enter it after entered the input (esc key)

  13. #13
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    arg, beely, please work on your english

    > coz you are using scanf to get the input
    getchar(), not scanf

    But, my code has faults..that I now realised.
    case 27 is for getch() which is in conio.h, which is non-standard

    You can use q for escaping out of a program with getchar()
    Code:
    /*
    
    	Program:
    	Menu.c
    	
    	Purpose:
    	Demontrates a simple menu.
    
    */
    
    #include <stdio.h>
    
    int PrintMenu ( void );
    
    int main ( void )
    {
    	char choice;
    	while ( PrintMenu() == 0 )
    	{
    		choice = getchar();
    		switch ( choice )
    		{
    			case '1':
    			printf("To be, or not to be.\n\n");
    			printf("Press enter to continue..");
    			break;
    			
    			case '2':
    			printf("Hello, world.\n\n");
    			printf("Press enter to continue..");
    			break;
    			
    			case 'q':
    			printf("Closing..");
    			return 0;
    			break;
    			
    			default:
    			break;
    		}
    	}
    	return 0;
    }
    
    int PrintMenu ( void )
    {
    	printf("Choose a message to print:\n\n");
    	printf("1.  To be, or not to be.\n");
    	printf("2.  Hello, world.\n\n");
    	printf("q exits.\n\n");
    	return 0;
    }
    Code:
    /*
    
    	Program:
    	Menu.c
    	
    	Purpose:
    	Demontrates a simple menu.
    
    */
    
    #include <stdio.h>
    #include <conio.h>
    
    int PrintMenu ( void );
    
    int main ( void )
    {
    	char choice;
    	while ( PrintMenu() == 0 )
    	{
    		choice = getch();
    		switch ( choice )
    		{
    			case '1':
    			printf("To be, or not to be.\n\n");
    			printf("Press enter to continue..");
    			break;
    			
    			case '2':
    			printf("Hello, world.\n\n");
    			printf("Press enter to continue..");
    			break;
    			
    			case 27:
    			printf("Closing..");
    			return 0;
    			break;
    			
    			default:
    			break;
    		}
    	}
    	return 0;
    }
    
    int PrintMenu ( void )
    {
    	printf("Choose a message to print:\n\n");
    	printf("1.  To be, or not to be.\n");
    	printf("2.  Hello, world.\n\n");
    	printf("Escape exits.\n\n");
    	return 0;
    }
    There's both versions(I tested both with mingw/gcc). Sorry for the errors, I threw them up in a hurry.
    The world is waiting. I must leave you now.

  14. #14
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    yo my friends, dont be so angry, man.
    angry will cause conflict
    i know my english is suc+, sigh.

    well i got your meaning right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM

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