New at C++, getting weird errors.

Okay, so I'm a beginner at C++. I have this homework problem:

Write a C++ program that computes and outputs the volume of a cone, given the diameter of its base and its height. The formula for computing the cone's volume is:
1/3 PI * Radius ^2 * Height.

I used the <cmath>, and <iostream> and all of that. Should I use <iomanip>? Do I have to use setprecision anywhere?

EDIT: Sorry, this really should be in the beginner section :X

EDIT:

Here's the code:

//This program will compute the volume of a cone given the diameter of its base and its height.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
const double PI = 3.14159; //Value of PI, and it cannot be changed.

//Local Variables
double radius, diameter, height, volume;

//Calculate values.
diameter = radius / .5;
volume = (1/3.0) * PI * pow (radius, 2.0) * height;

//Output results
cout << "Enter diameter of the cone's base." << endl;
cin >> diameter >> endl;

diameter = radius / .5;

cout << "Enter the height of the cone." << endl;
cin >> height >> endl;

volume = (1.0/3.0) * PI * pow (radius, 2.0) * height;

//Now that we have the height and diameter we can get the volume of the cone.
cout << "The volume of the cone is " << volume << endl;

return 0;

}
Last edited on
Are you using ^ for exponentiation? ^ is the bitwise XOR operator, you need need to use pow().
http://www.cplusplus.com/reference/clibrary/cmath/pow/
yeah I used pow. I had pow (radius, 2.0) I believe. Once I get home, I'll post my entire code which would probably be more helpful.
Last edited on
you don't need iomanip unless you have to do fancy output

same for set precision...


did you using namespace std; or std::something?
I used using namespace std;

//This program will compute the volume of a cone given the diameter of its base and its height.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
const double PI = 3.14159; //Value of PI, and it cannot be changed.

//Local Variables
double radius, diameter, height, volume;

//Calculate values.
diameter = radius / .5;
volume = (1/3.0) * PI * pow (radius, 2.0) * height;

//Output results
cout << "Enter diameter of the cone's base." << endl;
cin >> diameter >> endl;

diameter = radius / .5;

cout << "Enter the height of the cone." << endl;
cin >> height >> endl;

volume = (1.0/3.0) * PI * pow (radius, 2.0) * height;

//Now that we have the height and diameter we can get the volume of the cone.
cout << "The volume of the cone is " << volume << endl;

return 0;

}


that's the code I put in. but this is what happens when I build it:

1>------ Build started: Project: homework3, Configuration: Debug Win32 ------
1>Compiling...
1>homework3.cpp
1>c:\documents and settings\jolie elliott\my documents\visual studio 2008\projects\homework3\homework3.cpp(27) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1144): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1146): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1148): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1150): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(155): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(161): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]



so on, so forth
>> endl
Never do that.
Last edited on
Is there a reason why you shouldn't?
endl is for output only

you have your calculations twice in your file, you should remove the ones before your first cin (they are operating on bogus data)

code tags please !! the # icon on the right
Last edited on
Okay, I've been playing around with it now. And I retyped the code to:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//This program will compute the volume of a cone given the diameter of its base and its height.

#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.1415926;//Value of PI and it cannot be modified

int main()
{

double diameter, radius, volume;
int height;

cout << "Enter the diameter of the cone's base." << endl;
cin >> diameter;

radius = diameter / 2.0; //Calculates the radius of the base.

cout << "Enter the height of the cone." << endl;
cin >> height;

volume = ((1/3.0) * PI) * pow (radius, 2.0) * height; //Calculates the volume of the cone.

cout << "The volume of the cone is " << volume << endl;

	return 0;
}



If any of you wanted to try it out and maybe see if it works correctly for you too, that would be greatly appreciated!
Last edited on
Good job ;)

Why is height an int?

some food for thought:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
using namespace std;

const double PI = 3.1415926;//Value of PI and it cannot be modified

int main()
{
    double volume(double radius, double height);
    double diameter, height;
    
    cout << "Enter the diameter of the cone's base." << endl;
    cin >> diameter;
    cout << "Enter the height of the cone." << endl;
    cin >> height;
    
    cout << "The volume of the cone is " << volume(diameter/2,height) << endl;

    return 0;
}

double volume(double radius, double height)
{
    return pow(radius, 2.0)/3.0 * height * PI;
}
Thank you!
Yeah, I changed that to double with the rest :)
Topic archived. No new replies allowed.