Thread: Output error

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

    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
    28,413
    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()
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    2,739
    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
    385
    Damn!!

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    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
    2,739
    Relieve you, maybe. Driving insane your future coworkers, that's for sure!
    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
    385
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    385
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    385
    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!!

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