question

Pages: 12
i need some help please i keep trying stuff out for this code but nothing works, can some one please help me out and tell me what i am doing wrong.

#include <iostream>
#define pi 3.1415926535
// Definition of the function (area of cone)
double cone_area(double height, double radius)
{
return (1/3)*pi*radius*height;
}

void main()
{
double raidus,height;
cout << "We are going to calculate the area of a cone" << end-el;
cout << "xxxxx" <<;
cin >> radius;
cout << endl;
cout << "yyyyy " <<;
cin >> height;
cout << "zzzzz" << cone_area(height,radius) << end-el;
}

here is the errors i get when i try to build it
1>------ Rebuild All started: Project: page 137 with constants, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'page 137 with constants', configuration 'Debug|Win32'
1>Compiling...
1>page 137 with constants.cpp
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(12) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(12) : error C2065: 'end' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(12) : error C2065: 'el' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(13) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(13) : error C2059: syntax error : ';'
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(14) : error C2065: 'cin' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(14) : error C2065: 'radius' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(15) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(15) : error C2065: 'endl' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(16) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(16) : error C2059: syntax error : ';'
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(17) : error C2065: 'cin' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(18) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(18) : error C2065: 'radius' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(18) : error C2065: 'end' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(18) : error C2065: 'el' : undeclared identifier
1>Build log was saved at "file://c:\Users\tom\Documents\Visual Studio 2008\Projects\page 137 with constants\page 137 with constants\Debug\BuildLog.htm"
1>page 137 with constants - 16 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
std::cout
std::endl

to start with.

cone_area always returns 0 since 1/3 == 0 in integer division.

you have various other syntactic errors.

end-el ???
<< ; ???
i am sorry if i sound like i am stupid but i am new to c++, i don't understand what you mean
#include <iostream>
#define pi 3.1415926535


add
using namespace std;

in addition instead of

return (1/3)*pi*radius*height;


use return (1/3.0)*pi*radius*height;

Because the 1/3 expression means value "1" integer / value "3" integer therefore it yields value 0

The 1/3.0 the right expression because it use float value 3.0 and it will be implicit converted to double.

void main()
{
double raidus,height;
cout << "We are going to calculate the area of a cone" << end-el;
cout << "xxxxx" <<;



And fix your errors:
1
2
3
4
5
void main()
{
double raidus,height;
cout << "We are going to calculate the area of a cone" << endl;
cout << "xxxxx";

here is the new code andthe errors iget with it can some one please help me figure this out.

#include <iostream>
#define pi 3.1415926535;
namespace
// Definition of the function (area of cone)
cone_area (double height, double radius);
{
return (1/3.0)*pi*radius*height;
}

void main()
{
double raidus,height;
cout << "We are going to calculate the area of a cone" << endl;
cout << "xxxxx";
cin >> radius;
cout << endl;
cout << "yyyyy ";
cin >> height;
cout << "zzzzz" << cone_area(height,radius) << end-el;
}

1>------ Rebuild All started: Project: page 137 with constants, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'page 137 with constants', configuration 'Debug|Win32'
1>Compiling...
1>page 137 with constants.cpp
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(5) : error C2059: syntax error : '('
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(6) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(13) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(13) : error C2065: 'endl' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(14) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(15) : error C2065: 'cin' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(15) : error C2065: 'radius' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(16) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(16) : error C2065: 'endl' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(17) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(18) : error C2065: 'cin' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(19) : error C2065: 'cout' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(19) : error C2065: 'radius' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(19) : error C2065: 'end' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(19) : error C2065: 'el' : undeclared identifier
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(19) : error C3861: 'cone_area': identifier not found
1>Build log was saved at "file://c:\Users\tom\Documents\Visual Studio 2008\Projects\page 137 with constants\page 137 with constants\Debug\BuildLog.htm"
1>page 137 with constants - 16 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
using namespace std;

not

namespace
As well as what jsmith said, there are also a load of other errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#define pi 3.1415926535; //This semicolon should not be there - 
namespace //should be using namespace std;

// Definition of the function (area of cone)
cone_area (double height, double radius); //semicolon should not be here and function should have a return-type
{
return (1/3.0)*pi*radius*height;
}

void main() //should be int main() NOT void
{
double raidus,height; //misspelling of  radius
cout << "We are going to calculate the area of a cone" << endl;
cout << "xxxxx";
cin >> radius;
cout << endl;
cout << "yyyyy ";
cin >> height;
cout << "zzzzz" << cone_area(height,radius) << end-el; // should be endl NOT end-el
}
here is the new code as i have fixed it

#include <iostream>
#define pi 3.1415926535
using namespace std;

// Definition of the function (area of cone)
cone_area (double height, double radius)
{
return (1/3.0)*pi*radius*height;
}

int main()
{
double radius,height;
cout << "We are going to calculate the area of a cone" << endl;
cout << "xxxxx";
cin >> radius;
cout << endl;
cout << "yyyyy ";
cin >> height;
cout << "zzzzz" << cone_area(height,radius) << endl;
}

1>------ Rebuild All started: Project: page 137 with constants, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'page 137 with constants', configuration 'Debug|Win32'
1>Compiling...
1>page 137 with constants.cpp
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\tom\documents\visual studio 2008\projects\page 137 with constants\page 137 with constants\page 137 with constants.cpp(8) : warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data
1>Build log was saved at "file://c:\Users\tom\Documents\Visual Studio 2008\Projects\page 137 with constants\page 137 with constants\Debug\BuildLog.htm"
1>page 137 with constants - 1 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
guestgulkan wrote:
...and function should have a return-type...

for more information read here:
http://www.cplusplus.com/doc/tutorial/functions/

So you are missing a data type specifier for your function cone_area (double height, double radius)

It should be:
double cone_area (double height, double radius)
because your function is going (i mean you want) to return a floating-point number (Optionally you can use float, but double has better precision (float 32 bits[S1,E8,M23], double 64 bits[S1,E11,M52])

edit

it's about time to use [code][/ code] tags :-)
Last edited on
here is the code i have and it works but the the teacher says it does not do what it is suppose to
#include <iostream>
#define pi 3.1415926535
using namespace std;

// Definition of the function (area of cone)
int cone_area (double height, double radius)
{
return (1/3.0)*pi*radius*height;
}

int main()
{
double radius,height;
cout << "We are going to calculate the area of a cone" << endl;
cout << "xxxxx";
cin >> radius;
cout << endl;
cout << "yyyyy ";
cin >> height;
cout << "zzzzz" << cone_area(height,radius) << endl;
}
here is what it says when i run it
we are going to calculate the area of a cone
xxxxx
and this is what the teacher wrote to me
There are no errors, but the program doesn’t do what it’s supposed to do – and it looks like it is sitting there waiting for you to input a value for the radius. You need to add prompts for the inputs, and see if the program actually calculates and outputs as it should.

Please for the love of my eyes, use [code] tags!!! It's easy, just click the # on the right of the screen, under Format:.

What the teacher means is that rather than just, to use his/her words, "sit there waiting". For example, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#define pi 3.1415926535
using namespace std;

// Definition of the function (area of cone)
int cone_area (double height, double radius)
{
return (1/3.0)*pi*radius*height;
}

int main()
{
double radius,height;
cout << "We are going to calculate the area of a cone" << endl;
cout << "Please enter a number in the format xxxxx";
cin >> radius;
cout << "\n";
cout << "Please enter a number in the format yyyyy ";
cin >> height;
cout << " zzzzz" << cone_area(height,radius) << endl;
}



I can't go farther than that without know what the program is "supposed to do".
Last edited on
the assignment is
Use constants for pi, radius, and the height (since we haven't covered imputing data yet- although you might have already read about it in chapter 4). format the output as shown below:
Radius height volume
xxxxx yyyyy zzzzz
output 2 decimal place. use proper formatting and appropriate comments
Right. Well you aren't doing that.

If you make cone_area return double (which it should, because it accepts arguments as double and returns a value related to the arguments) then it will return it's output to however many decimal places it uses. You could use printf ( http://www.cplusplus.com/reference/clibrary/cstdio/printf/ ) to do that. You also aren't commenting your code.

As constants, rather than use #define, which is a feature of C and the C preprocessor, you could define PI as const double PI.

I'm not sure what s/he means when s/he says to use constants for radius and height. What is this program meant to do if it doesn't take input???
the problem that it was all based on was
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/3pi x radius cubed x height
and then he wrote

Use constants for pi, radius, and the height (since we haven't covered imputing data yet- although you might have already read about it in chapter 4). format the output as shown below:
Radius height volume
xxxxx yyyyy zzzzz
output 2 decimal place. use proper formatting and appropriate comments
You need to comment your code then, and I'm still unsure of exactly how your program relates. This is how I'd do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>

int main() {
    const double pi = 3.1415926535; // pi cannot be modified
    int height = 0, radius = 0; // Initialize two integers, height and radius, to 0.
    std::cout << "Please enter the height: ";
    std::cin  << height;
    std::cout << "\nPlease enter the radius: ";
    std::cin  << radius;
    // Now we have the height and radius, we can calculate the result:
    std::cout << "\nResult: " << (1 / 3.0 * pi) * (pow(radius, 3) * height) << "\n";
    std::cin.get();
    return 0;
}

Note that that's just an example.

Or have you got to use a function?

Oh and you aren't cubing (x3) your radius. It says radius cubed. You will need to use the pow() function which is included in math.h (or cmath in the stl).
Last edited on
it should be radius squared instead
Are you trying to calculate the surface area, or the volume? 1.0/3.0*pi*r*r*h is the volume. The surface area is pi*r*r+pi*r*sqrt(r*r+h*h).
Last edited on
the volume
now this is what the teacher had to say
You still haven’t showed me the program running. You have no prompts for the input, and have apparently not input anything. You don’t know if it works or not so can someone please help me and let me know what he means so i can get this assignment done
for a start, put in using namespace std. also, make sure you spell variable names correctly.
Pages: 12