An introduction to Ch: Implementing a temperature converter in Ch

Ch is a combined C shell and IDE

Here, the sscanf function is used to determine whether temp_raw contains a valid number— sscanf(temp_raw, "%f", &temp_in). The string "%f", following temp_raw, tells sscanf that temp_raw is expected to contain a positive or negative number with an optional decimal point (such numbers are referred to as floating point numbers and therefore use the format specifier %f). The value, &temp_in, tells sscanf to store the numeric value of temp_raw in the variable temp_in if temp_raw contained a valid number (the temp_in variable, like the other variables in this program, will be created and initialized at the beginning of the program).

If temp_raw contains a valid number, sscanf will return a value of 1, in which case a printf statement prints out the value specified for the temperature. Otherwise, an error message is printed out. Notice that this if statement uses the equals operator (==) to check whether sscanf returned a value of 1. The equals operator (==) is need to compare whether two values are equal because, as you may recall, C and Ch use a single equal sign (=) to assign a value to a variable (for example, temp_raw = argv[1]).

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;
float temp_in = 0.0;

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

		/* check if the specified input temperature contained a
valid number */

		if (sscanf(temp_raw, "%f", &temp_in) == 1) {
printf("Input temperature is '%f' degrees\n",
temp_in);
} else {
printf("Invalid 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");
}
}

As mentioned above, the second line of the main function creates the variable temp_in as a floating point variable using the float keyword and initializes it to the value of 0.0 (float temp_in = 0.0). Also added to the program are the lines:

/* check if the specified input temperature contained a
valid number */

These lines are known as comments. Comments consist of text that is added to a program by the programmer to describe what the program is doing in a particular part. Although there is no requirement to add comments to a program (and both C and Ch ignore comments), including comments in a program is a good idea since comments can those who have to later read or edit the program understand what the program is doing (or trying to do). There are no set rules on what to include in comments, but the examples below should help illustrate the types of information that may be useful to include in comments (for the sake of readability, the comments are included only in the full program listings below)

Now, if this program is executed with an invalid value for the temperature, such as hello, it will display the following output:

/> ch ./temp_converter.ch hello F C
Invalid input temperature: 'hello' degrees
Input temperature scale: 'F'
Output temperature scale: 'C'

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.000000' degrees
Input temperature scale: 'F'
Output temperature scale: 'C'

The next validation step is to make sure that valid temperature scales were specified. To simplify the program, the valid temperature scales can be limited to either the Fahrenheit (F) or Celsius (C) temperature scales. To check whether the specified temperature scale was either Fahrenheit or Celsius, the strcmp C function, which compares two strings and returns a value of 0 if the strings match, in combination with an if statement can be used:

if (strcmp(scale_in_raw, "Fahrenheit") == 0 ||
strcmp(scale_in_raw, "F") == 0 ||
strcmp(scale_in_raw, "Celsius") == 0 ||
strcmp(scale_in_raw, "C") == 0) {
printf("Input temperature scale: '%s'\n",
scale_in_raw);
} else {
printf("Invalid input temperature scale: '%s'\n",
scale_in_raw);
}

Here, the strcmp function is first used to see whether the input temperature scale, scale_in, equals the string "Fahrenheit" (strcmp(scale_in, "Fahrenheit") == 0). If it does not, the or operator, ||, is used to check whether scale_in equals the string "F" (strcmp(scale_in, "F") == 0). If scale_in does not equal "Fahrenheit" or "F", the or operator, ||, is then used to check whether scale_in equals the strings “Celsius” or “C”:

strcmp(scale_in_raw, "Celsius") == 0 ||
strcmp(scale_in_raw, "C") == 0)

If scale_in equals at least one of these values, then its value is printed out. If not, an error message is printed. With this if statement, the complete program will look like the following:

#include <string.h>
#include <stdio.h>

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

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

		/* check if the specified input temperature contained a
valid number */

		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);
}

		/* check if the input temperature scale is either
Fahrenheit or Celsius */

		if (strcmp(scale_in_raw, "Fahrenheit") == 0 ||
strcmp(scale_in_raw, "F") == 0 ||
strcmp(scale_in_raw, "Celsius") == 0 ||
strcmp(scale_in_raw, "C") == 0) {
printf("Input temperature scale: '%s'\n",
scale_in_raw);
} else {
printf("Invalid 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");
}
}

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
[]