2

I'm trying to make a program I already made more organized by using functions. the purpose of it is processing a menu with several options for calculating different values. This code is for processing the Main menu, and the error I'm getting is that every character I type comes out as invalid (activates the default case in the switch) even if it is 1, 2 or 3, which are the possible options. What am I doing wrong?

void process_main_menu(){ int c; print_main_menu(); int option=getchar(); while((c=getchar())!='\n' && c!=EOF); switch(option){ case 1: program_state=ST_MENU_BASS; break; case 2: program_state=ST_MENU_TREBLE; break; case 3: program_state=ST_EXIT_PROGRAM; break; default: fprintf(stderr, "%s\n", MSG_INVALID_NUMBER); program_state=ST_MAIN_MENU; } } 

I'm updating the code as I see it wasn't complete enough. I'm actually using macros for this

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #define OPT_MENU_BASS 1 #define OPT_MENU_TREBLE 2 #define OPT_EXIT_PROGRAM 3 typedef enum { ST_MAIN_MENU, ST_MENU_BASS, ST_MENU_TREBLE, ST_EXIT_PROGRAM, ST_MENU_TREBLE_FREQ, ST_MENU_TREBLE_GAIN, ST_MENU_TREBLE_FREQ_FREQ_TREBLE, ST_MENU_TREBLE_FREQ_RESISTOR_3, ST_MENU_TREBLE_FREQ_CAPACITOR_3, ST_MENU_TREBLE_GAIN_RES5, ST_MENU_BASS_FREQ, ST_MENU_BASS_GAIN, ST_MENU_BASS_FREQ_FREQ_BASS, ST_MENU_BASS_FREQ_RESISTOR_2, ST_MENU_BASS_FREQ_CAPACITOR_1, ST_MENU_BASS_GAIN_RESISTOR_1, } state_t; state_t program_state; void process_main_menu(){ int c; print_main_menu(); char option=getchar(); while((c=getchar())!='\n' && c!=EOF); switch(option){ case OPT_MENU_BASS: program_state=ST_MENU_BASS; break; case OPT_MENU_TREBLE: program_state=ST_MENU_TREBLE; break; case OPT_EXIT_PROGRAM: program_state=ST_EXIT_PROGRAM; break; default: fprintf(stderr, "%s\n", MSG_INVALID_NUMBER); program_state=ST_MAIN_MENU; } } 
3
  • 1
    The most basic debugging technique when something goes wrong like this is 'print the value the program actually read'. Include fprintf(stderr, "Got %d ('%c')\n", option, option); in the default clause and you'd see the information that you typed 49 ('1') which would strongly hint at why you have a problem. It takes, perhaps, a bit more experience to know to print both the numeric and character versions of the value in option, but it becomes automatic over time. Commented Oct 24, 2016 at 3:46
  • Thank you for your completed post. You still cannot use case OPT_MENU_BASS: unless you do case (OPT_MENU_BASS + '0'): because OPT_MENU_BASS is equal to decimal 1 not character '1' (which is actually decimal 49) or you can do switch(option - '0') to make option a decimal instead of a character. Look carefully at e.g. ASCII Table.com. Alternatively you could #define OPT_MENU_BASS '1', but the redefinition of OPT_MENU_BASS would likely cause issues elsewhere in your code. Commented Oct 24, 2016 at 4:01
  • Thank you for your answer, but all of those options still give me the same error. I'll figure it out somehow Commented Oct 24, 2016 at 4:24

1 Answer 1

4

You're reading in a character such as , which is stored as its ASCII code, not a numerical value.

You need to change your cases to look for the character '1', not the number 1.

 case '1': program_state=ST_MENU_BASS; break; case '2': program_state=ST_MENU_TREBLE; break; case '3': program_state=ST_EXIT_PROGRAM; break; 

EDIT:

Given the macros you're using, you need to modify the macros to represent the characters '1', '2' or '3' instead of the numbers 1, 2 or 3.

#define OPT_MENU_BASS '1' #define OPT_MENU_TREBLE '2' #define OPT_EXIT_PROGRAM '3' 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answering! Your solution works, but only for 2 and 3; if i type 1 it returns 2 messages of invalid numbers. Also, originally (and how it was supposed to be) replacing the numbers i had macros, like OPT_MENU_BASS assigned to values 1, 2 and 3, how could i do it using that?
Without seeing more of your code, all we can do is guess that you properly declare the global program_state and have properly defined the macros ST_MENU_BASS, ST_MENU_TREBLE, ST_EXIT_PROGRAM. We're good, but we cannot see your display from here...
@Andibadia See my edit. You need to change the macros to use character constants, not numerical constants.
Yes, that worked! At first it didn't but I had a stupid error in my code that made me think that it didn't work. Thank you very much
@Andibadia Glad I could help. Feel free to accept this answer if you found it useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.