$ a.out
Please choose what to do next:
f to convert a Fahrenheit temperature to Celsius
c to convert a Celsius temperature to Fahrenheit
q to quit (exit the program)
Your choice? f
Enter the temperature to convert: 212
That temperature in Celsius is 100
Please choose what to do next:
f to convert a Fahrenheit temperature to Celsius
c to convert a Celsius temperature to Fahrenheit
q to quit (exit the program)
Your choice? f
Enter the temperature to convert: 98.6
That temperature in Celsius is 37
Please choose what to do next:
f to convert a Fahrenheit temperature to Celsius
c to convert a Celsius temperature to Fahrenheit
q to quit (exit the program)
Your choice? c
Enter the temperature to convert: 37
That temperature in Fahrenheit is 98.6
Please choose what to do next:
f to convert a Fahrenheit temperature to Celsius
c to convert a Celsius temperature to Fahrenheit
q to quit (exit the program)
Your choice? x
Invalid response
Please choose what to do next:
f to convert a Fahrenheit temperature to Celsius
c to convert a Celsius temperature to Fahrenheit
q to quit (exit the program)
Your choice? c
Enter the temperature to convert: 100
That temperature in Fahrenheit is 212
Please choose what to do next:
f to convert a Fahrenheit temperature to Celsius
c to convert a Celsius temperature to Fahrenheit
q to quit (exit the program)
Your choice? q
It is up to you how many functions to use and how to structure your program, but you may find it helpful to use the english_to_metric, program presented in class as a guideline.
Recall that the formula for converting from Fahrenheit to Celsius is C = (5/9)(F - 32), while the formula for converting from Celsius to Fahrenheit is F = (9/5)C + 32.
$ a.out Enter one or more lines of text, control-D to end: abcd! 1234? x y z w user enters control-D 8 letters 4 digits 20 characters in allNote that the total number of characters includes the "newline" characters at the end of each line, but does not include the control-D typed to signal end of file.
Hints: You can easily check whether a character is a letter, or whether it is a digit, by using library functions described in Section 8.3. For an example of reading input a character at a time, see example program echo_input.cc (presented in class).
After you write your program and test it with some input from the keyboard, also try it out with input from a file, using Unix input redirection. "Input redirection" means that the program's standard input (normally entered from the keyboard) will instead be redirected to come from some other source, such as a file. To use this to run program a.out getting input from text file input.txt rather than from the keyboard, type the following:
a.out < input.txt
input.txt can be any file, for example one created
using a text editor. It could even be a file containing program
source. So you could count the characters in your program's
source code by typing something like a.out < mypgm.cc .
To solve this problem, it may be useful to know that each character is represented inside the computer as a small integer. For example, try the following lines of code:
int x = 'a';
cout << x << endl;
One should be very careful about treating characters as integers since
the same character may be represented using different numbers on
different computers. However, it is fairly safe to write
the following loop to
print all the lowercase characters a, b, ..., z.
char c = 'a';
while (c <= 'z')
{
cout << c << endl;
++c;
}
Note that this program should produce a lot of output, probably more than will fit into a terminal window or telnet window. You may be able to scroll the window up and down to see all of it; if not, you can use Unix output redirection to put the output in a text file, which you can then examine using your favorite text editor. (This approach also makes it very easy to determine how many strings the program printed, which could help you determine whether the program is working correctly.) "Output redirection" means that everything the program prints to standard output (normally the screen) will instead be redirected to somewhere else, such as a file. To use this to run program a.out and put its output in output.txt, type the following:
a.out > output.txt
Note that if output.txt already exists,
the above command probably will not work;
you will probably (depending on how your account is configured)
get the message "Cannot clobber existing file".
There are two ways to get around this.
One is to delete output.txt and try the
above command again.
The other is to tell Unix you want to overwrite the previous
contents of output.txt. Syntax for doing this depends
on how your account is set up. One of the following two commands
should work for you:
a.out >| output.txt
a.out >! output.txt
To find out which one to use, first type
echo $SHELL
If the response is /bin/bash, use the first form.
If the response is /bin/tcsh, use the second form.
If the response is anything else, check with your instructor
about what to do.