Like Tree3Likes

Output error

This is a discussion on Output error within the C++ Programming forums, part of the General Programming Boards category; Why does the following code output 5 and 10, when it is supposed to output 5 and 10.2... Code: #include<iostream> ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    Output error

    Why does the following code output 5 and 10, when it is supposed to output 5 and 10.2...

    Code:
    #include<iostream>
     
    class ex
    {
    	int i,float j;
    
    public:
    	ex(int ii,float jj)
    	{
    		i=ii; j=jj;
    	}
    	void display()
    	{
    		std::cout<<i<<std::endl<<j<<std::endl;
    	}
    };
    void main()
    {
    	ex e(5,10.2);
    	e.display();	
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Quote Originally Posted by juice
    Why does the following code output 5 and 10, when it is supposed to output 5 and 10.2...
    Why does it even compile for you? This line:
    Code:
    int i,float j;
    should be:
    Code:
    int i;
    float j;
    Furthermore, although your compiler might accept it, this line:
    Code:
    void main()
    should be:
    Code:
    int main()
    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

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by laserlight View Post
    Furthermore, although your compiler might accept it, this line:
    Code:
    void main()
    should be:
    Code:
    int main()
    Been told multiple times, I assume he/she is just too stubborn to learn.
    ( I also assume that a certain (Turbo C++) dinosaur compiler is partially responsible for this )
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Damn!!

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Hey, void main() and <iostream.h> cause no errors in my compiler. Plus they relieve me of mentioning the namespace std:: and the return 0.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Relieve you, maybe. Driving insane your future coworkers, that's for sure!
    Salem likes this.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    86
    First off, congratulations. I don't know how you got that to compile, so you must have some serious compiling skills.

    In the more likely even that you're using a compiler that lets you get away with weird stuff, it's probably because you should declare your variables like this:

    Code:
    //No!!
    int i, float j;
    
    //Good
    int i;
    float j;
    The top one, for whatever reason, is allowing you to declare improperly, and it thinks you are declaring two ints (my guess). Because you can actually declare two ints like this

    Code:
    int i, j;
    and I guess some compilers will let you stretch that to a weird extreme?

    Anyhoo, this will work

    Code:
    #include<iostream>
      
    class ex
    {
        int i;
        float j;
    
    public:
        ex(int ii,float jj)
        {
            i=ii; j=jj;
        }
        void display() 
        {
            std::cout<<i<<std::endl<<j<<std::endl;
        }
    };
        
    int main()
    {   
        ex e(5,10.2); 
        e.display();
        
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    The book that I am referring "Let us C++" by Yashwant Kanetkar has never said int main() or <iostream.h>, and I don't find any compelling reasons to do that either.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Quote Originally Posted by juice
    Hey, void main() and <iostream.h> cause no errors in my compiler. Plus they relieve me of mentioning the namespace std:: and the return 0.
    I also note that this:
    Code:
    int i,float j;
    apparently did not cause your compiler to report an error. Plus it relieved you of getting the correct output.

    void main() is non-standard. The return type of main has nothing to do with the need to qualify names. Rather, using the correct version makes it more likely that your code will compile on another compiler, which in turn means that people are a little more likely to attempt to compile your code to help you along.

    <iostream.h> is pre-standard. Using the standard version makes it more likely that your code will compile on another compiler, which in turn means that people are a little more likely to attempt to compile your code to help you along. Furthermore, the use of namespaces helps to avoid name collisions when used appropriately, which is a Good Thing for you.
    Last edited by laserlight; 10-01-2011 at 04:16 AM.
    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

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by wildcard_seven View Post
    First off, congratulations. I don't know how you got that to compile
    I have absolutely no idea how it compiled but it sure did and thats why I had to take the pain of posting it up...

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    Quote Originally Posted by juice
    The book that I am referring "Let us C++" by Yashwant Kanetkar has never said int main() or <iostream.h>, and I don't find any compelling reasons to do that either.
    I have provided you with compelling reasons. If you do not find them compelling, I will find it compelling to stop answering your questions.

    Mind you, despite my experience, I did not find your crucial mistake in this thread at a glance. It was only after I tried to compile that I discovered the problem. If I decided that because I had to do more than just copy and paste your code (i.e., I had to change void main to int main) to compile it and so I could not be bothered to try, you may well still be waiting for someone to help you right now.
    AndrewHunter likes this.
    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

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    How about changing compiler, then? Get Visual Studio 2010 Express, or Code::Blocks and MinGW and we'll if your code compiles, then!
    They're two of the best compilers that exists for C++ and they are very popular, and they both conform to the standard relatively well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by laserlight View Post
    I have provided you with compelling reasons. If you do not find them compelling, I will find it compelling to stop answering your questions.

    Mind you, despite my experience, I did not find your crucial mistake in this thread at a glance. It was only after I tried to compile that I discovered the problem. If I decided that because I had to do more than just copy and paste your code (i.e., I had to change void main to int main) to compile it and so I could not be bothered to try, you may well still be waiting for someone to help you right now.
    Fair enogh. Thats convincing enough for me to change my style of coding. And as per the code, it compiled with my compiler with the following warning which I overlooked.
    "warning C4518: 'float ' : storage-class or type specifier(s) unexpected here; ignored"

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    What outdated version of the compiler are you using? 4.0?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by Elysia View Post
    How about changing compiler, then? Get Visual Studio 2010 Express, or Code::Blocks and MinGW and we'll if your code compiles, then!
    They're two of the best compilers that exists for C++ and they are very popular, and they both conform to the standard relatively well.
    I have visual studio 2010 but I am a newbie, and I am not that apt at using these modern compilers. So it will take some time for me to catch up!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with an error in the output of my code!
    By justiliang in forum C Programming
    Replies: 11
    Last Post: 09-24-2011, 09:29 AM
  2. compiler error not formatting output
    By Joeg1484 in forum Linux Programming
    Replies: 8
    Last Post: 04-06-2011, 03:08 PM
  3. sprintf output error
    By Ji33my in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2010, 10:53 PM
  4. rcp error output question
    By cristane in forum Linux Programming
    Replies: 1
    Last Post: 07-18-2005, 09:47 PM
  5. Error End of OutPut
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 07-01-2005, 08:43 AM

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