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

Thread: Area and volume of cylinder

  1. #61
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    Name:  C++3.jpg
Views: 92
Size:  28.6 KB
    When i select option 1 there is no problem but when i enter double program sending volume value.
    Name:  C++4.jpg
Views: 89
Size:  22.3 KB
    Which part i have to make changes.

    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";
    	}
    This isnt work in my program.

  2. #62
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: Area and volume of cylinder

    You are probably reading into int variables.

    Consider the following:

    Code:
    int r , h;
    cin >> r;
    cin >> h;
    Reading into an int variable stops when

    1) a whitespace is encountered (space, tab, return)
    2) end-of-file
    3) a character that is invalid for a int variable is encountered

    So, if you enter 3.45 into an int variable (r), the variable gets a value of 3 ...
    Since the "." is an invalid character for an int. The internal read buffer still
    has the characters ".45" (without the quotes).

    Next, the program executes cin >> h;

    The buffer still has ".45" in it, so the code tries to read that into
    the int variable (h). This results in an immediate error (since the "." is
    invalid). The read fails ... so:

    1) The value of the variable h is undefined.
    2) Any read using cin will fail after this, until its state is reset:

    Code:
    cin.clear();
    However, that just resets the streams internal flags, the ".45" is still
    in the buffer. Normally, you could use the ignore() function.

  3. #63
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: Area and volume of cylinder

    This follows from my question in post #50. How are you going to have the user input int, float or double? How are you going to test your various class overloaded functions? You can't input a float/double number into a variable defined as int - although from post #8 you can input an int into a float or double and a float into a 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.

  4. #64
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    I tried cin>>clear(); after cin>>r; cin>>h but it gives error.

    clear undecleared for first use of function.
    then i insert #include<stdio.h> library.

    How can i handle.
    Thank you.

  5. #65
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: Area and volume of cylinder

    You're probably looking to write:
    Code:
    cin.clear();
    which is what Philip Nicoletti wrote in post #62.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #66
    Join Date
    Apr 2013
    Posts
    25

    Re: Area and volume of cylinder

    laserlight thank you,
    I used as philip said but stil jumping to the volume result
    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdio.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;
    			cin.clear();
               	cout<<"\nEnter height of Cylinder\n";
    			cin>>h;
    			cin.clear();
    			obj.cylinder_area(r,h); 			   
    			break;
     		case 2:
    			cout<<"\nEnter radius of Cylinder:\n";
    			cin>>r3;
    			cin.clear();
    		   	cout<<"\nEnter height of Cylinder:\n";
    			cin>>h3;
    			cin.clear();
               	obj.cylinder_volume(r3,h3);	
    			break;
    		default:
    			cout<<"\nThe choice entered is a wrong choice";
    	}
    	getch();
    }
    Name:  C++5.jpg
Views: 81
Size:  31.8 KB

  7. #67
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: Area and volume of cylinder

    You might want to re-read post #62, especially the very last part.

    By the way, I really don't think that your member functions should be printing anything. Design them to return the result instead. This will make them more reusable, and possibly more testable too.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #68
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: Area and volume of cylinder

    Quote Originally Posted by laserlight View Post
    You might want to re-read post #62, especially the very last part.
    Yes, but the current issue with this homework assignment is that the program needs to be able to input r and h and call the overloaded class functions with either int, float or double arguments. So the program needs to to able to input real numbers (float or double) as well as ints.
    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 5 of 5 FirstFirst ... 2345

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