Area and volume of cylinder - Page 4
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 4 of 5 FirstFirst 12345 LastLast
Results 46 to 60 of 68

Thread: Area and volume of cylinder

  1. #46
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    Re: Area and volume of cylinder

    Quote Originally Posted by 2kaud View Post
    r and h are private.
    By accident.

  2. #47
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    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
    How's that working out for you so far? I'm not seeing any progress. I do want you learning. My advice to you was to get a that would teach you the basics. With the fundamentals you'll get nowhere. Surely this thread is illustrating that. You can did your heals in if it makes you feel better, but in the long run, it won't help you any.

  3. #48
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Hi again,
    Today, i made some changes as your advices. This is working but i know some mistakes. I wanted to make program automatic can understand number types. I am waiting your comments.

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    const float PI=3.14;
    
    class calculate
    {
    	public:
    		void cylinder_area(int r,int h);
    		void cylinder_area(float r1,float h1);
    		void cylinder_area(double r2, double h2);
    		void cylinder_volume(int r3,int h3);
    		void cylinder_volume(float r4,float h4);
    	              void cylinder_volume(double r5, double h5);
    };
    void calculate::cylinder_area(int r, int h)
    {
    	cout<<"\n Area of Cylinder: "<<(2 * PI * r)*( r + h);
    }
    void calculate::cylinder_area(float r1, float h1)
    {
    	cout<<"\nArea of Cylinder "<<(2 * PI * r1)*( r1 + h1);
    }
    void calculate::cylinder_area(double r2, double h2)
    {
    	cout<<"\nArea of Cylinder "<<(2 * PI * r2)*( r2 + h2);
    }
    void calculate::cylinder_volume(int r3, int h3)
    {
    	cout<<"\nVolume of Cylinder "<<(2 * PI * r3 * h3);
    }
    void calculate::cylinder_volume(float r4, float h4)
    {
    	cout<<"\nVolume of Cylinder "<<(2 * PI * r4 * h4);
    }
    void calculate::cylinder_volume(double r5, double h5)
    {
    	cout<<"\nVolume of Cylinder "<<(2 * PI * r5 * h5);
    }
    int main()
    {
    	int ch;
    	int r,h,r3,h3;
    	float r1,h1,r4,h4;
    	double r2,h2,r5,h5;
    	calculate obj;
    	cout<<"\tTHIS PROGRAM CALCULATES AREA AND VOLUME OF CYLINDER\n";
    	cout<<"\n1. Area of Cylinder\n";
    	cout<<"\n2. Volume of Cylinder\n";
    	cout<<"\n\tPlease select\n";
    	cin>>ch;
    	switch(ch)
    	{       
           case 1:
    			cout<<"\nEnter radius of Cylinder\n";
    			cin>>r;
    			cout<<"\nEnter height of Cylinder\n";
    			cin>>h;
    			obj.cylinder_area(r,h);
    					   
    			break;
                
     		case 2:
    			cout<<"\nEnter radius of Cylinder:\n";
    			cin>>r3;
    			cout<<"\Enter height of Cylinder\n";
    			cin>>h3;
    			obj.cylinder_volume(r3,h3);
    		
    			break;
    		default:
    			cout<<"\nThe choice entered is a wrong choice";
    	}
    	getch();
    }

  4. #49
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    Re: Area and volume of cylinder

    Much better. You still have redundancies in your switch statement. It could/should be written as
    Code:
    	cout<<"\nEnter radius of Cylinder\n";
    	cin>>r;
    	cout<<"\nEnter height of Cylinder\n";
    	cin>>h;
     	switch(ch)
     	{       
                    case 1:
    			obj.cylinder_area(r,h);
    			break;
    		case 2:
    			obj.cylinder_volume(r,h);
    			break;
    		default:
    			cout<<"\nThe choice entered is a wrong choice";
    	}
    Typically, calculate function will return a value, not actually output it. So a better design would be for the cout statements to be in main, and just have your calculate functions return the value they calculate.
    Last edited by GCDEF; May 24th, 2013 at 12:33 PM.

  5. #50
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

    Re: Area and volume of cylinder

    As GCDEF said, much improved. Your class calculate now has various overloaded functions for cylinder_area and cylinder_volume. So from you main function, how are you going to allow the user to input data as either int, float or double? How are going to test all the overloaded class functions to make sure they all produce the correct results?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #51
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Thank you i did with your helps.

    I tested to the program when user input second choice with integer there is no problem.
    But in the second choice when user input for example 3.75 it is calculate volume too,
    How i can handle it.

    Thank you.Name:  C++.jpg
