Мой профессор хочет, чтобы я вывел «область» из вычисленияArea как символ/строку. Я не уверен, что он имеет в виду, но, возможно, некоторые из вас могут понять.Возврат потока строк (char *)
#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;
}
я получаю выход, как это, хотя:
Что ваш диаметр? 12
Каков ваш аккорд? 10
Угол обзор: 33,5573
X: 3.31662 дюйма
ч: 2.68338 дюйма
Debug: Район: 18.8553 дюймов
Debug: Район: 18.8553 дюймов
T
Мне нужно выяснить, как вернуть строки tStr. Если вы не получите то, что я говорю, извините, не совсем уверен, что просит профессор.
Почему бы не просто вернуть саму строку? –
Вы уверены, что он просит вернуть строку? Думаю, я хотел бы вернуть «двойную» функцию и напечатать, например. в функции 'main()'. –
Да, я решил, как это сделать (удвоить и вернуть строку). Возвращая строку и все, но когда он назначил ее, было что-то о const char *. Итак, я предполагаю, что он хотел чего-то подобного. –