Temp conversions

This is a discussion on Temp conversions within the C++ Programming forums, part of the General Programming Boards category; Hi Guys Im trying to wirte a program that reads in 10 tempruratures then sends them to a function which ...

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

    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;
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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,475
    Cheers guys
    I'm just trying to be a better person - My Name Is Earl

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, 04:54 PM
  4. Need help
    By duty11694 in forum C Programming
    Replies: 6
    Last Post: 11-17-2002, 02:23 PM
  5. Help with my c-program
    By duty11694 in forum C Programming
    Replies: 1
    Last Post: 11-15-2002, 01:13 PM

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