CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 5 FirstFirst 12345 LastLast
Results 31 to 45 of 68
  1. #31
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    2kaud i copied with Alt and # pasted it with nice style but when i send i saw that it doesnt change

  2. #32
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    2kaud i copied with Alt and # pasted it with nice style but when i send i saw that it doesnt change
    Before posting, format your code properly with indentations etc. Then Go Advanced, paste in the code, select the code and then click '#'. To format the code you have already posted, on the bottom of your post #12 you should see 'edit post'. Click this, then click Go Advanced, select the code then click '#' then click save. Your code should then be posted correctly formatted.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #33
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Area and volume of cylinder

    I tried to do as your opinions still i am getting error. If you help me i will be learn how to do. Thank you.
    In a class, by default all members are private - which basically means that they cannot be accessed outside of the class. This is fine for member variables like r and h, but functions such as area and volume need to accessed anywhere. Hence these need to be public. So you need public: before the start of these function definitions. Have a look at
    http://www.learncpp.com/cpp-tutorial...ss-specifiers/

    In main function, you try to obtain the values of r and h, but r and h aren't declared in function main! You need to declare them in main. Then you need a method function of the class to set the class variables r and h from these values entered. The variables r and h used in function main are not the same variables r and h defined within the class. You should also initialise the class variables to 0 when the class is instantiated by using a default class constructor. Have a look at
    http://www.learncpp.com/cpp-tutorial...class-members/
    Last edited by 2kaud; May 23rd, 2013 at 01:58 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #34
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    const float PI=3.14;
     
    class cylinder{
          double r, h; 
    public:
           double area();
           double volume();
    double volume(double radius,double height){
           r=radius;
           h=height;
           return PI*r*r*h;
           }
    double area(double radius,double height){
           r=radius;
           h=height;       
           return 2*PI*r*r*+2*PI*r*h;
    }
    }c;
    int main()
    {
    	int ch;
    	double x,y;
    	cout<<"\tCALCULATION OF AREA AND VOLUME";
        cout<<"\n1.Area of the cylinder";
    	cout<<"\n2. Volume of the cylinder";
    	cout<<"\n\tEnter your choice ";
    	cin>>ch;
    	switch(ch)
    	{		
    		case 1:
    			cout<<"\nEnter the radius";
    			cin>>x;
    			cout<<"\nEnter the height";
    			cin>>y;
    			break;
    		case 2:
    			cout<<"\nEnter the radius";
    			cin>>x;
    			cout<<"\nEnter the height";
    			cin>>y;
    			break;
    		default:
    			cout<<"\nThe choice entered is a wrong choice";
    	}
    	getch();
    }
    At least i show to the terminal but i am selecting for example to the 1 then asking radius and height but doesnt calculate.

  5. #35
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Area and volume of cylinder

    Again, you're guessing without really understanding what you're working with.

    First, you need to create an instance of cylinder. In main, write a line of code like this.

    Code:
    cylinder c;
    Replace the c; from the end of the cylinder declaration with just a ;

    Access r and h through your c object, such as c.r.

    You're not calculating area and volume because there's nothing in your code to call those functions.

    Your code in case 1 and case 2 is identical, which should tell you you've done something wrong. Move the input out of the switch statement and use case 1 and case to call the appropriate function.

  6. #36
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    const float PI=3.14;
     
    class cylinder{
          double r, h; 
    public:
           double area();
           double volume();
    double volume(double radius,double height){
           r=radius;
           h=height;
           return PI*r*r*h;
           }
    double area(double radius,double height){
           r=radius;
           h=height;       
           return 2*PI*r*r*+2*PI*r*h;
    }
    }c;
    int main()
    {
    	int ch;
    	cylinder c;
    	c.r;
    	c.h;	
    	double area(x,y);
    	double volume(x,y);
    	cout<<"\tCALCULATION OF AREA AND VOLUME";
        cout<<"\n1.Area of the cylinder";
    	cout<<"\n2. Volume of the cylinder";
    	cout<<"\n\tEnter your choice ";
    	cin>>ch;
    	switch(ch)
    	{		
    		case 1:
    			cout<<"\nEnter the radius";
    			cin>>x;
    			cout<<"\nEnter the height";
    			cin>>y;
    			break;
    		case 2:
    			cout<<"\nEnter the radius";
    			cin>>x;
    			cout<<"\nEnter the height";
    			cin>>y;
    			break;
    		default:
    			cout<<"\nThe choice entered is a wrong choice";
    			 cout<< "Area =" << c.area() << "\n";
                 cout<< "Volume=" << c.volume() << "\n";
    	}
    	getch();
    }
    Actually i mixed.

  7. #37
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Area and volume of cylinder

    A lot of your code is nonsense. You need to take a step back and reread whatever book or whatever you've been learning from. We're telling you things that you're not understanding, and you need to have a grasp of the basics that you currently lack. I'm serious that you can't guess your way through C++. You really need to understand what every line you write does.

  8. #38
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Area and volume of cylinder

    Well you're getting closer!

    In your case statements, you haven't actually called the class functions! So in case 1 for example you need

    Code:
    cin>>y;
    cout << "Area is: " << c.area(x, y) << endl;
    and similar for case 2.

    Your calculation for area is not quite right
    Code:
    return 2*PI*r*r*+2*PI*r*h;
    As you are passing r and h via the functions area(..) and volume(..) you don't actually need
    Code:
    double area();
    double volume();
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #39
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Area and volume of cylinder

    Code:
    default:
    	cout<<"\nThe choice entered is a wrong choice";
            cout<< "Area =" << c.area() << "\n";
            cout<< "Volume=" << c.volume() << "\n";
    So you are only going to calculate the area and volume when the user enters a wrong choice???? Doh!!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #40
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    2kaud, i would like to thank you.
    With patience you teach me.
    Aim of this forum should be guidence.
    GCDEF you said "read from book" so you shouldnt give responce.
    Why you reply
    I understand, but you dont want to help.And you dont know how to talk with a lady.
    2kaud, Tomorrow i will try again.
    Thank you.

  11. #41
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Area and volume of cylinder

    Quote Originally Posted by GCDEF View Post
    A lot of your code is nonsense. You need to take a step back and reread whatever book or whatever you've been learning from. We're telling you things that you're not understanding, and you need to have a grasp of the basics that you currently lack. I'm serious that you can't guess your way through C++. You really need to understand what every line you write does.
    GCDEF is quite correct. You cannot guess c++. You must understand what you are doing. You must have a grasp of the basics before you advance.

    As GCDEF said, you really need to take a step back and revise the areas of c++ that have been covered so far so that you understand the basics. Your code indicates that you don't understand - even with the guidance and advice that this forum has provided.

    What book(s) etc are you using from which to learn? If you are stuggling with a book, try looking at
    http://www.learncpp.com/

    which is an easy going tutorial for c++ starting with the basics.

    We'll provide what guidance we can, but we are not teachers. This forum cannot teach you c++ - only explain for the code you post problems with guidance to fix. But if you don't understand the basics of c++ you don't understand the guidance we have provided - what is what has been demonstrated in your recent posts. We won't write the code for you - that is cheating. You have to put in the effort to lean properly the language.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #42
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    2kaud, i would like to thank you.
    With patience you teach me.
    Aim of this forum should be guidence.
    GCDEF you said "read from book" so you shouldnt give responce.
    Why you reply
    I understand, but you dont want to help.And you dont know how to talk with a lady.
    2kaud, Tomorrow i will try again.
    Thank you.
    Whether you like my response or not, it's the truth. I've been trying to help, but you there's only so much any of us can do given your current knowledge. "Learn the basics" is good advice. As to your comment about talking with a lady, that's completely inappropriate in this forum.

  13. #43
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Area and volume of cylinder

    Quote Originally Posted by 2kaud View Post
    GCDEF is quite correct.
    Thank you.

  14. #44
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    I am right, your talking style so rude.
    And I am learning basics as doing applications with helpfull programmers.
    You dont want my learning.
    Anyway

  15. #45
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Area and volume of cylinder

    Quote Originally Posted by GCDEF View Post
    Access r and h through your c object, such as c.r.
    r and h are private.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 3 of 5 FirstFirst 12345 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured