Hi i am writing a program to calculate the area of a shape using functions. I got the program to work fine when there was only one function and one shape however when i added the second one the program will still run both functions. That is even when i type in 'R' it still wants to find the area of a circle? here is what i have so far..

#include <iostream>
#include <cmath>
using namespace std;
double circle(double r)
{
	double A;
	A=3.14159*r*r;
	return(A);
}
double rectangle(double w, double h)
{
	double A;
	A=w*h;
	return(A);
}
void main (void)
{
	char c1, c2, x;
	double r, A, w, l;
	c1='C';
	c2='R';
	cout<<"Enter a shape:"<<endl;
	cin>>x;
	if (x = c1)
		{
			cout<<"Enter a radius:"<<endl;
			cin>>r;
			A = circle(r);
			cout<<A;
		} 
	else(x = c2)
		{
		cout<<"Enter a width:"<<endl;
		cin>>w;
		cout<<"Enter a lenght:"<<endl;
		cin>>l;
		A = rectangle(w, l);
		cout<<A;
	}
}

Thank you in advance for trying to help me!

Recommended Answers

All 4 Replies

awesome your right! i stared at that for an hour and couldnt figure it out.. thanks for your help!

Well if i had to do that hw i would do it this was

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

int circle();
int rectangle();
int getchoice();

int main()
{
int choice;
do{
    choice = getchoice();
    if ( choice == 1)circle();
    else if ( choice == 2)rectangle();
   }
   while (choice != 3);
}

int getchoice()
{
    int x;
    cout << "\n\nPress 1 to find the area of a circle" << endl;
    cout << "Press 2 to find the area of a rectangle" << endl;
    cout << "Press 3 to quit\n" << endl;
    cin >> x;
    return x;
}

int circle()
{
	double answer1;
	double r;
	cout << "\nEnter Radious of the circle = ";
    cin >> r;
    answer1=3.14159*r*r;
    cout << "\nArea = " <<answer1;
}
int rectangle()
{
	double answer2;
	double w;
	double h;
	cout << "\nEnter the width of the rectangle = ";
    cin >> w;
    cout << "\nEnter the height of the rectangle = ";
    cin >> h;
    answer2=w*h;
    cout << "\nArea = " << answer2;
}

Hope this helps
and one more thing u should specify if its in the number for example is i in centimeters

Well you code has errors since, you are declaring circle() and rectangle() with the return type integer, so you must specify a return inside it. or you can just declare them as void.

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.