2kaud i copied with Alt and # pasted it with nice style but when i send i saw that it doesnt change
Printable View
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.
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 atQuote:
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.
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/
At least i show to the terminal but i am selecting for example to the 1 then asking radius and height but doesnt calculate.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();
}
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.
Replace the c; from the end of the cylinder declaration with just a ;Code:cylinder c;
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.
Actually i mixed.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();
}
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.
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
and similar for case 2.Code:cin>>y;
cout << "Area is: " << c.area(x, y) << endl;
Your calculation for area is not quite right
As you are passing r and h via the functions area(..) and volume(..) you don't actually needCode:return 2*PI*r*r*+2*PI*r*h;
Code:double area();
double volume();
So you are only going to calculate the area and volume when the user enters a wrong choice???? Doh!!Code:default:
cout<<"\nThe choice entered is a wrong choice";
cout<< "Area =" << c.area() << "\n";
cout<< "Volume=" << c.volume() << "\n";
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.
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.:)
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.
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