Hi I am learning C++ and i coded this program but it isnt working as it was supposed to..

#include<iostream.h>
using namespace std;

class furniture

{

        protected:

                int color,width, height;

};


class bookshelf : public furniture

{

        private:

                int no_of_shelves;

        public:

        int accept() {

                cout<<"enter color"<<endl;
                cin>>color;

                cout<<"enter width"<<endl;
                cin>>width;

                cout<<"enter height"<<endl;
                cin>>height;

                cout<<"enter no of shelves"<<endl;
                cin>>no_of_shelves;

                }


        int display() {

                cout<<"color is "<<color<<endl;
                cout<<"width is "<<width<<endl;
                cout<<"hegiht is "<<height<<endl;
                cout<<" no of shelevs is "<<no_of_shelves<<endl;

                }
        };

int main() {

        bookshelf b;
        b.accept();
        b.display();
        }

output is as follows:

enter color
aa
enter width
enter height
enter no of shelves
color is 0
width is 0
hegiht is -1073744596
no of shelevs is -1881141193

it just asks for color and rest of it is a mess..! :( can u tell me what's goign wrong ? thanks!

Recommended Answers

All 11 Replies

oops.. i didnt realize that color is int! grrrr

in your code at the first place use void accept() and void display ()
these two functions are not returning any integer values so why use int data type for them .

#include<iostream.h>
using namespace std;

class furniture

{

        protected:

                float width, height;
                char color[10];

};


class bookshelf : public furniture

{

        private:

                int no_of_shelves;

        public:

        void accept() {

                cout<<"enter color"<<endl;
                cin>>color;

                cout<<"enter width"<<endl;
                cin>>width;

                cout<<"enter height"<<endl;
                cin>>height;

                cout<<"enter no of shelves"<<endl;
                cin>>no_of_shelves;

                }


        void display() {

                cout<<"color is "<<color<<endl;
                cout<<"width is "<<width<<endl;
                cout<<"hegiht is "<<height<<endl;
                cout<<" no of shelevs is "<<no_of_shelves<<endl;

                }
        };

int main() {

        bookshelf b;
        b.accept();
        b.display();
        }

i have changed the width and height to be in float so that they can have better accuracy .

This code won't compile. You cannot use namespace with old c++ library (iostream.h).
Drop the second line of your code or use iostream instead of iostream.h

This code won't compile. You cannot use namespace with old c++ library (iostream.h).
Drop the second line of your code or use iostream instead of iostream.h

since the person itself has written the code using namepace . i assume that she is working on gcc compiler ,not the old tatted turbo c . thus there isnt any thing wrong with the code i have mentioned .

>there isnt any thing wrong with the code i have mentioned .
The person must be working with a 'magical' compiler. Since no standard compiler allow you to use iostream.h with namespaces. If you are using namespaces, you must use the iostream and not iostream.h
As far as your code is concerned, it is clear that you have not compiled the program. Don't worry, it is just fine. You are not expected to check and compile the code before posting. I am not blaming you but the OP.
BTW, I would surely like to ask the OP what compiler he/she is using because I have seen previously such code posted by him/her.

since the person itself has written the code using namepace . i assume that she is working on gcc compiler ,not the old tatted turbo c . thus there isnt any thing wrong with the code i have mentioned .

Wrong, look here ...

Wrong, look here ...

well i guess pal , u need to see her problem mentioned . she could nt display some elements , she never mentioned about error caused by namespace.now why would you assume that ...?

Dear rahul,

I thought you were saying that compiling the following is possible:

#include <iostream.h>
using namespace std;
...

So, I must have misinterpreted your post, sorry for that !

The person must be working with a 'magical' compiler. Since no standard compiler allow you to use iostream.h with namespaces. If you are using namespaces, you must use the iostream and not iostream.h

may be in your neighborhood . but you see not all have grown up from there .
well it might give you a warning . may be a bad practice . but surely it compiles and fetches the right output .

As far as your code is concerned, it is clear that you have not compiled the program.

well seems like person is loathed with overconfidence.when i write post i write with utmost honesty .

A C++ compiler which follows the ANSI/ISO-standard will never compile this code ...

may be in your neighborhood . but you see not all have grown up from there .
well it might give you a warning . may be a bad practice . but surely it compiles and fetches the right output .

Well, I don't do useless discussion. tux4life is absolutely right in saying that no standard compiler (standard compiler means complying to ANSI/ISO standard) can compile that code.
Now I don't want any posting regarding this matter. Any issues regarding this can be done through PM. This will make sure that you don't spread dirt here.
BTW, if you know any compiler compiling that code, please do tell me. I will never use it. Furthermore, it is not likely for you to argue when:
1:You are wrong.
and most importantly,
2:You know you are wrong.
Thats it!

PS: If you are interested( and ready to shed some money) you may like to see the C++ ANSI/ISO standards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.