Views: 55
Size:  28.0 KB

  7. #52
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

    Re: Area and volume of cylinder

    In main, r and h are defined as integers. cin >> r tries to get an integer and reads 3. cin >> h tries to read another integer but finds . which isn't a digit so the cin fails. Hence h hasn't been changed by the cin. As r and h haven't been previously initialised, h contains whatever happens to be in the memory location used for h. Hence the weid result.

    You should check that cin has succeeded after it's used and if it has failed, output an error message. Also r and h should be initialised (usually to 0) when they are defined.

    This leads on from my question in post #50 about how are you going to allow the user to input of either int, float or double?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #53
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Code:
    cout<<"\nEnter radius of Cylinder\n";
    	cin>>r;
    	cout<<"\nEnter height of Cylinder\n";
    	cin>>h;
     	switch(ch)
     	{       
                    case 1:
    			obj.cylinder_area(r,h);
    			break;
    		case 2:
    			obj.cylinder_volume(r,h);
    			break;
    		default:
    			cout<<"\nThe choice entered is a wrong choice";
    	}
    GCDEF when i did with your style i am getting error should i enter other object parameters.

  9. #54
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Should i use if else for example if(r==float){
    }
    and which part of the codes i have to check.

  10. #55
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    Should i use if else for example if(r==float){
    }
    and which part of the codes i have to check.
    No, you cannot do this. r is defined as type integer. You cannot do comparisons against type.

    Again, you are guessing without understanding what you are doing. You cannot guess with c++ - you must know and understand the basics before progressing.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #56
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    GCDEF when i did with your style i am getting error should i enter other object parameters.
    What error are you getting? I tried it and it works for me.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #57
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,050

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    Code:
    cout<<"\nEnter radius of Cylinder\n";
    	cin>>r;
    	cout<<"\nEnter height of Cylinder\n";
    	cin>>h;
     	switch(ch)
     	{       
                    case 1:
    			obj.cylinder_area(r,h);
    			break;
    		case 2:
    			obj.cylinder_volume(r,h);
    			break;
    		default:
    			cout<<"\nThe choice entered is a wrong choice";
    	}
    GCDEF when i did with your style i am getting error should i enter other object parameters.
    Can you show the code please

  13. #58
    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 calculate
    {
    	public:
    		void cylinder_area(int r,int h);
    		void cylinder_area(float r1,float h1);
    		void cylinder_area(double r2, double h2);
    		void cylinder_volume(int r3,int h3);
    		void cylinder_volume(float r4,float h4);
    	    void cylinder_volume(double r5, double h5);
    };
    void calculate::cylinder_area(int r, int h)
    {
    	cout<<"\n Area of Cylinder: "<<(2 * PI * r)*( r + h);
    }
    void calculate::cylinder_area(float r1, float h1)
    {
    	cout<<"\nArea of Cylinder "<<(2 * PI * r1)*( r1 + h1);
    }
    void calculate::cylinder_area(double r2, double h2)
    {
    	cout<<"\nArea of Cylinder "<<(2 * PI * r2)*( r2 + h2);
    }
    void calculate::cylinder_volume(int r3, int h3)
    {
    	cout<<"\nVolume of Cylinder "<<(2 * PI * r3 * h3);
    }
    void calculate::cylinder_volume(float r4, float h4)
    {
    	cout<<"\nVolume of Cylinder "<<(2 * PI * r4 * h4);
    }
    void calculate::cylinder_volume(double r5, double h5)
    {
    	cout<<"\nVolume of Cylinder "<<(2 * PI * r5 * h5);
    }
    int main()
    {
    	int ch;
    	int r,h,r3,h3;
    	float r1,h1,r4,h4;
    	double r2,h2,r5,h5;
    	calculate obj;
    	cout<<"\tTHIS PROGRAM CALCULATES AREA AND VOLUME OF CYLINDER\n";
    	cout<<"\n1. Area of Cylinder\n";
    	cout<<"\n2. Volume of Cylinder\n";
    	cout<<"\n\tPlease select\n";
    	cin>>ch;
    	switch(ch)
    	{ case 1:
    			obj.cylinder_area(r,h);
    			break;
    	  case 2:
    			obj.cylinder_volume(r,h);
    			break;
    	  		default:
    			cout<<"\nThe choice entered is a wrong choice";
    
    	}
    	getch();
    }
    Program starts when i select option 2 calculating direct volume before sould ask height.
    Name:  C++2.jpg
Views: 42
Size:  37.0 KB

  14. #59
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    I know we cant compare number types you are right. Accidentaly i wrote it.

  15. #60
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,265

    Re: Area and volume of cylinder

    Quote Originally Posted by melissax View Post
    Program starts when i select option 2 calculating direct volume before sould ask height.
    So where is the code to ask the user to input radius and height? Where is

    Code:
    cout<<"\nEnter radius of Cylinder\n";
    cin>>r;
    cout<<"\nEnter height of Cylinder\n";
    cin>>h;
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 4 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center