Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-21-2006, 07:20 PM   #1 (permalink)
dajohnson1s
Registered User
 
Join Date: Feb 2006
Posts: 1
dajohnson1s is on a distinguished road
help!!

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
Code:
#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);
}
areaOfTriange(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 I'm not really sure if this is how to let the user make a selection like this, and when i compile i get an error saying that t,r,c are undeclaired, could anybody please explain this?
dajohnson1s is offline   Reply With Quote
Old 02-21-2006, 09:28 PM   #2 (permalink)
Caleb.Carlton
Registered User
 
Join Date: Oct 2004
Posts: 7
Caleb.Carlton is on a distinguished road
T, R, and, C are undeclared because you never declared them.

Code:
char circle = 'c';
char rectangle = 'r';
char triangle = 't';
That is unnessisary and doesn't really do anything.

Instead of doing
Code:
if(variable == c)
{...
else if(variable == r)
{...
else if(variable == t)
{...
You could do this,
Code:
switch(variable)
{
case 'c':
cout << "\nEnter radius";
cin >> radius;
cout << "Area is: " << areaOFCircle(PI, radius);
cout << "\nPerimeter is: " << circumferenceOFCirle(PI, radius);
break;
case '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);
break;
case '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);
break;
default:
cout<<"You must enter C,R, or T\n";
break;
}
Before your program was looking something declared as "c" when you wrote "if(variable == c)" but your never declared c, you declared "circle", you could just change it to "if(variable == "c")" to fix your problems and the same thing for "R" and "T".
Caleb.Carlton is offline   Reply With Quote
Old 02-22-2006, 09:18 AM   #3 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
A switch would be cleaner. The minimum necessary to fix your code would be to surround c, r, and t in your if statements with single apostrophes.

As it is, you're attempting to compare the input to variables named c, r, and t. You want to compare it to the literal characters c, r, and t. The syntax for a literal character is to surround it with single apostrophes, like 'c'.

Alternately, you could compare the input to the variables you have declared (circle, rectangle, triangle). If you do that, you don't want to surround the variable name with any kind of quotes whatsoever.

And indent your code. Really. It makes finding simple errors (things like missing end braces) much easier to find, and can help you make sure you know what you're doing when tracking down logic issues at all. I know it probably seems silly right now. But, as an example:

Code:
if( a )
{
if( b )
{
if( c )
{
if( d )
{
if( e )
{
if( f )
{
}
}
}
}
}
isn't obvious. On the other hand,
Code:
if( a )
{
	if( b )
	{
		if( c )
		{
			if( d )
			{
				if( e )
				{
					if( f )
					{
					}
			}
		}
	}
}
Has an obvious missing end brace. If the lack of indention was because you re-typed your code instead of cut-n-paste, I apologize.
kyoryu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 12:39 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting