box function problem

This is a discussion on box function problem within the C++ Programming forums, part of the General Programming Boards category; Firstly, sorry i posted this twice but i had connection problems and thought it hadnt posted the first time. Here ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Bangkok, Thailand
    Posts
    6

    box function problem

    Firstly, sorry i posted this twice but i had connection problems and thought it hadnt posted the first time.
    Here is my code for a box function.

    Code:
    #include <iostream>
    using namespace std;
    
    void box(int length, int width, int height);
    
    int main()
    {
    	box(7, 20, 4);
    	box(50, 3, 2);
    	box(8, 6, 9);
    
    	return 0;
    }
    
    void box(int length, int width, int height)
    {
    	cout << "The volume of the box is " << length * width * height << "\n";
    }
    and here is the output:

    The volume of the box is 560

    In the book im using the output is this:

    volume of box is 560
    volume of box is 300
    volume of box is 432


    why is my code only outputting the first line?

    ive checked the code and i can't spot any mistakes. Im ahaving the same problem with other function codes. Please help. Thanks
    Last edited by ryan_28; 09-22-2009 at 09:23 PM. Reason: posted twice

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The output of this program, as you posted is
    Code:
    The volume of the box is 560
    The volume of the box is 300
    The volume of the box is 432
    If you are not seeing the output, perhaps you need to scroll down.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    Bangkok, Thailand
    Posts
    6
    If you are not seeing the output, perhaps you need to scroll down
    no, scrolled down all the way and no output, but thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, that's all I can do. There's nothing wrong with your program; perhaps there's a compiler issue if you're using some bizarre/antiquated compiler, but g++ and Microsoft's Visual Studio handle it just fine.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Location
    Bangkok, Thailand
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Well, that's all I can do. There's nothing wrong with your program; perhaps there's a compiler issue if you're using some bizarre/antiquated compiler, but g++ and Microsoft's Visual Studio handle it just fine.
    i am using microsoft visual so it cant be that. just one of those annoing set backs newbies seem to encounter, never mind.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Because you are using "\n", which you shouldn't be using. Use cout << ... << endl; instead. It acts a newline, and it also flushes the output buffer.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  7. #7
    Registered User
    Join Date
    Sep 2009
    Location
    Bangkok, Thailand
    Posts
    6
    problem solved! it was the way i was debugging the programming. i was using 'right click > run to cursor' but when i realised you just press ctrl and F5 it worked fine. thanks

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by IceDane View Post
    Because you are using "\n", which you shouldn't be using. Use cout << ... << endl; instead. It acts a newline, and it also flushes the output buffer.
    Wrong. Using "\n" is fine, and overusing endl is an extremely common newbie mistake. The only time you want to explicitly flush cout is when you are writing progress messages during a lengthy operation. It is automatically flushed at program exit and when you request input from cin.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  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