Wrong declaration, Right result ?

This is a discussion on Wrong declaration, Right result ? within the C++ Programming forums, part of the General Programming Boards category; I'm reading a beginners guide to C++, and it say's, that this code should give the result: -5536 But when ...

  1. #1
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62

    Question Wrong declaration, Right result ?

    I'm reading a beginners guide to C++, and it say's,
    that this code should give the result: -5536

    But when I compile it using Visual C++ 6.0,
    the result is: 60000
    which sure is correct, but when "result" is
    declared as an "int", the result should be an
    overflow right ?


    Code:
    #include <iostream.h>
    
    void main(void)
    {
    	int result;
    
    	result = 200 * 300;
    
    	cout << "200 * 300 = " << result << endl;
    }
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    ints are usually 32 bits, which means they usually have a range from -(2^31)+1 to 2^31-1

    Check to make sure you got the type from your book right. Also, it looks like that book might be outdated since it uses <iostream.h>, which is deprecated. You should use <iostream> and put the following line after your includes:
    Code:
    using namespace std;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the maximum value that can be stored in an int can be found in the file limits.h. If you attempt to store a larger value than can be stored, the result is undefined behavior, which means you can not count on any value. Most compilers I have worked with just wrap the number, but there is no guarentee that all compilers will do that.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    On your platform a short is probably only 16 bits, so try it with a short instead to see what the book was trying to show you. Or get a newer, more standards compliant book.

  5. #5
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    If I write:

    Code:
    #include <iostream>
    instead of <iostream.h>
    I'll get this:
    Code:
    Compiling...
    regn_overflow.cpp
    c:\temporary files\regn_overflow\regn_overflow.cpp(9) : error C2065: 'cout' : undeclared identifier
    c:\temporary files\regn_overflow\regn_overflow.cpp(9) : error C2297: '<<' : illegal, right operand has type 'char [13]'
    c:\temporary files\regn_overflow\regn_overflow.cpp(9) : error C2065: 'endl' : undeclared identifier
    Error executing cl.exe.
    
    regn_overflow.exe - 3 error(s), 0 warning(s)
    I have iostream in the
    C:\Program Files\Microsoft Visual Studio\VC98\Include directory (837 bytes).

    iostream.h is 2.397 bytes

    But iostream hasn't got any extention...


    In my book it says that an int has a range from:
    -32.768 to 32.767
    so when I get 60.000 it should get an overflow accordingly.
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Your book is outdated or for a different operating system.

    Since Win95 and 32Bit, and int is 32 bits long. The limits your book quotes are from 16 bit times. If you want to know the size of variable types in bits, use

    Code:
    cout << ( sizeof( type ) * 8 ) << endl;
    To bring your book up to date and let this example work out the way the author intended, use short instead of int as type.

    Read Jawibs whole text:

    You should use <iostream> and put the following line after your includes:

    Code:
    using namespace std;
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    I still wonder why I get this "undeclared identifier"
    when I write:
    #include <iostream>
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    because you also must identify the namespace in which the cout function resides -- std. you can do that a couple ways
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World" << std::endl;
    
       return 0;
    }
    or
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       cout << "Hello World" << endl;
    
       return 0;
    }
    or
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
       cout << "Hello World" << endl;
    
       return 0;
    }

  9. #9
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62

    Thumbs up

    Great, thanks !

    I'll use:
    Code:
    #include <iostream>
    
    using namespace std;
    
    and then:
    
    cout << "Hello World" << endl;

    So now I guess that iostream without
    the ".h" should work in vc6


    I'll try this when I get home from work
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  10. #10
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Code:
    #include <iostream>
    
    using std::cout;
    
    void main(void)
    {
    	int result;
    
    	result = 200 * 300;
    
    	cout << "200 * 300 = " << result << endl;
    }
    If I use iostream.h it works, but when I use this code
    with iostream, I'll get the following error message:

    [QUOTE]Compiling...
    regn_overflow.cpp
    c:\temporary files\regn_overflow\regn_overflow.cpp(11) : error C2065: 'endl' : undeclared identifier
    Error executing cl.exe.

    Of course I can remove 'endl', then it works...
    hmm...

    regn_overflow.exe - 1 error(s), 0 warning(s)
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  11. #11
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Code:
    #include <iostream>
    
    using std::cout;
    
    void main(void)
    {
    	int result;
    
    	result = 200 * 300;
    
    	cout << "200 * 300 = " << result << endl;
    }
    If I use iostream.h it works, but when I use this code
    with iostream, I'll get the following error message:

    Compiling...
    regn_overflow.cpp
    c:\temporary files\regn_overflow\regn_overflow.cpp(11) : error C2065: 'endl' : undeclared identifier
    Error executing cl.exe.
    Of course I can remove 'endl', then it works...
    hmm...

    regn_overflow.exe - 1 error(s), 0 warning(s)
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  12. #12
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Code:
    #include <iostream>
    
    using std::cout;
    
    void main(void)
    {
    	int result;
    
    	result = 200 * 300;
    
    	cout << "200 * 300 = " << result << endl;
    }
    If I use iostream.h it works, but when I use this code
    with iostream, I'll get the following error message:

    Compiling...
    regn_overflow.cpp
    c:\temporary files\regn_overflow\regn_overflow.cpp(11) : error C2065: 'endl' : undeclared identifier
    Error executing cl.exe.

    regn_overflow.exe - 1 error(s), 0 warning(s)
    Of course I can remove 'endl', then it works...
    hmm...
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  13. #13
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    When someone replies to your question, it is in your best interest to read the entire post.

    First: don't include iostream.h, it's not standard. Include iostream. Forget iostream.h exists.

    Second: As a result of the first item, you have to define what namespace you'll be using for what statements, and where you'll be using it. There are a number of ways to do this. Ancient Dragon laid it out all very nicely for you.

    Look at the options. Look at your code. How do the they differ? For the option you seem to have chosen:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl; <<-- You need to include as well to do it this way.
    
    void main(void)
    {
    	int result;
    
    	result = 200 * 300;
    
    	cout << "200 * 300 = " << result << endl;
    }
    Speaking of standard, I'd stop using void main(void) if you want to get out of this thread alive...that really ticks people off around here. Use int main() instead.
    Last edited by Decrypt; 10-24-2005 at 12:47 PM.
    There is a difference between tedious and difficult.

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    The best way would just be to put:

    Code:
    using namespace std;
    Then you havent got to put using std::cout and using std::endl

  15. #15
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Hi Decrypt !

    When someone replies to your question, it is in your best interest to read the entire post.
    You're quite right.

    regards,

    The SharK
    Studying programming languages,
    you'll ALWAYS be a student ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Program exiting
    By sql.scripter in forum C Programming
    Replies: 9
    Last Post: 01-28-2006, 07:51 PM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. What is wrong with this program(data structures)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-06-2002, 12:55 PM

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