Hi, I'm having a problem with this program.
When I choose an option, all the program does is output three random numbers with letters in them. Do any of you know what my problem is? Any help is appreciated.
Thanks for taking the time to read my program.
- bookmark.

#include <cmath>
#include <iostream>
#include <string>
using namespace std;

const double PI = 3.14159265358979323846;
double s;

bool die( const string & msg);
bool getDouble( double & value, const string & prompt );
char userChoice;
double circleCircumference( double radius );
double circleArea( double radius );
double trianglePerimeter( double side );
double triangleArea( double side );
double  squarePerimeter( double side );
double squareArea( double side );
double hexagonPerimeter( double side );
double hexagonArea( double side );

int main(){
	char j, c, C, t, T, h, H, q, Q;
	cout << "C: analyze circle" << endl
	<< "T: analyze equilateral triangle" << endl 
	<<  "S: analyze square" << endl
	<< "H: analyze regular hexagon" << endl
	<< "Q: quit the program" << endl;			
	cin >> j;
	cout << endl;

	while(cin>>j){
	if (j == 'c' || j == 'C'){
	cout << getDouble << endl; 
	cout << circleCircumference<< endl; 
	cout << circleArea << endl; 	
}
	else if( j == 't' || j == 'T'){
	cout << getDouble << endl;
	cout << trianglePerimeter << endl;
	cout << triangleArea << endl;
}
	else if(j == 's' || j == 'S'){
	cout << getDouble << endl;
	cout << squarePerimeter << endl;
	cout << squareArea << endl;
}
	else if(j =='h' || j == 'H'){
	cout << getDouble << endl;
	cout << hexagonPerimeter << endl;
	cout << hexagonArea << endl;
}
	else {
	exit(EXIT_SUCCESS);

	system("pause");
	}
}
}
	bool die(const string & message){
	cout << message << endl;
	exit(EXIT_FAILURE);	 
	}
bool getDouble( double & s, const string & prompt){
	cout << "What is the length of your side or radius?" << endl;
	if(cin >> s)   {
		 s = s;
		 return true;   }
	 else{
		 return false;}
}
double circleCircumference( double r){
	s = r;
	cin >> r;
	if( r < 0){
		bool die("radius is negative");
	}
	else {
	cout << "The circumference of the circle is " << 2 *  PI * r << endl;
	return r;
	}		
}
double circleArea( double r ){
	double A; 
	s = r;
	A = PI * (r*r);
	if( r < 0){
		bool die("radius is negative");
	}
	else  {
	cout << " The area of the circle is " <<  A << endl;
	return r;
}
}
double trianglePerimeter( double l){
	s = l;
	double tP;
	tP = 3*l;
	if(s<0){
		bool die("side is negative"); }
	else {
	cout << "The Perimeter of the triangle is " << tP << endl;}
	return tP;
}
double triangleArea( double m){
	s = m;
	double tA;
	tA = ((sqrt(3.0)) / 4 ) * m * m;
	if (s<0){
		bool die("side is negative") ;}
	else{ 
	cout << "The area of the triangle is " << tA << endl;
	return tA;
	}
}

double squarePerimeter( double n){
	s = n;
	double sP;
	sP = 4*n;
	if (n<0){
		bool die("side is negative");}
	else{ 
	cout << "The perimeter of the square is " << sP << endl;
	return sP;
	}
}
	double squareArea(double o){
	s = o;
	double sA;
	sA = o * o;
	if (o<0){
		bool die ("side is negative");}
	else{
	cout << "The perimeter of the square is " << sA << endl;
	}	  return sA;}
double hexagonPerimeter(double p){
	s = p;
	double hP;
	hP = 6 * p;
	if( p < 0){
		bool die("side is negative");}
	else {
	cout << "The perimeter of the hexagon is " << hP << endl;
	}	return hP;
}
double hexagonArea(double q){
	s = q;
	double hA;
	hA = 6 *  ((sqrt(3.0)) / 4 ) * q * q;
	if ( q < 0 ){
		bool die ( "side is negative");}
	else{
	cout << "The perimeter of the hexagon is " << hA << endl;
	}
	return hA;
}

Recommended Answers

All 2 Replies

Hi, I'm having a problem with this program.
When I choose an option, all the program does is output three random numbers with letters in them. Do any of you know what my problem is? Any help is appreciated.
Thanks for taking the time to read my program.
- bookmark.

#include <cmath>
#include <iostream>
#include <string>
using namespace std;

const double PI = 3.14159265358979323846;
double s;

bool die( const string & msg);
bool getDouble( double & value, const string & prompt );
char userChoice;
double circleCircumference( double radius );
double circleArea( double radius );
double trianglePerimeter( double side );
double triangleArea( double side );
double  squarePerimeter( double side );
double squareArea( double side );
double hexagonPerimeter( double side );
double hexagonArea( double side );

int main(){
	char j, c, C, t, T, h, H, q, Q;
	cout << "C: analyze circle" << endl
	<< "T: analyze equilateral triangle" << endl 
	<<  "S: analyze square" << endl
	<< "H: analyze regular hexagon" << endl
	<< "Q: quit the program" << endl;			
	cin >> j;
	cout << endl;

	while(cin>>j){
	if (j == 'c' || j == 'C'){
	cout << getDouble << endl; 
	cout << circleCircumference<< endl; 
	cout << circleArea << endl; 	
}
	else if( j == 't' || j == 'T'){
	cout << getDouble << endl;
	cout << trianglePerimeter << endl;
	cout << triangleArea << endl;
}
	else if(j == 's' || j == 'S'){
	cout << getDouble << endl;
	cout << squarePerimeter << endl;
	cout << squareArea << endl;
}
	else if(j =='h' || j == 'H'){
	cout << getDouble << endl;
	cout << hexagonPerimeter << endl;
	cout << hexagonArea << endl;
}
	else {
	exit(EXIT_SUCCESS);

	system("pause");
	}
}
}
	bool die(const string & message){
	cout << message << endl;
	exit(EXIT_FAILURE);	 
	}
bool getDouble( double & s, const string & prompt){
	cout << "What is the length of your side or radius?" << endl;
	if(cin >> s)   {
		 s = s;
		 return true;   }
	 else{
		 return false;}
}
double circleCircumference( double r){
	s = r;
	cin >> r;
	if( r < 0){
		bool die("radius is negative");
	}
	else {
	cout << "The circumference of the circle is " << 2 *  PI * r << endl;
	return r;
	}		
}
double circleArea( double r ){
	double A; 
	s = r;
	A = PI * (r*r);
	if( r < 0){
		bool die("radius is negative");
	}
	else  {
	cout << " The area of the circle is " <<  A << endl;
	return r;
}
}
double trianglePerimeter( double l){
	s = l;
	double tP;
	tP = 3*l;
	if(s<0){
		bool die("side is negative"); }
	else {
	cout << "The Perimeter of the triangle is " << tP << endl;}
	return tP;
}
double triangleArea( double m){
	s = m;
	double tA;
	tA = ((sqrt(3.0)) / 4 ) * m * m;
	if (s<0){
		bool die("side is negative") ;}
	else{ 
	cout << "The area of the triangle is " << tA << endl;
	return tA;
	}
}

double squarePerimeter( double n){
	s = n;
	double sP;
	sP = 4*n;
	if (n<0){
		bool die("side is negative");}
	else{ 
	cout << "The perimeter of the square is " << sP << endl;
	return sP;
	}
}
	double squareArea(double o){
	s = o;
	double sA;
	sA = o * o;
	if (o<0){
		bool die ("side is negative");}
	else{
	cout << "The perimeter of the square is " << sA << endl;
	}	  return sA;}
double hexagonPerimeter(double p){
	s = p;
	double hP;
	hP = 6 * p;
	if( p < 0){
		bool die("side is negative");}
	else {
	cout << "The perimeter of the hexagon is " << hP << endl;
	}	return hP;
}
double hexagonArea(double q){
	s = q;
	double hA;
	hA = 6 *  ((sqrt(3.0)) / 4 ) * q * q;
	if ( q < 0 ){
		bool die ( "side is negative");}
	else{
	cout << "The perimeter of the hexagon is " << hA << endl;
	}
	return hA;
}

You must provide arguments to your functions when you call them. When you write a function and you say it takes a double as a parameter, you need to give that function the double when you call it, or else the computer will make something up for you.

You should also move "double s" from the top (file scope) into your main function, and pass it as an argument (by reference). Else it defeats the purpose (if it's declared in file scope you can manipulate it anywhere, but it's not a safe or efficient thing to do, and it makes functions kind of pointless).

Remember to pass everything and keep track of where your information is going, so that each function gets the necessary information it needs. And remember that when you pass by value, you cannot alter that variable like that--if you need to alter it, you must pass by &reference.

There are a few things weird about your program--for example: char j, c, C, t, T, h, H, q, Q; I see how you need "j", but why the rest? You're confusing variables with values for variables.

if(cin >> s)   {
		 s = s;

This is also weird. What you're saying is that "If cin successfully gave s a value, then s = s" ??

You should never have to say "variable1 = variable1" ever, except maybe in some college proofs class.

I haven't gone over everything, there are other things that are wrong, such as certain things in your formula are out of order, but I believe that your code could work with a few minor changes here and there. What I suggest is that you test each portion of your code separately. Cout a value you want to test at the beginning and end of each function. And trace your double to make sure it's getting passed correctly.
-Greywolf

thanks for the help Greywolf. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.