Thread: Temp conversions

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Temp conversions

    Hi Guys

    Im trying to wirte a program that reads in 10 tempruratures then sends them
    to a function which converts them to farhenheit. It compiles ok but its only converting and returning the first value as the output.

    I guess I am making a very obvious mistake but cannot see where.

    here is my code I appreciate any help on this:

    Code:
    #include <iostream>
    #include <iomanip>
    
    // function prototype
    double celsius ( double[], const int );
    
    // main function - begins program execution ////////////////////////////////////
    //
    int main ( void )
    {
       const int ARRAY_SIZE = 10;
       
       double cel[ ARRAY_SIZE ] = { 0 };
       
       std::cout << "Enter 10 temp in celsius: ";
       
       for ( int i = 0; i < ARRAY_SIZE; i++ )
       {
          std::cin >> cel[ i ];
       }
       
       std::cout << "\nCELSIUS" << std::setw( 17 ) << "FARHENHEIT\n\n";
       
       for ( int i = 0; i < ARRAY_SIZE; i++ )
       {
          std::cout << cel[ i ] <<std::setw( 17 ) 
                    << celsius ( cel, ARRAY_SIZE ) << std::endl;
       }
       
       std::cin.get(); // freeze console output window
       std::cin.ignore();
       
       return 0; // return value from int main
    }
    
    double celsius ( double x[], const int size )
    {
       double farhenheit;
       
       for ( int k = 0; k < size; k++ )
       {
          // calculate conversion from celsius
          // to farhenheit
          int factor = 212 - 32;
       
          // convert celsius to farhenheit
          farhenheit = factor * x[ k ] / 100 + 32;
       }
       
       return farhenheit;
    }
    Double Helix STL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The obvious problem is that celsius() operates on an array to return a single value. It probably should operate on a single value to convert it. In such a case you can then loop over the array and use celsius() on each element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Which is also the only way your formatting could possibly work.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Cheers guys
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  3. Help with my program
    By duty11694 in forum C Programming
    Replies: 2
    Last Post: 11-24-2002, 05:54 PM
  4. Need help
    By duty11694 in forum C Programming
    Replies: 6
    Last Post: 11-17-2002, 03:23 PM
  5. Help with my c-program
    By duty11694 in forum C Programming
    Replies: 1
    Last Post: 11-15-2002, 02:13 PM