Question:
Need help with a C++ Program?
2013-04-10 13:48:17 UTC
Okay so i have to write this program for my C++ class. Apparently this assignment is the exact same for several people. I'm having difficulties calling my void calc function into my main function, or instead if i could pull the variables: s and area from the calc function, and into the main. I've been trying to read the book but my brain feels fried, so it would be much appreciated if someone could assist me with this. Also "s" and "area" have to be to 3 decimal places. im not sure if i need to use "int" or "double"
or do i need floating points.
Here is me code. (yes i know its very sloppy and has no //explanations-steps)

#include
#include
using namespace std;

int main()
{
int a, b, c;

cout << "Enter the sides of the triangle ";
cout << "and i will calculate the perimeter and area: ";
cin >> a >> b >> c;

calc(int a,int b,int c, int &s,int &area);

return 0;

}

void calc(int a,int b,int c, int &s,int &area)
{
s = ( a + b + c )/2;
area = sqrt( s*(s-a)*(s-b)*(s-c) );
cout << "The perimeter is: " << s;
cout << "The area is: " << area;
}
Three answers:
The Shadowman
2013-04-10 14:29:55 UTC
Here's the first problem:



calc(int a,int b,int c, int &s,int &area);



Not sure what was your intention here, were you trying to declare the function or calling the function? Or Both? If you are trying to declare the function, you have to put it between main() and "using namespace std".



using namespace std;

void calc(int a, int b, int c, int &s, int &area);

int main()



If you are trying to call the function, you don't specify the data type (int, double, float, string, etc). Another problem are these 2 variables, "s" and "area" in the parenthesis:



calc(int a,int b,int c, int &s,int &area);



"s" and "area" is declared within the function calc(), but it is not declare in main(). You cannot used a variable that was declare within a function and expect to used it in a different function, unless it is global. So since the calc() function prints out the parameter and area, you don't need to pass in "s" and "area" as arguments to the calc() function, also delete the data types:



calc(a, b, c);

return 0;



So now, you can safely delete the parameters "s" and "area" here:



void calc(int a,int b,int c)

{



Next, you declare "s" and "area" inside calc():



void calc(int a,int b,int c)

{

double s = blah blah

double area = blah blah

.......

......

}



Now to put the answers in 3 decimal places, you need to include the iomanip library:

#include



And then for your cout statements, do this:



cout << "The perimeter is: " << setprecision(3) << s;

cout << "The area is: " << setprecision(3) << area;



Edit: Change a, b, and c to double in order to fully output a decimal number.
?
2013-04-10 21:28:23 UTC
The ideal solution would be to have one function that calculates the perimeter of the triangle and another which computes the area.



This is much better:



// Computes the area of a triangle using Heron's formula

float area(float a, float b, float c)

{

float answer = sqrt(s * (s - a) * (s - b) * (s - c));

return answer;

}



// Computes the perimeter of a triangle

float perimeter(float a, float b, float c)

{

return (a + b + c);

}



You don't even really need a function for the second part unless the assignment demands you use functions to carry out both tasks. You must use either float or double when decimals will be involved. The type of the variables in your main function should also be changed to float.



If you are using a fairly modern compiler, math.h is incorrect. For newer tools, use

#include



To change how many digits are shown, use this:

http://www.cplusplus.com/reference/iomanip/setprecision/
darkshadowz1221
2013-04-10 21:23:00 UTC
//it seems to work now



#include

#include

using namespace std;



//you originally had this part in main, but it should be up here.

void calc(int a,int b,int c, int &s,int &area);

//

int main()

{

int a, b, c;

int s, area ;

cout << "Enter the sides of the triangle ";

cout << "and i will calculate the perimeter and area: ";

cin >> a >> b >> c;



calc(a,b,c,s,area);



cout << "The perimeter is: " << s << endl;

cout << "The area is: " << area << endl;



return 0;



}



void calc(int a,int b,int c, int &s,int &area)

{

s = ( a + b + c )/2;

area = sqrt( s*(s-a)*(s-b)*(s-c) );

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...