| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with C++ Program
I just wanted to say that I am new to C++ and greatly accept any suggestions.
I have been working on this program for awhile and can't for the life of me figure out what is going wrong. Everybody else in my class is having the same issue as I am. What I am supposted to be doing is creating a library with functions that are given to me, I believe the .h file is correct and i can get the functions.cpp file to compile without errors, but when I compile the driver (spent hours trying to figure out) i get errors, out of frustration I think I have screwed up my program... this is the driver section #include <iostream> #include <cmath> using namespace std; #include "geometry.h" int main () { char circle = 'c'; char rectangle = 'r'; char triangle = 't'; char variable; double radius; double sideOne; double sideThree; double sideTwo; cout << "enter a variable to calculate area and perimeter (c = circle, r = rectangle, t = triangle) :"; cin >> variable; if(variable == c) { cout << "\nEnter radius"; cin >> radius; cout << "Area is: " << areaOFCircle(PI, radius); cout << "\nPerimeter is: " << circumferenceOFCirle(PI, radius); } else if(variable == r) { cout <<"\nEnter side one"; cin >> sideOne; cout << "\nEnter side two"; cin >> sideTwo; cout << "\nEnter side three"; cin >> sideThree; cout << "Area is: " << areaOfSquare(sideOne, sideTwo); cout << "\nPerimeter is: " << perimeterOfSquare(sideOne, sideTwo); } else if(variable == t) { cout <<"\nEnter side one"; cin >> sideOne; cout << "\nEnter side two"; cin >> sideTwo; cout << "\nEnter side three"; cin >> sideThree; cout << "Area is: " << perimeterOfTriangle(sideOne, sideTwo, sideThree); cout << "Perimeter is: " << areaOfTriange(sideOne, sideTwo, sideThree); } } Here are my functions: circumferenceOFCirle(double PI, double radius) { return(2 * PI * radius); } areaOFCircle(double PI, double radius) { return(PI * (pow(radius,2))); } /************************************************/ perimeterOfSquare(double sideOne, double sideTwo) { return((2 * sideOne) + (2 * sideTwo)); } areaOfSquare(double sideOne, double sideTwo) { return(sideOne * sideTwo); } /***********************************************/ perimeterOfTriangle(double sideOne, double sideTwo, double sideThree) { return(sideOne + sideTwo + sideThree); } areaOfTriangle(double sideOne, double sideTwo, double sideThree) { return(sqrt((( sideOne + sideTwo + sideThree)/2) * ((( sideOne + sideTwo + sideThree)/2) - sideOne) * (((sideOne + sideTwo + sideThree)/2) - sideTwo) * (((sideOne + sideTwo + sideThree)/2) - sideThree))); } any suggestions? also, when I compile this I get an error saying that c,t,r are undeclaired identifiers...could anybody clear this up for me? ![]() |
|
#2
|
|||
|
|||
|
Quote:
In the if , else if and else conditions instead of writing if (variable == t) write if(variable = 't') . Remember, t is is cahracter rather than a variable.If your remove quotes, error wil come. |
|
#3
|
||||
|
||||
|
I agree, c, t, and r are not variables in your program. circle, rectangle, and triangle are (with respective values, 'c', 't', and 'r'). So what I think your teacher wants to see (and I concur) is:
if( variable == circle ) .. if( variable == rectangle ) .. if( variable == triangle ) .. Good luck! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with C++ Program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|