Please help me debug a program

This is a discussion on Please help me debug a program within the C++ Programming forums, part of the General Programming Boards category; Hi all, Could you guys help me debug this program? It is supposed to take a temperature scale and temperature ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Please help me debug a program

    Hi all,
    Could you guys help me debug this program? It is supposed to take a temperature scale and temperature value given by the user and convert it into the other two temperature scales. The mechanics of this program seem ok but its just the math part. Please take a look at it and tell me what you guys think:
    Code:
    #include <iostream.h>
    /* 
       Programmer: Bensan George Benny
       Date Due: 05/29/2003
       Project 1 - A program that formats an initial temperature value to three temperature standards
    */
    
    // Function Prototypes
    double get_far(char user_input, double temp_value);
    double get_celsius(char user_input, double temp_value);
    double get_kelvin(char user_input, double temp_value);
    
    double main(){
    
    char sentinel = 'y';	// set variable initially to yes to pass doubleo while loop
    char user_input;   // variable to decide what initial temperature format is
    double temp_value;	// variable to decide what value it is.
    double celsius, fahrenheit, kelvin;
    
    while ((sentinel != 'n')){  
    	cout << "Welcome to Temperature Converter!\n";
    	cout << "Is starting temperature Celsius, Fahrenheit, or Kelvin?\n";
    	cout << "Please enter c, f, or k: ";
    	cin >> user_input;
    	cout << "Please enter starting temperature: ";
    	cin >> temp_value;
      
    	switch(user_input){ 
    	  case 'K':
    	  case 'k':
    		// perform functions
    		fahrenheit = get_far(user_input, temp_value);
    		celsius = get_celsius(user_input, temp_value);
    		kelvin = get_kelvin(user_input, temp_value);
    		cout << "This temperature is equivalent to the following:\n\n";
    		cout << fahrenheit << " degrees Fahrenheit\n";
    		cout << celsius << " degrees Celsius\n";
    		cout << kelvin << " degrees Kelvin\n\n";
    		break;
       
    	  case 'C':
    	  case 'c':
    		// perform functions
    		fahrenheit = get_far(user_input, temp_value);
    		celsius = get_celsius(user_input, temp_value);
    		kelvin = get_kelvin(user_input, temp_value);
    		cout << "This temperature is equivalent to the following:\n\n";
    		cout << fahrenheit << " degrees Fahrenheit\n";
    		cout << celsius << " degrees Celsius\n";
    		cout << kelvin << " degrees Kelvin\n\n";
    		break;
       
    	  case 'F':
    	  case 'f':
    		// perform functions
    		fahrenheit = get_far(user_input, temp_value);
    		celsius = get_celsius(user_input, temp_value);
    		kelvin = get_kelvin(user_input, temp_value);	
    		cout <<  "This temperature is equivalent to the following:\n\n";
    		cout << fahrenheit << " degrees Fahrenheit\n";
    		cout << celsius << " degrees Celsius\n";
    		cout << kelvin << " degrees Kelvin\n\n";
    		break;
    		
    	  default:
    		cout << "ERROR: Unknown temperature scale.\n";
    		break;
    	}
    
    
    
    		cout << "Would you like to run it again?(y/n): ";
    		cin >> sentinel;
    	}
      return 0;
    }
    
    
    /*
    Conversion between Celsius and Fahrenheight: 
    
      C = 5/9 * (F - 32)
           and
      F = 9/5 * C + 32
    
    Conversion between Celsius and Kelvin: 
    
      C = K - 273.15
    
    */
    
    // Function Declarations
    double get_far(char user_input, double temp_value){
    double c, f, k;
    switch(user_input){
    	case 'F':
    	case 'f':
    	// find f, given f
    	f = temp_value;
    	break;
    	
    	case 'C':
    	case 'c':
    	// find f, given c
    	c = temp_value;
    	f = (c + 32)*(9/5);
    	break;
    
    	case 'K':
    	case 'k':
    		// find f, given k
    		k = temp_value;
    		c = k - 273.15;
    		f = (c+32)*(9/5);
    	break;
    }
    	return f;
    }
    
    double get_celsius(char user_input, double temp_value){
    double c, f, k;
    switch(user_input){
    	case 'C':
    	case 'c':
    		c = temp_value;
    	break;
    	
    	case 'F':
    	case 'f':
    		f = temp_value;
    		c = (f - 32)*(5/9);
    	break;
    
    	case 'K':
    	case 'k':
    		k = temp_value;
    		c = k - 273.15;
    	break;
    	}
    	return c;
    }
    
    double get_kelvin(char user_input, double temp_value){
    double c, f, k;	
    switch(user_input){
    	case 'K':
    	case 'k':	
    	// find k, given k
    	k = temp_value;
    	break;
    
    	case 'C':
    	case 'c':	
    	// find k, given c
    		c = temp_value;
    		k = c + 273.15;
    	break;
    
    	case 'F':
    	case 'f':
    		// find k, given f
    		f = temp_value;
    		c = (f - 32)*(5/9);
    		k = c + 273.15;
    	break;
    }
    	return k;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    double main(){
    Wheee-hoooo, that's a new one as opposed to all the void main's you encounter. Anyway, main returns an int, not double.

    As for your question can you be more specific on what is happening (or what does not happen)??? Doesn't it compile? Does it give the wrong output? Does it crash?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A couple problems I noticed:
    1) The conversion from C to F is: F = (9.0 / 5.0) * C + 32
    2) It thinks that the 9 and 5 are integers, and so 5/9=0. To avoid this, put the decimals in, so 5.0/9.0 and 9.0/5.0 will give you doubles.

    Hope this helps.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Smile

    Thanks a lot guys... I don't know why I put double main()... everyone is allowed atleast one brain fart right :-) Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 12:54 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21