Newbie function trouble

This is a discussion on Newbie function trouble within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to learn functions but the book I'm using places the arguments into the function 'manually', they ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Newbie function trouble

    Hi,

    I am trying to learn functions but the book I'm using places the arguments into the function 'manually', they are 'constants'.

    I'm trying to pass the arguments to the function by using variables.

    Here is my code:-

    Code:
    #include <iostream>
    
    using namespace std;
    
    void box(int length, int width, int height);
    
    int main()
    {
        int length;
        int width;
        int height;
    
    	cout << "Enter length of the box " << endl;
    	cin >> length;
    	cout << "Enter length of the box " << endl;
    	cin >> width;
    	cout << "Enter length of the box " << endl;
    	cin >> height;
    	box();
    	
    	system("pause");
    
    	return 0;
    }
    
    void box(length, width, height)
    {
    	cout << "The volume of the box is " << length * width * height << endl;
    }
    This isn't compiling, using Dev-C++ it is saying:-

    Line:5 "too few arguments to function `void box(int, int, int)'
    Line 5 is-
    void box(int length, int width, int height);
    I'm confused as this line is straight from the book.

    Could anyone be so kind as to offer me some pointers?

    Many thanks!

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    void box(length, width, height)
    length, width, and height are variables and you need to specify types for them. In the prototype these variables all have type "int":
    Code:
    void box(int length, int width, int height);
    They should be type "int" in the function itself too:
    Code:
    void box(int length, int width, int height)
    {
    	cout << "The volume of the box is " << length * width * height << endl;
    }
    Don't quote me on that... ...seriously

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And also, since you're telling the compiler that box needs three numbers, you need to bloody well give box three numbers:
    Code:
    	cout << "Enter length of the box " << endl;
    	cin >> length;
    	cout << "Enter length of the box " << endl;
    	cin >> width;
    	cout << "Enter length of the box " << endl;
    	cin >> height;
    	box(length, width, height);

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Don't make the common mistake of thinking that the length, width and height in main() have anything to do with the length, width and height in box(). They don't. They're completely independent variables.
    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

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    I somtimes wish books would stop having "using namespace std;" as a common thing to use. Problems can occur later on when using real namespaces and classes for example, and including the entire namespace on simple programs is not needed. All books should really use the std:: prefixes.
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by swgh View Post
    I somtimes wish books would stop having "using namespace std;" as a common thing to use. Problems can occur later on when using real namespaces and classes for example, and including the entire namespace on simple programs is not needed. All books should really use the std:: prefixes.
    It's probably because brevity is more important in a paper book than in a source file - even Stroustrup's book leaves out std:: in its code snippets. (BTW, it's spelled "propane", without an i, and "accessories".)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,462
    I somtimes wish books would stop having "using namespace std;" as a common thing to use. Problems can occur later on when using real namespaces and classes for example, and including the entire namespace on simple programs is not needed. All books should really use the std:: prefixes.
    If I remember correctly, Accelerated C++ starts with using fully qualified names, but eventually uses using-declarations and using-directives. It lightens the printing hassle, I suppose. Also, Stroustrup uses them for his own online examples, but that could be pure laziness with (no) respect to simple examples.

    Either way it does not seem to be the cause of Swerve's problem.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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