#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0

    cout not displaying output


    I am trying to learn C++ using "SAM's Teach Yourself C++ in 24 Hours." Needless to say, I haven't gotten very far and am already having a problem. The compiler I am using is Dev-C++ 4 (http://www.bloodshed.net/devcpp/) and I'm using WinNT 4. I am at the chapter where I am learning how to declare variables. The test code is supposed to multiple the variables Width and Length and store them in Area. The output of the script should be:

    Width: 5
    Length: 10
    Area: 50

    Here is the code:

    #include <iostream.h>

    int main()
    {
    int Width = 5, Length;
    Length = 10;

    int Area = Width * Length;

    cout << "Width: " << Width << "\n";
    cout << "Length: " << Length << endl;
    cout << "Area: " << Area << endl;
    return 0;
    }

    When I run the program via a command shell, I get no output whatsoever, just gives me a new prompt. Does anyone have any ideas what could be causing this so I can try to correct it and continue with my learning. I'd appreciate any help. Thanks.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Betcha the problem is because your DOS window is closing too fast for you to read the output. Try doing this (bold lines are my additions).
    Code:
    #include <iostream.h> 
    #include <stdlib.h>
    
    int main(void) 
    { 
    int Width = 5, Length; 
    Length = 10; 
    
    int Area = Width * Length; 
    
    cout << "Width: " << Width << "\n"; 
    cout << "Length: " << Length << endl; 
    cout << "Area: " << Area << endl; 
    system("pause");
    return 0; 
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    Hey James20
    your code is running without any errors
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0
    I'm actually running the executable from the command line, while in a DOS window, typing the program's name at the prompt, so there's no window closing. I did try your suggestion however, and it still didn't work. Thanks though.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0
    That's odd. What OS are you using? I've done a few other examples like using the sizeof() function to output the bytes each type of variable takes. I don't see anything different in this one, other than the multiplication operator. Anyone have any other suggestions? I'll try this on my XP machine after work also.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    UK
    Posts
    41
    Rep Power
    12
    I would expect your compiler to complain because you are not writing "using namespace std" or "std::cout".

    I originally learned C++ from Sam's TYSC++ in 21 days. I did the examples from the book using a variety of IDEs, including Dev-C++, MSVC++, and KDevelop for Linux as well as emacs. As far as I can remember, the code worked fine with all of the above.

    Try out Scorpion's code but run the program through the IDE's interface instead of from the command line.

    ** I just tried this with DevC++ 4 on Windows 2000 5.00.2195 SP4 and it works fine, prints out the variables as it should.
    Last edited by Cuku; July 24th, 2003 at 12:28 PM.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0
    Well, I just don't get it. I thank you guys for all of your help, but it's still not working. When I use the system("pause") all that is displayed is "Press any key to continue..." I'm going to assume that this is an NT4 bug, and hopefully I'll prove that when I attempt the same code on my XP laptop.

IMN logo majestic logo threadwatch logo seochat tools logo