Crunch Posted September 21, 2004 Share Posted September 21, 2004 (edited) UPDATE #1: Semicolon Error STATUS: FIXED UPDATE #2: NEW ERROR WITH sqrt function STATUS: FIXED UPDATE #3: Adding Functions and trying to fix some warnings STATUS: FIXING UPDATE #4: How to implement the getAngles function void Triangle::getAngles() { blah = 1; float temp_A, temp_B, temp_C; temp_A = (side_a / side_c); temp_B = (side_b / side_c); //temp_C = (side_c / side_c); angle_A = sin(temp_A); angle_B = sin(temp_B); //angle_C = cout << "Angle A is " << angle_A << endl; cout << "Angle B is " << angle_B << endl; //cout << "Angle C is " << angle_C << endl; } Angle C is gonna always be my hypotenuse // Can I do this? Is this correct? How would you do it? --------------------------------------------------------------------------------------------- ok this is a program i am coding that isnt finished If you have any hints or things i messed please elaborate so I can fix CODE THUS FAR will update frequently with time i fixed Updated: (3:27 AM) EST #include <iostream> #include <cmath> using namespace std; void menu_a(); // Menu for system-defined Triangle void menu_b(); // Menu for user-designed Triangle class Triangle{ private: float side_a; float side_b; float side_c; float angle_A; float angle_B; float angle_C; public: float perimeter; float area; float s; int blah; Triangle(); Triangle(float x, float y, float z); void getSides(); void setSides(float x, float y, float z); void check(); void getArea(); void getAngles(); void getPerimeter(); }; Triangle::Triangle() { blah = 0; side_a = 9; side_b = 9; side_c = 9; } Triangle::Triangle(float x, float y, float z) { blah = 0; side_a = x; side_b = y; side_c = z; getSides(); } /* NOT STARTED TO WORK ON THIS FUNCTION */ void Triangle::check() { } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::getArea() { s = (side_a + side_b + side_c)/2; area = sqrt(s * (s - side_a) * (s - side_b) * (s - side_b)); cout << "=========================================" << endl; cout << "Area of the Triangle is " << area << endl; } /* NOT STARTED TO WORK ON THIS FUNCTION */ void Triangle::getAngles() { blah = 1; float temp_A, temp_B, temp_C; temp_A = (side_a / side_c); temp_B = (side_b / side_c); //temp_C = (side_c / side_c); angle_A = sin(temp_A); angle_B = sin(temp_B); //angle_C = cout << "Angle A is " << angle_A << endl; cout << "Angle B is " << angle_B << endl; //cout << "Angle C is " << angle_C << endl; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::getPerimeter() { perimeter = (side_a + side_b + side_c); cout << "=========================================" << endl; cout << "Perimeter of the Triangle is " << perimeter << endl; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::getSides() { cout << "=========================================" << endl; cout << "Side a = " << side_a << endl; cout << "Side b = " << side_b << endl; cout << "Side c = " << side_c << endl; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::setSides(float x, float y, float z) { side_a = x; side_b = y; side_c = z; getSides(); } int main() { int question_1 = 0; cout << " Welcome to Triangulate " << endl; cout << "--------------------------------------------" << endl; cout << "Note: Triangulate will set default values" << endl; cout << "to the triangle to a isoceles triangle with" << endl; cout << "the lengths of the sides all measuring 9" << endl; cout << "--------------------------------------------" << endl; do { cout << "Would you like to use this triangle or define your own?" << endl; cout << " (Please input 1 or 2 and Press ENTER}" << endl; cout << "1. Use the Predefined Triangle" << endl; cout << "2. Define your own Triangle" << endl; cin >> question_1; }while(question_1 < 1 || question_1 > 2); if (question_1 == 1) menu_a(); else if (question_1 == 2) menu_b(); else cout << "You caused an error. Please restart the program." << endl; return 0; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void menu_a() { Triangle a; int question_2 = 0; do { cout << "------------------------------------------" << endl; cout << "1. Get triangle measurements" << endl; cout << "2. Get Area of the triangle" << endl; cout << "3. Get the Perimeter" << endl; cout << "4. Get Angle measurements" << endl; if (a.blah == 1) cout << "5. Check for recognizable triangle shape" << endl; cout << "6. Make your own triangle" << endl; cout << "7. Exit Triangulate" << endl; cout << "Note: Number 5 can only be accessed after Number 4" << endl; cin >> question_2; if (question_2 == 1) a.getSides(); else if (question_2 == 2) a.getArea(); else if (question_2 == 3) a.getPerimeter(); else if (question_2 == 4) a.getAngles(); else if (question_2 == 5) { if (a.blah == 1) a.check(); } else if (question_2 == 6) menu_b(); }while(question_2 != 7); } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void menu_b() { Triangle b; int question_3 = 0; float side_x, side_y, side_z; cout << "Please input your triangle sides lengths" << endl; cout << "Enter side a: "; cin >> side_x; cout << "Enter side b: "; cin >> side_y; cout << "Enter side c: "; cin >> side_z; b.setSides(side_x, side_y, side_z); do { cout << "------------------------------------------" << endl; cout << "1. Get triangle measurements" << endl; cout << "2. Get Area of the triangle" << endl; cout << "3. Get the Perimeter" << endl; cout << "4. Get Angle measurements" << endl; if (b.blah == 1) cout << "5. Check for recognizable triangle shape" << endl; cout << "6. Make your own triangle" << endl; cout << "7. Exit Triangulate" << endl; cout << "Note: Number 5 can only be accessed after Number 4" << endl; cin >> question_3; if (question_3 == 1) b.getSides(); else if (question_3 == 2) b.getArea(); else if (question_3 == 3) b.getPerimeter(); else if (question_3 == 4) b.getAngles(); else if (question_3 == 5) { if (b.blah == 1) b.check(); } else if (question_3 == 6) { cout << "Please input your triangle sides lengths" << endl; cout << "Enter side a: "; cin >> side_x; cout << "Enter side b: "; cin >> side_y; cout << "Enter side c: "; cin >> side_z; b.setSides(side_x, side_y, side_z); } }while(question_3 != 7); } Edited September 21, 2004 by Crunch Link to comment Share on other sites More sharing options...
0 trance Posted September 21, 2004 Share Posted September 21, 2004 You didn't define a default constructor. You need to do that first. Also, can you paste in all the code so we can properly look at this? Link to comment Share on other sites More sharing options...
0 Crunch Posted September 21, 2004 Author Share Posted September 21, 2004 (edited) SEE FIRST POST Edited September 21, 2004 by Crunch Link to comment Share on other sites More sharing options...
0 gflores Posted September 21, 2004 Share Posted September 21, 2004 You need to have a semi-colon after the class declaration. class Triangle{ private: // stuff here public: // stuff here }; // <----------------------semicolon here! Also, you haven't implemented the function menu_b, yet. Link to comment Share on other sites More sharing options...
0 Crunch Posted September 21, 2004 Author Share Posted September 21, 2004 ahh thnx Link to comment Share on other sites More sharing options...
0 Crunch Posted September 21, 2004 Author Share Posted September 21, 2004 (edited) UPDATE #2: NEW ERROR WITH sqrt function ERROR c:\Documents and Settings\Jason\My Documents\Visual Studio Projects\Triangle ADT\Triangle ADT.cpp(64) : error C3861: 'sqrt': identifier not found, even with argument-dependent lookup STATUS: FIXED Edited September 21, 2004 by Crunch Link to comment Share on other sites More sharing options...
0 d3nuo Posted September 21, 2004 Share Posted September 21, 2004 UPDATE #2: NEW ERROR WITH sqrt functionERROR c:\Documents and Settings\Jason\My Documents\Visual Studio Projects\Triangle ADT\Triangle ADT.cpp(64) : error C3861: 'sqrt': identifier not found, even with argument-dependent lookup just gettin ready to post that!! Link to comment Share on other sites More sharing options...
0 Cubano Posted September 21, 2004 Share Posted September 21, 2004 Well, it seems you are trying to use the sqrt function without first declaring it. Either include math.h or declare your own sqrt function. Hope this helps. Link to comment Share on other sites More sharing options...
0 Crunch Posted September 21, 2004 Author Share Posted September 21, 2004 UPDATE #3: Adding Functions and trying to fix some warnings --------------------------------------------------------------------------------------------- If you have any hints or things i messed please elaborate so I can fix Link to comment Share on other sites More sharing options...
0 Crunch Posted September 21, 2004 Author Share Posted September 21, 2004 what file do I need to include to have the use of sin, cos, tan? Link to comment Share on other sites More sharing options...
0 justin89h Posted September 21, 2004 Share Posted September 21, 2004 what file do I need to include to have the use ofsin, cos, tan? include maths.h Has everything to do with maths :D Link to comment Share on other sites More sharing options...
0 Cubano Posted September 21, 2004 Share Posted September 21, 2004 like i stated above, include math.h. If you need to know what functions are available just open the math.h header file, and look at the declarations Link to comment Share on other sites More sharing options...
0 Crunch Posted September 21, 2004 Author Share Posted September 21, 2004 (edited) UPDATE #4: How to implement the getAngles function void Triangle::getAngles() { blah = 1; float temp_A, temp_B, temp_C; temp_A = (side_a / side_c); temp_B = (side_b / side_c); //temp_C = (side_c / side_c); angle_A = sin(temp_A); angle_B = sin(temp_A); //angle_C = cout << "Angle A is " << angle_A << endl; cout << "Angle B is " << angle_B << endl; //cout << "Angle C is " << angle_C << endl; } Angle C is gonna always be my hypotenuse // Can I do this? Is this correct? How would you do it? I get the correct values in radians how do I express in Degrees? Edited September 21, 2004 by Crunch Link to comment Share on other sites More sharing options...
0 caustiK Posted September 21, 2004 Share Posted September 21, 2004 i think you want to use arcsine instead of sine. sin x = a / b x = arcsin(a/b) also is this only for right triangles, because i think you might need some more complex trig for non-right ones. convert rads to degrees: multiply by (180/pi) one more note, you might consider having a function that simply computes the angles and then let getangles simply return those values. you would call this function whenever the triangle is changed - this is more efficient usually because getx() functions are usually called more than the values are changed. otherwise the program has to recalculate the angles everytime the client needs them. it's not much of an issue for something this simple, just something to think about. Link to comment Share on other sites More sharing options...
0 MrA Posted September 22, 2004 Share Posted September 22, 2004 Like caustiK said above, the trig function in the "normal" form only work for right angle triangles. For non-right-angle triangles, use the sine law and cosine law Link to comment Share on other sites More sharing options...
Question
Crunch
UPDATE #1: Semicolon Error
STATUS: FIXED
UPDATE #2: NEW ERROR WITH sqrt function
STATUS: FIXED
UPDATE #3: Adding Functions and trying to fix some warnings
STATUS: FIXING
UPDATE #4: How to implement the getAngles function
void Triangle::getAngles() { blah = 1; float temp_A, temp_B, temp_C; temp_A = (side_a / side_c); temp_B = (side_b / side_c); //temp_C = (side_c / side_c); angle_A = sin(temp_A); angle_B = sin(temp_B); //angle_C = cout << "Angle A is " << angle_A << endl; cout << "Angle B is " << angle_B << endl; //cout << "Angle C is " << angle_C << endl; }Angle C is gonna always be my hypotenuse // Can I do this?
Is this correct? How would you do it?
---------------------------------------------------------------------------------------------
ok this is a program i am coding that isnt finished
If you have any hints or things i messed please elaborate so I can fix
CODE THUS FAR
will update frequently with time i fixed
Updated: (3:27 AM) EST
#include <iostream> #include <cmath> using namespace std; void menu_a(); // Menu for system-defined Triangle void menu_b(); // Menu for user-designed Triangle class Triangle{ private: float side_a; float side_b; float side_c; float angle_A; float angle_B; float angle_C; public: float perimeter; float area; float s; int blah; Triangle(); Triangle(float x, float y, float z); void getSides(); void setSides(float x, float y, float z); void check(); void getArea(); void getAngles(); void getPerimeter(); }; Triangle::Triangle() { blah = 0; side_a = 9; side_b = 9; side_c = 9; } Triangle::Triangle(float x, float y, float z) { blah = 0; side_a = x; side_b = y; side_c = z; getSides(); } /* NOT STARTED TO WORK ON THIS FUNCTION */ void Triangle::check() { } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::getArea() { s = (side_a + side_b + side_c)/2; area = sqrt(s * (s - side_a) * (s - side_b) * (s - side_b)); cout << "=========================================" << endl; cout << "Area of the Triangle is " << area << endl; } /* NOT STARTED TO WORK ON THIS FUNCTION */ void Triangle::getAngles() { blah = 1; float temp_A, temp_B, temp_C; temp_A = (side_a / side_c); temp_B = (side_b / side_c); //temp_C = (side_c / side_c); angle_A = sin(temp_A); angle_B = sin(temp_B); //angle_C = cout << "Angle A is " << angle_A << endl; cout << "Angle B is " << angle_B << endl; //cout << "Angle C is " << angle_C << endl; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::getPerimeter() { perimeter = (side_a + side_b + side_c); cout << "=========================================" << endl; cout << "Perimeter of the Triangle is " << perimeter << endl; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::getSides() { cout << "=========================================" << endl; cout << "Side a = " << side_a << endl; cout << "Side b = " << side_b << endl; cout << "Side c = " << side_c << endl; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void Triangle::setSides(float x, float y, float z) { side_a = x; side_b = y; side_c = z; getSides(); } int main() { int question_1 = 0; cout << " Welcome to Triangulate " << endl; cout << "--------------------------------------------" << endl; cout << "Note: Triangulate will set default values" << endl; cout << "to the triangle to a isoceles triangle with" << endl; cout << "the lengths of the sides all measuring 9" << endl; cout << "--------------------------------------------" << endl; do { cout << "Would you like to use this triangle or define your own?" << endl; cout << " (Please input 1 or 2 and Press ENTER}" << endl; cout << "1. Use the Predefined Triangle" << endl; cout << "2. Define your own Triangle" << endl; cin >> question_1; }while(question_1 < 1 || question_1 > 2); if (question_1 == 1) menu_a(); else if (question_1 == 2) menu_b(); else cout << "You caused an error. Please restart the program." << endl; return 0; } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void menu_a() { Triangle a; int question_2 = 0; do { cout << "------------------------------------------" << endl; cout << "1. Get triangle measurements" << endl; cout << "2. Get Area of the triangle" << endl; cout << "3. Get the Perimeter" << endl; cout << "4. Get Angle measurements" << endl; if (a.blah == 1) cout << "5. Check for recognizable triangle shape" << endl; cout << "6. Make your own triangle" << endl; cout << "7. Exit Triangulate" << endl; cout << "Note: Number 5 can only be accessed after Number 4" << endl; cin >> question_2; if (question_2 == 1) a.getSides(); else if (question_2 == 2) a.getArea(); else if (question_2 == 3) a.getPerimeter(); else if (question_2 == 4) a.getAngles(); else if (question_2 == 5) { if (a.blah == 1) a.check(); } else if (question_2 == 6) menu_b(); }while(question_2 != 7); } /* FUNCTION IS COMPLETE UNLESS I IMPROVE THE CODE AND ADD FEATURES */ void menu_b() { Triangle b; int question_3 = 0; float side_x, side_y, side_z; cout << "Please input your triangle sides lengths" << endl; cout << "Enter side a: "; cin >> side_x; cout << "Enter side b: "; cin >> side_y; cout << "Enter side c: "; cin >> side_z; b.setSides(side_x, side_y, side_z); do { cout << "------------------------------------------" << endl; cout << "1. Get triangle measurements" << endl; cout << "2. Get Area of the triangle" << endl; cout << "3. Get the Perimeter" << endl; cout << "4. Get Angle measurements" << endl; if (b.blah == 1) cout << "5. Check for recognizable triangle shape" << endl; cout << "6. Make your own triangle" << endl; cout << "7. Exit Triangulate" << endl; cout << "Note: Number 5 can only be accessed after Number 4" << endl; cin >> question_3; if (question_3 == 1) b.getSides(); else if (question_3 == 2) b.getArea(); else if (question_3 == 3) b.getPerimeter(); else if (question_3 == 4) b.getAngles(); else if (question_3 == 5) { if (b.blah == 1) b.check(); } else if (question_3 == 6) { cout << "Please input your triangle sides lengths" << endl; cout << "Enter side a: "; cin >> side_x; cout << "Enter side b: "; cin >> side_y; cout << "Enter side c: "; cin >> side_z; b.setSides(side_x, side_y, side_z); } }while(question_3 != 7); }Edited by CrunchLink to comment
Share on other sites
14 answers to this question
Recommended Posts