-1

This is a very basic problem, I need to find the area and volume of a sphere but I am getting this error:

 error C2065: 'v' : undeclared identifier
 error C2065: 'a' : undeclared identifier

Here is my program:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int r;
int computeSphere(int r) {
    double a,v;
    a = 4*3.14* pow(r,2);
    v = (4/3) * 3.14 * pow(r,3);

    return a,v;
}

int main() {
    cout << "Enter the radius: ";
    cin >> r;
    cout << fixed << setprecision(2);

    computeSphere(r);

    cout << "The area of a sphere of radius " << r << " is " << a << " and its ";
    cout << "volume is ";
    cout << v;
    cout << endl;

    return 0;
}

the question says that The function should not perform any I/O operations. so how can I show the results ??

athspk
  • 6,722
  • 7
  • 37
  • 51
user1092492
  • 181
  • 1
  • 2
  • 5
  • 2
    That's not how functions work. Check what you just learned in class to figure out what you're supposed to do. (you can do this with pointers, references, structs, classes, macros, or other things) – SLaks Dec 11 '11 at 17:43
  • 4
    Neither `return a,v;` nor `4/3` do what you think they do. Read a good book and try again when you've covered a few more of the basics of C++. – Kerrek SB Dec 11 '11 at 17:44
  • possible duplicate of [What is an 'undeclared identifier' error and how do I fix it?](http://stackoverflow.com/questions/22197030/what-is-an-undeclared-identifier-error-and-how-do-i-fix-it) – sashoalm Mar 05 '14 at 13:21

3 Answers3

4

Two problems:

  • You can't return multiple values from a function.
  • You aren't doing anything with the return value where you call the function.

One way to solve the first problem is to define a struct:

struct SphereStuff
{
    double a;
    double v;
};


SphereStuff computeSphere(double r)
{
    SphereStuff stuff;
    stuff.a = ...;
    stuff.v = ...;
    return stuff;
}

int main()
{
    SphereStuff s = computeSphere(42);        // ***
    std::cout << s.a << ", " << s.v << "\n";
}

Note also how I'm "collecting" the return value of the function (on the line marked with "***").

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

Divide it into two functions

double Volume (int r) { return (4/3) * 3.14 * pow(r,3); }
double Area (int r) { return 4*3.14* pow(r,2); }

And then use it like that

double v = Volume(r);
double a = Area(r);
thim
  • 585
  • 1
  • 3
  • 16
  • the problem is that the question says I have to use 1 function only – user1092492 Dec 11 '11 at 18:21
  • I see. Then I think that the solution given by Oli Charlesworth is good. You can also return an array (or a pointer to an array) of two doubles. But the one with struct is more clear to me ;) – thim Dec 11 '11 at 19:26
  • @user1092492: Please edit your question to include the "one function only" requirement. – trashgod Dec 12 '11 at 16:20
-1
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int r;

double calc_a(int r){
    return 4*3.14* pow(r,2);
}

double calc_v(int r){
    return (4/3) * 3.14 * pow(r,3);
}

int main()
{
  double a,v;  

  cout << "Enter the radius: ";
  cin >> r;

  a = calc_a(r);
  v = calc_v(r);

  cout << fixed << setprecision(2);    
  cout << "The area of a sphere of radius " << r << " is " << a << " and its ";
  cout << "volume is ";
  cout << v;
  cout << endl;

  return 0;
}
zed_0xff
  • 32,417
  • 7
  • 53
  • 72