Thread: Bit of help required still - using a function with array

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Bit of help required still - using a function with array

    Sorry I pasted an earlier code before fixing it, this one compiles but I can't figure out how to make the values go through my function, or if I made it correctly to change input from user into celsius.


    Code:
    // Computer Lab test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    double c;
    double celsius;
    double CheckCent(double c)
    {
        if ( c>30 )
        {
            return 1.8 * celsius + 32;
        }
    	return c;
    }
    
    
    // call this function to convert celsius to fahrenheit (function name is convert)
    int _tmain(int argc, _TCHAR* argv[])
    {
    double myarray[51];
    //double i;
    int flag;
    flag=1;
    int i;
    for(i = 0; i < 51 && flag == 1; i++)
    {   // Loop to enter temperatures - pressing 1 continues, pressing 0 stops //
    	cout << "Enter Temperature Reading:";
    	cin >>myarray[i];
    	cout << "Enter 1 to input more values, 0 to stop";
    	cin >>flag;
    }// End for
    cout << "Number of elements in the array is: " << i;
    cout <<"\n";
    
    		return 0;
    }
    Last edited by Planetx33; 04-05-2007 at 04:57 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So apart from not compiling, what do you want us to do with than unindented mess?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    6
    edited post

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So, call the function?

    And don't use globals. The first one is not even what you think. The second one seems uninitialized.
    Code:
    //double c;
    //double celsius;
    double CheckCent(double c)
    {
        double celsius = ?;
        if ( c>30 )
        {
            return 1.8 * celsius + 32; //1.8 * c + 32 ?
        }
    	return c;
    }
    _tmain() and "stdafx.h" are MSVC specific stuff, I think, so your code doesn't compile for many.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if ( c>30 )
    And what do you return if it's <= 30?
    The maths works on all values, so why bother with this condition.

    You didn't fix the lack of indentation with your edit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Basically the same question here as well
    http://cboard.cprogramming.com/showthread.php?t=88246
    Closed (& read the rules)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM