Question

My professor wants me to output the "area" from calculateArea as a char/string. I'm not sure exactly what he means, but maybe some of you might understand.

#include <iostream>
#include "math.h"
#include <cmath>
#include <sstream>
#include <string>

using namespace std;

const char& calculateArea(double diameter, double chord)
{   
    double length_1, length_2, angle; //This creates variables used by the formula.
    angle = acos( (0.5 * chord) / (0.5 * diameter) ); //This calculates the angle, theta, in radians.
    
    cout << "Angle: " << (angle * 180) / 3.14159 << "\n"; //This code displays the angle, currently in radians, in degrees.
    
    length_1 = (sin(angle)) * 6; //This finds the side of the triangle, x.
    
    cout << "X: " << length_1 << " inches "<< "\n"; //This code displays the length of 'x'.
    
    length_2 = (0.5 * diameter) - length_1; /*This code finds the length of 'h', by subtracting 'x' from the radius (which is half                                              the diameter).*/
    
    cout << "h: " << length_2 << " inches" << "\n"; //This code displays the length of 'h'.
    
    double area = ((2.0/3.0) * (chord * length_2)) + ( (pow(length_2, 3) / (2 * chord) ) ); /*This code calculates the area of the                                                                                                            slice.*/
    ostringstream oss;
    
    oss << "The area is: "<< area << " inches";
    
    string aStr = oss.str();
    
    cout << "Debug: "<< aStr.c_str() << "\n";
    
    const char *tStr = aStr.c_str();
    
    cout << "Debug: " << tStr << "\n";
    
    return *tStr;
    
    //This returns the area as a double.

}

int main(int argc, char *argv[]) {
    
    double dia, cho; //Variables to store the user's input.
    
    cout << "What is your diameter? ";  //
    cin >> dia;                          // This code asks the user to input the diameter & chord. The function will calculate
    cout << "What is your chord? ";      // the area of the slice.
    cin >> cho;                          //
    
    const char AreaPrint = calculateArea(dia, cho); //Sends the input to the function.
    
    cout << AreaPrint; //Prints out the area.
            
    return 0;
}

I get the output as this though:

What is your diameter? 12

What is your chord? 10

Angle: 33.5573

X: 3.31662 inches

h: 2.68338 inches

Debug: The area is: 18.8553 inches

Debug: The area is: 18.8553 inches

T

I need to figure out how to return the string tStr points to. If you don't get what I'm saying, sorry, not really sure what the professor is asking for.

Était-ce utile?

La solution

You are returing a char reference not a string. (the *tStr says 'give me the contents of the pointer')

A more correct version of what you are trying is:

const char* calculateArea(double diameter, double chord)

and

return tStr; // no 'content of'

But this is still bad: the string returned is "out of scope" when aStr goes out of scope, so you really need to return a copy of the characters or just return the string itself (and let the std lib worry about the copy for you)

const string calculateArea(double diameter, double chord)

...

return aStr;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top