Converter Debug


Program Listing

ConverterDebug.cpp

/************************************************************************
*                                                                       *
*   Program:    ConverterLoop.cpp                                       *
*                                                                       *
************************************************************************/

#include <iostream>
#include <string>

//  Define or undefine the symbol DEBUG

#define DEBUG

using namespace std;

int main() {

//  Prompt the user for a temperature

    cout << "Enter a temperature in Fahrenheit, Celcius, or Kelvin,";
    cout << "followed by F, C, K to indicate the scale (e.g., 39 F). ";
    cout << "(Type 9999 Z to quit):  " << endl << endl;
    
    int temp;
    char scale;
    cin >> temp >> scale;
    
    int tempF;
    int tempC;
    int tempK;

//  Read and convert temperatures until user quits
    
    while (temp != 9999 || scale != 'Z') {

    //  Select the case based on the scale used

        switch (scale) {
        
        //  Convert Fahrenheit to Celsius and Kelvin
        
            case 'F': case 'f':
#ifdef DEBUG
                cout << "DEBUG:  Entering case 'F'" << endl;
                cout << "DEBUG:  temp = " << temp << endl;
#endif
                tempF = temp;
                tempC = round(5.0/9.0*(tempF - 32));
                tempK = tempC + 273;
                break;
        
        //  Convert Celsius to Fahrenheit and Kelvin
        
            case 'C': case 'c':
#ifdef DEBUG
                cout << "DEBUG:  Entering case 'C'" << endl;
                cout << "DEBUG:  temp = " << temp << endl;
#endif
                tempC = temp;
                tempF = round(9.0/5.0*tempC + 32);
                tempK = tempC + 273;
                break;
        
        //  Convert Kelvin to Fahrenheit and Celsius
        
            case 'K': case 'k':
#ifdef DEBUG
                cout << "DEBUG:  Entering case 'K'" << endl;
                cout << "DEBUG:  temp = " << temp << endl;
#endif
                tempK = temp;
                tempC = tempK - 273;
                tempF = round(9.0/5.0*tempC + 32);
                break;
        
        //  Invalid input
        
            default:
#ifdef DEBUG
                cout << "DEBUG:  Entering default case" << endl;
                cout << "DEBUG:  scale = " << scale << endl;
#endif
                cout << "Invalid temperature scale.  Use F, C, or K" << endl;
                cout << "Aborting..." << endl;
                exit(1);
                break;
        }

    //  Print the results

        cout << endl << "The temperatures are " << tempF << " F = " << tempC << " C = ";
        cout << tempK << " K" << endl;

    //  Prompt the user for another temperature

        cout << "Enter another temperature (or 9999 Z to quit):  " << endl << endl;
        cin >> temp >> scale;
    }
    
    cout << "Good-bye" << endl;
    
    return 0;
}

Sample Run #1

Enter a temperature in Fahrenheit, Celcius, or Kelvin,followed by F, C, K to indicate the scale (e.g., 39 F). (Type 9999 Z to quit):  

32 F

The temperatures are 32 F = 0 C = 273 K
Enter another temperature (or 9999 Z to quit):  

100 C

The temperatures are 212 F = 100 C = 373 K
Enter another temperature (or 9999 Z to quit):  

0 K

The temperatures are -459 F = -273 C = 0 K
Enter another temperature (or 9999 Z to quit):  

9999 Z
Good-bye

Sample Run #2

Enter a temperature in Fahrenheit, Celcius, or Kelvin,followed by F, C, K to indicate the scale (e.g., 39 F). (Type 9999 Z to quit):  

32 F
DEBUG:  Entering case 'F'
DEBUG:  temp = 32

The temperatures are 32 F = 0 C = 273 K
Enter another temperature (or 9999 Z to quit):  

100 C
DEBUG:  Entering case 'C'
DEBUG:  temp = 100

The temperatures are 212 F = 100 C = 373 K
Enter another temperature (or 9999 Z to quit):  

0 K
DEBUG:  Entering case 'K'
DEBUG:  temp = 0

The temperatures are -459 F = -273 C = 0 K
Enter another temperature (or 9999 Z to quit):  

9999 Z
Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:35 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/ConverterDebug.html

Return to Coms 261 home page