An introduction to Ch: Implementing a temperature converter in Ch

Ch is a combined C shell and IDE

Let’s start by creating a new file called temp_converter.ch with the following statements:

#include <stdio.h>

void main (int argc, string_t argv[]) {
string_t temp_raw, scale_in_raw, scale_out_raw;

temp_raw = argv[1]; scale_in_raw = argv[2]; scale_out_raw = argv[3];

	printf("Input temperature: '%s' degrees\n", temp_raw);
printf("Input temperature scale: '%s'\n", scale_in_raw);
printf("Output temperature scale: '%s'\n", scale_out_raw);
}

The program first creates three string variables named temp_raw, scale_in_raw, and scale_out_raw, which store the input temperature, the input temperature’s scale, and the output temperature scale, respectively. Next, the program sets the value of temp_raw equal to the first command line argument (temp_raw = argv[1]), the value of scale_in_raw equal to the second command line argument (scale_in_raw = argv[2]), and the value of scale_out equal to the third command line argument (scale_out_raw = argv[3]). Finally, the program uses three printf statements to print out the values of these variables.

Assuming the file temp_converter.ch containing this program is located in the current directory, the program can be executed as follows:

/> ch ./temp_converter.ch

It should display the following output:

Input temperature: '' degrees
Input temperature scale: ''
Output temperature scale: ''

In its current state, the program accepts blank or null values for the input temperature, the input temperature’s scale, and the output temperature. Because the program needs a value for each of these inputs, the program needs to be able to determine if valid inputs have been provided. This can be accomplished this using the conditional if statement. The basic syntax of an if statement is as follows:

if (condition) {
first statement;
second statement;
…
last statement;
} else {
first statement;
second statement;
…
last statement;
}

When an if statement is executed, the condition is first checked. If the condition is true, then each statement until the else statement is executed. If, however, the condition is false, then each statement following the else statement is executed. The else statement and its associated statements are optional and may be omitted when there are no statements to execute when the condition is false.

The first form of input validation the program needs to perform is to check if at least three command line arguments were provided. This can be checked using the following if statement:

if (argc >= 4) {
temp_raw = argv[1];
scale_in_raw = argv[2];
scale_out_raw = argv[3];

	printf("Input temperature: '%s' degrees\n", temp_raw);
printf("Input temperature scale: '%s'\n", scale_in_raw);
printf("Output temperature scale: '%s'\n",
scale_out_raw);
} else {
printf("Please specify the temperature and scales.\n");
}

The program first checks to see if the value of argc is greater than or equal to 4 (argc >= 4) because, when at least three command line arguments are specified, the value of argc will be greater than or equal to 4 (since the first item in the argv array is the name of the program being executed, the value of argc is always one greater than the total number of arguments supplied on the command line). If the value of argc is greater than or equal to 4, the program can safely set the values of the input temperature, the input temperature’s scale, and the output temperature equal to the first three command line arguments:

temp_raw = argv[1];
scale_in_raw = argv[2];
scale_out_raw = argv[3];

Otherwise, the program prints out a helpful error message, instructing the user to supply the required values:

printf("Please specify the temperature and scales.\n");

With this if statement, the complete program will look like the following:

#include <stdio.h>

void main (int argc, string_t argv[]) {
string_t temp_raw, scale_in_raw, scale_out_raw;

	if (argc >= 4) {
temp_raw = argv[1];
scale_in_raw = argv[2];
scale_out_raw = argv[3];

		printf("Input temperature: '%s' degrees\n", temp_raw);
printf("Input temperature scale: '%s'\n", scale_in_raw);
printf("Output temperature scale: '%s'\n",
scale_out_raw);
} else {
printf("Please specify the temperature and scales.\n");
}
}

Now, if this program is executed without any command line arguments, it will display the following output:

/> ch ./temp_converter.ch
Please specify the temperature and scales.

But when this program is executed with the command line arguments 212, F, C, it will display the following output:

/> ch ./temp_converter.ch 212 F C
Input temperature: '212' degrees
Input temperature scale: 'F'
Output temperature scale: 'C'

The next form of input validation is to determine whether the value that was specified for the temperature was number (although the number can be negative or positive, with any number of decimal points, it has to be a number). This can be accomplished using a if statement and the sscanf C function. The sscanf function reads or scans a string to determine whether it is in a particular format.

To determine whether the value that was specified for the temperature was a number, the following if statement can be used:

if (sscanf(temp_raw, "%f", &temp_in) == 1) {
printf("Input temperature: '%f' degrees\n",
temp_in);
} else {
printf("Invalid input temperature: '%s' degrees,\n",
temp_raw);
}

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Tags programmingCprogramming language

More about Inc.Linux

Show Comments
[]