In this lesson we are going to learn how to build a simple menu in C using a for loop. In doing so we are going to look at the necessary looping structure as well as building up the C skills we have by using decision constructs with if and reading interactive user input with scanf. This lesson builds on what we have already gained in the previous lesson where we looked at building a simple C program .
Menus and continuous loops
In this C program we are going to create a loop, the loop will be continuous prompting whether to continue or to exit. If they choose to quit the program at this stage we have provided functionality to exit the continuous loop. It may seem strange wanting to create a continuous loop but this is the structure that we need for our menu, the menu will display each time we loop, or for each iteration of the loop. We may then make our selection and as long as the selection is not to exit the menu stays running. Later we will move this code where we process a date, having processed the date we can ask if there is another date to process. This is much better than the user having to start the program each time they need to process a new date. We will use a for loop in this example but equally we could use while to create the loop:
for (;;) {
} // continuous loop using for
while (1) {
} // continuous loop using while
Includes
The C program that we create, let’s call it, looptest.c, will start with the include statements.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
Remember that the # identifies a pre-processor command.
We need the stdio.h to read in data with scanf and display data with printf. We use ctype.h for the tolower function we use to convert input to a known case. It is the easy to check for q rather than q and Q. The finally library we include is stdlib.h. This provides the code we use for exit in this program.
main method/function
We have a simple main method here that accepts no arguments. The parenthesis or round brackets are empty in the method definition denoting it accepts no arguments.
main () {
}
Within the body main we first declare a variable identified by c that is of a char data-type, accepting a single byte or character.
char c;
For loop
We then declare the for loop, we have a decision constructor in the parenthesis that looks at the need of continuing to process the lop, and the braces define the code block that makes up each iteration of the loop.
for (;;) {
}
Normally the for loop decision within the parenthesis would have three parameters separated by semi-colons. Here we just have the semi-colons identifying a continuous loop. A non-continuous loop may look like this:
int x;
for ( x=1; x < 10; x++) {
}
Consider the code and associated output as follows , (x++ increments the value of the variable x by 1).
Within the code block we print two lines with printf before the interest begins with scanf. We can see that this is vaguely similar to printf, instead of writing to standard output we read from standard input. We read in a string and populate the variable c with the input.
scanf(“%s”,&c);
The following line getchar() strips out the carriage return when we submit the answer. With the two lines of printf that prompted us for input we now have a menu that asks us to enter y to continue and q to quit,
printf(“Enter y to continuen”);
printf(“Enter q to quit: “);
scanf(“%1s”,&c);
getchar();
Now we may know what the user means if the enter Q instead of q but the computes does not see and relationship between these two characters. No more than i with z. To make the if statement easier we convert the user input to a known case, it does not matter if it is upper or lower but we choose lowercase in the example.
c = tolower(c);
The tolower and toupper functions are from the ctype.h libraries.
If statement
We then use and if statement to check to see if our input is asking the application to quit or continue. the reality is that we just need to check for the q to quit in this case, anything else we can continue.
if ( c == 'q' ) { printf("Exiting on user requestn"); exit(0); }
Note :
- = is the assignment operator
- == is the equals to operator
The exit functions come from the stdlib.h library , exiting with a code of 0 indicates success, anything other than 0 would normally be used to show the program failed in some way.
The full source for this example:
#include <stdio.h> #include <ctype.h> #include <stdlib.h> main () { char c; int i; for ( i = 1 ; i < 4 ; i ++) { printf("Enter y to continuen"); printf("Enter q to quit: "); scanf("%1s",&c); getchar(); c = tolower(c); if ( c == 'q' ) { printf("Exiting on user requestn"); exit(0); } } }
Compile the code:
gcc -o looptest looptest.c
The video can be found online on YouTube: