Thread: noob error returning a function evaluation

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    noob error returning a function evaluation

    I can't seem to figure out why this isn't returning the quotient of inches*2.54, even though I think to convert from inches to centimeters it should be inches/2.54. Anyways I can't get that value, I either get a warning saying "the address of 'float inches_to_centimeters(float)', will always be 'true' " or else I get an error saying something about the stuff under the { after if. I only get the error when I change the return in the function to "return (inches*2.54);
    Here's the code, if anyone can help...
    Code:
    /*
     * Name : xxxxxx xxxxxxx
     * Course : cps
     * Assignment : Homework03
     * Description : Convert from english units to metric and vice versa. 
     */
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    float inches_to_centimeters(float inches);
    int main()
    {
      string conversion;
        cout << "Please enter the name of the units you would like to convert to" << endl;
        cout << "   -Ex. to convert from inches to centimeters, enter \"inches\"" << endl;
        cout << "The following are the available conversions: " << endl;
        cout << "    inches, centimeters, feet, meters, miles, kilometers" << endl;
        cin >> conversion;
        if (conversion == "inches")
          {
        float inches_to_centimeters(float inches);
           cout << inches_to_centimeters << endl;
          }
        else
          {
        cout << "no" << endl;
          }
    }
    /*
     * Name: inches_to_centimeters
     * Purpose: Convert inches to centimeters
     * Receive: A number in inches
     * Return: Equivalent number in centimeters
     */
    
        float inches_to_centimeters(float inches)
          {
        cout << "The number of inches is: ";
        cin >> inches;
        (inches*2.54);
        return 0;
          }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
        if (conversion == "inches")
          {
        float inches_to_centimeters(float inches);
           cout << inches_to_centimeters << endl;
          }
    When you make a call to a function you do not specify the return type or the type of the parameter.

    You need to review how to call functions, how to get a return value, and how to pass parameter.

    (inches*2.54);
    This has has no affect as the result is not stored.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    I originally had it as this
    return (inches*2.54);
    instead of:
    (inches*2.54);
    return 0;

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Wow, this just makes my head hurt:
    Code:
    if (conversion == "inches")
          {
        float inches_to_centimeters(float inches);
           cout << inches_to_centimeters << endl;
          }
    Seriously, you need to review functions in general. If you have specific questions post them.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    Yea, well I figured out that for this program I only needed to make it a test driver, so I didn't need the if statement or anything. I'm just beginning to learn C++ that's why i'm so bad at it. I thank you for your input though.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here is how a simple function works:
    Code:
    #include<iostream>
    using namespace std;
    
    double simple(int a)
    {
        double temp = a * 1.5;
        return temp;
    }
    
    int main()
    {
        int some_int = 9;
        double result = simple(some_int);
        cout<<result<<endl;
    
        return 0;
    }
    Last edited by 7stud; 02-17-2005 at 08:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:28 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM