Allow users to quit from a text-based program in C
This post is about creating a program in C where it let's the user decide when to quit.
October 26, 2024
Reading Time: 4 minutes
Introduction
Most when when getting started with programming in C or C++ or any other language that let us create a text-based program, the programs we create a commonly run once and then quit.
In this post, we will see how do we create a program in C that continuously run until the user decided to quit.
Now, most people that uses text-based programs knows that we can use Ctrl+C to quit, but it is still a little more elegant for users knowing that a command is available to them inside the program that let them stop or quit it right? Now let's get to it.
Sample Program
Now here is a simple program that converts a temperature value between celcius and fahrenheit.
#include <stdio.h>
int main(int argc, char const *argv[])
{
// Display program information.
printf("---------------------------------------------------------\n");
printf("| Program Name:\tTemperature Converter \t\t\t|\n");
printf("| Description:\tConvert between temperature values. \t|\n");
printf("--------------------------------------------------------\n");
char input;
float cel;
float fah;
printf("Select input [c - Celcius | f - Fahrenheit]: ");
scanf("%c", &input);
printf("Enter temperature value: ");
if (input == 'c') {
scanf("%f", &cel);
} else if (input == 'f') {
scanf("%f", &fah);
} else {
printf("Invalid input condition. Program will exit now.\n");
return 1;
}
if (input == 'c') {
fah = cel * 9.0 / 5 + 32;
printf("Temperature in fahrenheit is: %.2f\n", fah);
} else {
cel = 5.0 / 9 * (fah - 32);
printf("Temperature in celcius is: %.2f\n", cel);
}
return 0;
}
If we compile the program and run it, we will be asked an input type and then a value, then it will show the result then quit.
Now let's try to add an infinite loop. For this we are going to need a boolean so we will include the <stdbool.h>
header file from the standard library.
Our code will now be:
#include <stdio.h>
#include <stdbool.h>
int main(int argc, char const *argv[])
{
// Display program information.
printf("---------------------------------------------------------\n");
printf("| Program Name:\tTemperature Converter \t\t\t|\n");
printf("| Description:\tConvert between temperature values. \t|\n");
printf("--------------------------------------------------------\n");
char input;
float cel;
float fah;
while (true) {
printf("Select input [c - Celcius | f - Fahrenheit | q - Quit]: ");
scanf("%c", &input);
if (input == 'q') {
return 0;
}
printf("Enter temperature value: ");
if (input == 'c') {
scanf("%f", &cel);
} else if (input == 'f') {
scanf("%f", &fah);
} else {
printf("Invalid input condition. Program will exit now.\n");
return 1;
}
if (input == 'c') {
fah = cel * 9.0 / 5 + 32;
printf("Temperature in fahrenheit is: %.2f\n", fah);
} else {
cel = 5.0 / 9 * (fah - 32);
printf("Temperature in celcius is: %.2f\n", cel);
}
}
return 0;
}
On the above code, we added a while loop and we also added a condition where if the user typed in the character 'q', the program will exit. This way, it should run continuously asking the user for the input. Let's try to compile and run it.
As we can see in the preview above, the program exited unexpectedly. This unexpected behavior is due to the fact that it also reads the previous inputted whitespace, or in our case, when we pressed the return
key.
To solve this, we will put a space in front of the "%c" in our scanf
call, this will ignore any whitespaces entered from the previous input of the user.
#include <stdio.h>
#include <stdbool.h>
int main(int argc, char const *argv[])
{
// Display program information.
printf("---------------------------------------------------------\n");
printf("| Program Name:\tTemperature Converter \t\t\t|\n");
printf("| Description:\tConvert between temperature values. \t|\n");
printf("--------------------------------------------------------\n");
char input;
float cel;
float fah;
while (true) {
printf("Select input [c - Celcius | f - Fahrenheit | q - Quit]: ");
scanf(" %c", &input);
if (input == 'q') {
printf("The program will now exit.\n");
return 0;
}
printf("Enter temperature value: ");
if (input == 'c') {
scanf("%f", &cel);
} else if (input == 'f') {
scanf("%f", &fah);
} else {
printf("Invalid input condition. Program will exit now.\n");
return 1;
}
if (input == 'c') {
fah = cel * 9.0 / 5 + 32;
printf("Temperature in fahrenheit is: %.2f\n", fah);
} else {
cel = 5.0 / 9 * (fah - 32);
printf("Temperature in celcius is: %.2f\n", cel);
}
}
return 0;
}
Now if we compile and run this program:
The program now runs as expected. I hope you get value from this content!
Leave a comment below for any questions or suggestions.