dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Having trouble with functions, syntax problem I believe

  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Having trouble with functions, syntax problem I believe

    Hey everyone. My assignment is to convert height in ft and inches to meters. I have to use separate functions for input, calculation, and output. I also need to use global variables. I have the code written in the three separate functions, but when I run the program it executes, but nothing's written on the screen. I'm pretty sure the problem is in the syntax...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    //Functions Used:
    void input();
    void calculation();
    void output();
    
    //Global Variables:
    int feet;
    float inches;
    float meters;
    
    
    int main()
    {
        //Gets values from user
        void input();
    
        //Calculates new value
        void calculation();
    
        //Outputs result
        void output();
    
        return 0;
    }
    
    
    void input()
    {
        cout << "This program will change a height in feet to meters." << endl << endl;
        cout << "To start, enter the height in ft(space)in: ";
        cin  >> feet >> inches;
    }
    
    void calculation()
    {
        meters = ((feet*12)+inches)*0.0254;
    }
    
    void output()
    {
        cout << meters;
    }

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Having trouble with functions, syntax problem I believe

    Remove the 'void' words from inside your 'main' function. That declares a function, not a function call.

    Viggy

  3. #3
    Join Date
    Nov 2011
    Posts
    2

    Re: Having trouble with functions, syntax problem I believe

    That was it, thank you very much

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with functions, syntax problem I believe

    While that will work, using global variables somewhat misses the point of functions. I recommend passing parameters rather than using globals. For instance:

    Code:
    double feetInchesToMeters(int feet, int inches)
    {
        return ((feet*12)+inches)*0.0254;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)