Thread: C++ functions

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    C++ functions

    I'm trying to calculate the volume, cost, charge, and profit by defining a function and having a function call in the main. I have defined my functions but I guess I don't know how to put the function call in the main, my answers are coming out weird. Can anyone help my with this?

    Code:
    #include <iostream>using namespace std;
    
    
    double cal_vol (double l, double w, double h) //volume function
    {
           double V;
           V = l * w * h;
           return V;
    }
    
    
    double cal_cost (double V)//cost function
    {
           double C;
           C = V * 0.23;
           return C;
    }
    
    
    double cal_charge (double V)// charge function
    {
           double D;
           D = V * 0.5;
           return D;
    }
    
    
    double cal_profit (double D, double C)//profit function
    {
           double P;
           P = D - C;
           return P;
    }
    main()
    {
          int l, w, h;
          double cal_vol, cal_cost, cal_charge, cal_profit;
          
          cout << "Enter crate length : ";
          cin >> l;
          cout << "Enter crate width : ";
          cin >> w;
          cout << "Enter crate height : ";
          cin >> h;
          
          cout << "The crate's volume is " << cal_vol << endl;
          cout << "The crate's cost is " << cal_cost << endl;
          cout << "The customer's charge is " << cal_charge << endl;
          cout << "The profit is " << cal_profit << endl;
          
          system("pause");
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Well I can see a few things that I can see you probably need to add. You never actually call any of the functions you declared you just created variables with the same names of the functions. If you just put something like "volume = cal_vol();" with the needed arguments in the parenthesis and do that for all the other functions I believe it should work as you wanted. I am fairly new to C++ myself so I do not know if this is all you need to do or if there is something more you need to do but hopefully I was able to help you some.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    thadis_4 has the right idea. The compiler cannot read your mind. As far as it's concerned you've told it about some functions, and some variables (which happen to share the same name, by your choice). Giving a function the same identifier as a variable will not just give these variables the correct values.

    For this reason, it's not wise to name your variables the exact same things as your function, even if they are related conceptually. Like thadis_4 suggested, switch the variable names to describe the quantities. cal_vol should become "volume" or "vol", calc_cost should become "cost", etc.

    You need to actually fill these variables with the appropriate values by calling the functions in the right-hand side of an assignment.
    Last edited by Ocifer; 02-09-2012 at 08:40 PM. Reason: correctness
    W7, Ubuntu -- mingw, gcc, g++, code::blocks, emacs, notepad++

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A function call looks like:
    Code:
    functionName(parameter1, parameter2);
    Variable declarations look like:
    Code:
    double variable1, variable2;
    You can't call a variable, nor can you set the value of a function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    thanks guys this really helped, i'm new to c++ and programming actually so this place is really helping me out

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    I am just learning too and these forums have helped me a lot too not only by answering questions about programming but just by reading threads that others have posted and looking at their code to try to notice were they might of made a mistake or if I do not know what parts of the program does yet, like classes, I try to guess what the part does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  2. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread