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)
{
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!

4
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by ChaseVoid

You're using = in your if statements where you should be using ==

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 r;
cout << "\nEnter Radious of the circle = ";
cin >> r;
cout << "\nArea = " <<answer1;
}
int rectangle()
{
double w;
double h;
cout << "\nEnter the width of the rectangle = ";
cin >> w;
cout << "\nEnter the height of the rectangle = ";
cin >> h;