I have looked through all of these and am relatively new to coding c++ and just don't know what I am missing. Any idea's?
My error occurs at line 45 "return pi * (Radius * Radius);" I am almost positive that the syntax for that line is correct but why am i getting compile errors.
#include <iostream>
#include <cstdlib>
using namespace std;
const double pi = 3.14159;
class Rectangle
{
protected:
float length, width;
public:
Rectangle(): length(0), width(0)
{
cout<<"Enter length: "; cin>>length;
cout<<"Enter width: "; cin>>width;
}
};
class Circle
{
protected:
float Radius;
public:
double radius;
Circle(): Radius(0)
{
cout<<"Enter Radius: "; cin>>Radius;
}
};
class Area : public Rectangle
{
public:
float getArea()
{
return length*width;
}
};
class Radius : public Circle
{
public:
float getRadius()
{
return pi * (Radius * Radius);
}
};
int main()
{
char choice;
for (int i = 0; i < 4; i++) //loop statement
{
cout << "Program to Find Area of a Square and Circle" << endl << //selection of which calculation to run
"Enter S for square square." << endl <<
"Enter C for circle." << endl <<
"Enter Q to Quit the program." << endl << endl <<
"Enter an option above: ";
cin >> choice;
switch(choice)
{
//Square option:
case 'S':
case 's': {
cout<<"Enter data for rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.getArea()<<" square\n\n";
break;}
//Circle option:
case 'C':
case 'c': {
cout<<"Enter data for circle to find radius.\n";
Radius c;
cout<<"Radius = "<<c.getRadius()<<" meter\n\n";
break;}
//Quit option:
case 'Q':
case 'q': {
cout << "Thank you for using Area Application" << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
break;}
//default option binds to a non-selected choice function:
default:
cout << choice << " is not a valid selection." << endl;
cout << "Select a valid shape choice: S or C" << endl << endl;
break;
}
}
cout << "Press enter to continue ..." << endl;
return EXIT_SUCCESS;
}
Thanks
David
Now I see you have a member in base class which has a name Radius which is the same as in derived class, this is what is causing an error. The solution is to qualify it with base class name:
change:
return pi * (Radius * Radius);
to:
return pi * (Circle::Radius * Circle::Radius);
this additonal: double radius; is probably from some testing - right?
[edit]
From design point of view the existance of class Radius : public Circle makes little sense, it should be fine to just use Circle to get its radious.
#include <iostream>
#include <cstdlib>
using namespace std;
const double pi = 3.14159;
class Rectangle
{
protected:
float length, width;
public:
Rectangle(): length(0), width(0)
{cout<<"Enter length: "; cin>>length;cout<<"Enter width: "; cin>>width;}
float getArea(){return length*width;}
};
class Circle
{
protected:
float Radius;
public:
Circle(): Radius(0) {cout<<"Enter Radius: "; cin>>Radius;}
float getRadius() {return pi * (Radius * Radius);}
};
int main()
{
char choice;
for (int i = 0; i < 4; i++) //loop statement
{
cout << "Program to Find Area of a Square and Circle" << endl << //selection of which calculation to run
"Enter S for square square." << endl <<
"Enter C for circle." << endl <<
"Enter Q to Quit the program." << endl << endl <<
"Enter an option above: ";
cin >> choice;
switch(choice)
{
//Square option:
case 'S':
case 's': {
cout<<"Enter data for rectangle to find area.\n";
Rectangle a;
cout<<"Area = "<<a.getArea()<<" square\n\n";
break;}
//Circle option:
case 'C':
case 'c': {
cout<<"Enter data for circle to find radius.\n";
Circle c;
cout<<"Radius = "<<c.getRadius()<<" meter\n\n";
break;}
//Quit option:
case 'Q':
case 'q': {
cout << "Thank you for using Area Application" << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
break;}
//default option binds to a non-selected choice function:
default:
cout << choice << " is not a valid selection." << endl;
cout << "Select a valid shape choice: S or C" << endl << endl;
break;
}
}
cout << "Press enter to continue ..." << endl;
return EXIT_SUCCESS;
}
Related
My program should terminate after 3 wrong attempts but mine would still proceed to the menu even if the attempts were wrong. I've tried using return 0, but I didn't know why it still not worked. Is there any way to fix my program?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string pin;
int attemptCount = 0;
while ( attemptCount < 3 )
{
cout << "Enter pin: " << endl;
cin >> pin;
if ( pin != "1234")
{
cout << "Pin is incorrect." << "\n" <<
endl;
attemptCount++;
}
else if ( attemptCount = 0 )
{
cout << "3 pins were unsuccessful.";
return 0;
}
else
{
cout << "Access granted." << endl;
break;
}
}
int a, b, c;
char choice;
cout<<"\n\n---Area of a polygon---\n[A] Triangle\n[B] Square\n[C] Rectangle\n[D] Exit \n";
cout<<"Enter choice: \n";
cin>>choice;
switch(choice){
case 'a':
case 'A':
int square_area, square_side;
float base, height, area1;
cout << "\nEnter length of base and height of Triangle: ";
cin >> base >> height;
area1 = (base * height) / 2;
cout << "Area of Triangle: " << area1 << endl;
break;
case 'b':
case 'B':
cout<<"\nEnter the side of the square: ";
cin >> square_side;
square_area = square_side * square_side;
cout << "Area of Square: " << square_area << endl;
break;
case 'c':
case 'C':
int length, width, area;
cout << "\nEnter the Length of Rectangle : ";
cin>>length;
cout << "\nEnter the Width of Rectangle : ";
cin>>width;
area = length * width;
cout << "\nArea of Rectangle : " << area;
break;
case 'd':
case 'D':
break;
;
}
return 0;
}
My program should terminate after 3 wrong attempts but mine would still proceed to the menu even if the attempts were wrong. I've tried using return 0, but I didn't know why it still not worked. Is there any way to fix my program?
first you have to do change this like that
if ( attemptCount == 2)
{
cout << "3 pins were unsuccessful.";
return 0;
}
else if ( pin != "1234" )
{
cout << "Pin is incorrect." << "\n" <<
endl;
attemptCount++;
}
what happening here you are checking first pin is correct or not if it goes there it increment the attemptCount and back to loop and you have to write your menu and all the statements in else condition and remove the break from it.
Like that
else
{
cout << "Access granted." << endl;
int a, b, c;
char choice;
cout<<"\n\n---Area of a polygon---\n[A] Triangle\n[B] Square\n[C] Rectangle\n[D] Exit \n";
cout<<"Enter choice: \n";
cin>>choice;
switch(choice){
case 'a':
case 'A':
int square_area, square_side;
float base, height, area1;
cout << "\nEnter length of base and height of Triangle: ";
cin >> base >> height;
area1 = (base * height) / 2;
cout << "Area of Triangle: " << area1 << endl;
break;
case 'b':
case 'B':
cout<<"\nEnter the side of the square: ";
cin >> square_side;
square_area = square_side * square_side;
cout << "Area of Square: " << square_area << endl;
break;
case 'c':
case 'C':
int length, width, area;
cout << "\nEnter the Length of Rectangle : ";
cin>>length;
cout << "\nEnter the Width of Rectangle : ";
cin>>width;
area = length * width;
cout << "\nArea of Rectangle : " << area;
break;
case 'd':
case 'D':
break;
}
}
Now your whole code look like this
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string pin;
int attemptCount = 0;
while ( attemptCount < 3 )
{
cout << "Enter pin: " << endl;
cin >> pin;
if ( attemptCount == 2)
{
cout << "3 pins were unsuccessful.";
return 0;
}
else if ( pin != "1234" )
{
cout << "Pin is incorrect." << "\n" <<
endl;
attemptCount++;
}
else
{
cout << "Access granted." << endl;
int a, b, c;
char choice;
cout<<"\n\n---Area of a polygon---\n[A] Triangle\n[B] Square\n[C] Rectangle\n[D] Exit \n";
cout<<"Enter choice: \n";
cin>>choice;
switch(choice){
case 'a':
case 'A':
int square_area, square_side;
float base, height, area1;
cout << "\nEnter length of base and height of Triangle: ";
cin >> base >> height;
area1 = (base * height) / 2;
cout << "Area of Triangle: " << area1 << endl;
break;
case 'b':
case 'B':
cout<<"\nEnter the side of the square: ";
cin >> square_side;
square_area = square_side * square_side;
cout << "Area of Square: " << square_area << endl;
break;
case 'c':
case 'C':
int length, width, area;
cout << "\nEnter the Length of Rectangle : ";
cin>>length;
cout << "\nEnter the Width of Rectangle : ";
cin>>width;
area = length * width;
cout << "\nArea of Rectangle : " << area;
break;
case 'd':
case 'D':
break;
}
}
}
return 0;
}
Consider what's happening as you step through your code. attemptCount is set to 0. Your while loop continues while attemptCount is less than 3. So far so good. We do enter the loop.
If the pin entered isn't "1234" then you print an error and increment attemptCount by 1.
If it is equal to "1234" and attemptCount is equal to 0 then it prints a message that "3 pins were unsuccessful." and then program ends because you've returned from main.
Otherwise, the pin was successful, and it prints "Access granted." and breaks out of the loop.
The only condition under which you return early and the code after the while loop in main isn't executed is if the PIN is successfully guessed on the first try.
(As a sidenote, do yourself a favor and neatly format your code. Code that's well-formatted is easier to reason about.)
//Hello everyone! so this is the revised code of my program, and it is now working properly!
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string pin;
int attemptCount = 0;
int x = 1;
while ( 1 )
{
cout << "Enter pin: " << endl;
cin >> pin;
if ( pin == "1234")
{
cout << "Access granted." << "\n";
break;
}
else if ( pin != "1234" )
{
cout << "The pin is incorrect."<<
endl;
++x;
}
if (x > 3)
{
cout << "3 attempts were unsuccessful." << endl;
system("pause");
return 0;
}
}
int a, b, c;
char choice;
cout<<"\n\n---Area of a polygon---\n[A] Triangle\n[B] Square\n[C] Rectangle\n[D] Exit \n";
cout<<"Enter choice: \n";
cin>>choice;
switch(choice){
case 'a':
case 'A':
int square_area, square_side;
float base, height, area1;
cout << "\nEnter length of base and height of Triangle: ";
cin >> base >> height;
area1 = (base * height) / 2;
cout << "Area of Triangle: " << area1 << endl;
break;
case 'b':
case 'B':
cout<<"\nEnter the side of the square: ";
cin >> square_side;
square_area = square_side * square_side;
cout << "Area of Square: " << square_area << endl;
break;
case 'c':
case 'C':
int length, width, area;
cout << "\nEnter the Length of Rectangle : ";
cin>>length;
cout << "\nEnter the Width of Rectangle : ";
cin>>width;
area = length * width;
cout << "\nArea of Rectangle : " << area;
break;
case 'd':
case 'D':
break;
;
}
return 0;
}
I am having trouble using a switch inside a do-while loop. menu is displaying, but after selection it is just displaying the menu again rather than opening up the proper switch case. and help would be greatly appreciated. I have tried looking for help and could not seem to find much.
#include <iostream>
using namespace std;
//function prototypes
int DisplayMenu(); //shows menu and returns input
double CalcAreaCircle(double radius ); //returns the area of the circle
double CalcAreaRectangle(double length, double width ); //returns the area of a rectangle
double CalcAreaTriangle(double base, double height ); //returns the area of a triangle
int Choice;
double AreaOfCircle;
double radius;
double AreaOfRectangle;
double length;
double width;
double AreaOfTriangle;
double base;
double height;
//function main
int main()
{
Choice = -1;
while (Choice != 4)
{
Choice = DisplayMenu();
switch (Choice)
{
case '1':
{
cout << "What is the radius of the circle?" << endl;
cin >> radius;
cout << endl;
AreaOfCircle = CalcAreaCircle(radius);
cout << endl << "The area of your circle is " << AreaOfCircle << endl;
break;
}
case '2':
{
cout << "what is the length of the rectangle?" << endl;
cin >> length;
cout << endl << "What is the width of the rectangle?" << endl;
cin >> width;
cout << endl;
AreaOfRectangle = CalcAreaRectangle(length, width);
cout << endl << "The area of your rectangle is " << AreaOfRectangle << endl;
break;
}
case '3':
{
cout << "What is the base of the triangle?" << endl;
cin >> base;
cout << endl << "What is the height of the triangle?" << endl;
cin >> height;
cout << endl;
AreaOfTriangle = CalcAreaTriangle(base, height);
cout << endl << "The area of your triangle is " << AreaOfTriangle << endl;
break;
}
}
}
system ("pause");
return 0;
}
//function DisplayMenu
int DisplayMenu()
{
int selection;
cout << "What would you like to know the area of?" << endl;
cout << "\t1. Area of a Circle." << endl;
cout << "\t2. Area of a Rectangle." << endl;
cout << "\t3. Area of a Triangle." << endl;
cout << "\t4. Quit." << endl;
cin >> selection;
while (selection < 1 || selection > 4)
{
cout << "Please enter a valid option." << endl;
cin >> selection;
cout << endl;
}
return selection;
}
//function CalcAreaCircle
double CalcAreaCircle(double radius)
{
double area;
const double PI = 3.14159;
area = PI * (area * area);
return area;
}
//function CalcAreaRectangle
double CalcAreaRectangle(double length, double width)
{
double area;
area = length * width;
return area;
}
//function CalcAreaTriangle
double CalcAreaTriangle(double base, double height)
{
double area;
area = base * height;
return area;
}
DisplayMenu() returns int. But your case statements are using char literals. When comparing char to int, it uses the character's code, e.g. case '1': is equivalent case 49:. Change your cases to use integer literals.
case 1:
and so on.
The following code asks the user to choose a shape, enter the dimensions for said shape, and display its volume.
It is used mostly of variable declarations and function calls, as this is required.
When I run the code I get the following output :
I dont understand why - nan (ind) appears instead of the result.
here is the full code:
#include <iostream>
using namespace std;
double height, width, length, radius, base_area, result;
//Function prototypes
int ReadInputShapeChoice();
void readshapedimension(int choice);
float CalculateBasicVolume(int choice);
void PrintResult(int choice);
double rectangular_solid(double length1, double width1, double height1);
double cylinder(double radius2, double height2);
double cone(double radius3, double height3);
double sphere(double radius4);
double square_based_pyramid(double height5, double base_area5);
//function definitions
double rectangular_solid(double length1, double width1, double height1)
{
double value;
value = (length1 * width1 * height1);
return value;
}
double cylinder(double radius2, double height2)
{
double value;
value = (3.14159 * (radius2 * radius2) * height2);
return value;
}
double cone(double radius3, double height3)
{
double value;
value = ((3.14159 * (radius3 * radius3) * height3) / 3);
return value;
}
double sphere(double radius4)
{
double value;
value = ((3.14159 * (radius4 * radius4 * radius4))*(4 / 3));
return value;
}
double square_based_pyramid(double height5, double base_area5)
{
double value;
value = ((height5 * base_area5) * (1 / 3));
return value;
}
int ReadInputShapeChoice()
{ int choice;
cout << "Choose what shape you want to calculate" << endl;
cout << "1 = Rectangular solid" << endl;
cout << "2 = Cylinder" << endl;
cout << "3 = Cone" << endl;
cout << "4 = Sphere" << endl;
cout << "5 = Square based pyramid" << endl;
cin >> choice;
return choice;
}
void readshapedimension(int choice)
{
switch (choice)
{
case 1:
{
int length, width, height;
cout << "You have chosen rectuangular solid" << endl;
cout << "Enter the values for length width and height" << endl;
cin >> length >> width >> height;
break;
}
case 2:
{
int radius, height;
cout << "You have chosen cylinder" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 3:
{
int radius, height;
cout << "You have chosen cone" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 4:
{
int radius;
cout << "You have chosen sphere" << endl;
cout << "Enter the radius" << endl;
cin >> radius;
break;
}
case 5:
{
int height, base_area;
cout << "You have chosen square based pyramid" << endl;
cout << "Enter height and area of the base" << endl;
cin >> height >> base_area;
break;
}
}
}
float CalculateBasicVolume(int choice)
{
switch (choice)
{
int result;
case 1:
{
result = rectangular_solid(length, width, height);
break;
}
case 2:
{
result = cylinder(radius, height);
break;
}
case 3:
{
result = cone(radius, height);
break;
}
case 4:
{
result = sphere(radius);
break;
}
case 5:
{
result = square_based_pyramid(height, base_area);
break;
}
return result;
}
}
void PrintResult(int choice)
{
switch (choice)
{
case 1:
{
cout << "The volume of the rectangular solid is " << result << endl;
break;
}
case 2:
{
cout << "the volume of the cylinder is " << result << endl;
break;
}
case 3:
{
cout << "The volume of the cone is " << result << endl;
break;
}
case 4:
{
cout << "The volume of the sphere is " << result << endl;
break;
}
case 5:
{
cout << "the volume of the square based pyramid is " << result << endl;
break;
}
}
}
int main() {
int choice;
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
PrintResult(choice);
return 0;
}
PLease can someone help me find a way to modify this code, so that it outputs the correct results ? Thankyou.
You're reading the radius and height into local variables declared in your switch statement. The global variables used in the calculation are never set, and have their default values of 0 in them. Your calculation results in a 0.0 / 0.0, which results in your NaN (Not a Number).
The following code asks the user to choose a shape, enter the dimensions for said shape, and display its volume.
When I run the code I get the following output, showing me the result is not a number (NaN) :
I realise this must have something to do with the fact my variables are local not global, and that the following function calls:
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
Are not passing the required data. Have tried to get around this and put the variables in the correct place but to no avail, I am a beginner at using any form of coding language and found the whole program fairly challenging to create.
I have posted a similar question but this is much more detailed, if anyone can shed any light on how I modify the location of my variables in order for the program to run correctly I would be very grateful. Thank you.
The full code is:
#include <iostream>
using namespace std;
double height, width, length, radius, base_area, result;
//Function prototypes
int ReadInputShapeChoice();
void readshapedimension(int choice);
float CalculateBasicVolume(int choice);
void PrintResult(int choice);
double rectangular_solid(double length1, double width1, double height1);
double cylinder(double radius2, double height2);
double cone(double radius3, double height3);
double sphere(double radius4);
double square_based_pyramid(double height5, double base_area5);
//function definitions
double rectangular_solid(double length1, double width1, double height1)
{
double value;
value = (length1 * width1 * height1);
return value;
}
double cylinder(double radius2, double height2)
{
double value;
value = (3.14159 * (radius2 * radius2) * height2);
return value;
}
double cone(double radius3, double height3)
{
double value;
value = ((3.14159 * (radius3 * radius3) * height3) / 3);
return value;
}
double sphere(double radius4)
{
double value;
value = ((3.14159 * (radius4 * radius4 * radius4))*(4 / 3));
return value;
}
double square_based_pyramid(double height5, double base_area5)
{
double value;
value = ((height5 * base_area5) * (1 / 3));
return value;
}
int ReadInputShapeChoice()
{ int choice;
cout << "Choose what shape you want to calculate" << endl;
cout << "1 = Rectangular solid" << endl;
cout << "2 = Cylinder" << endl;
cout << "3 = Cone" << endl;
cout << "4 = Sphere" << endl;
cout << "5 = Square based pyramid" << endl;
cin >> choice;
return choice;
}
void readshapedimension(int choice)
{
switch (choice)
{
case 1:
{
int length, width, height;
cout << "You have chosen rectuangular solid" << endl;
cout << "Enter the values for length width and height" << endl;
cin >> length >> width >> height;
break;
}
case 2:
{
int radius, height;
cout << "You have chosen cylinder" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 3:
{
int radius, height;
cout << "You have chosen cone" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 4:
{
int radius;
cout << "You have chosen sphere" << endl;
cout << "Enter the radius" << endl;
cin >> radius;
break;
}
case 5:
{
int height, base_area;
cout << "You have chosen square based pyramid" << endl;
cout << "Enter height and area of the base" << endl;
cin >> height >> base_area;
break;
}
}
}
float CalculateBasicVolume(int choice)
{
switch (choice)
{
int result;
case 1:
{
result = rectangular_solid(length, width, height);
break;
}
case 2:
{
result = cylinder(radius, height);
break;
}
case 3:
{
result = cone(radius, height);
break;
}
case 4:
{
result = sphere(radius);
break;
}
case 5:
{
result = square_based_pyramid(height, base_area);
break;
}
return result;
}
}
void PrintResult(int choice)
{
switch (choice)
{
case 1:
{
cout << "The volume of the rectangular solid is " << result << endl;
break;
}
case 2:
{
cout << "the volume of the cylinder is " << result << endl;
break;
}
case 3:
{
cout << "The volume of the cone is " << result << endl;
break;
}
case 4:
{
cout << "The volume of the sphere is " << result << endl;
break;
}
case 5:
{
cout << "the volume of the square based pyramid is " << result << endl;
break;
}
}
}
int main() {
int choice;
choice = ReadInputShapeChoice();
readshapedimension(choice);
result = CalculateBasicVolume(choice);
PrintResult(choice);
return 0;
}}
You are re-declaring within your functions; this causes the functions to use their local values rather than the globals, as an example I've commented out the re-declarations in your reshapedimensions function:
// This is our global declaration
double height, width, length, radius, base_area, result;
void readshapedimension(int choice)
{
switch (choice)
{
case 1:
{
// Take out our local declarations
// Otherwise cin below will write to the local and these values
// will subsequently be lost when the function exits
//int length, width, height;
cout << "You have chosen rectuangular solid" << endl;
cout << "Enter the values for length width and height" << endl;
cin >> length >> width >> height;
break;
}
case 2:
{
//int radius, height;
cout << "You have chosen cylinder" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 3:
{
//int radius, height;
cout << "You have chosen cone" << endl;
cout << "Enter the values for radius and height" << endl;
cin >> radius >> height;
break;
}
case 4:
{
//int radius;
cout << "You have chosen sphere" << endl;
cout << "Enter the radius" << endl;
cin >> radius;
break;
}
case 5:
{
//int height, base_area;
cout << "You have chosen square based pyramid" << endl;
cout << "Enter height and area of the base" << endl;
cin >> height >> base_area;
break;
}
}
}
I notice, however, that you are using int within the function and double in your global declaration; if this was intentional you would need to include a conversion somewhere and store the result back to the globals.
It's a good idea to keep your global names unique so you won't get them confused with locals; common techniques are to use ALLCAPS or a prefix such as glob_myvar.
You are redefining the result variable.
At the top of your file you declare it;
double result;
But you never initialize it, instead you redefine it later and put the value you want to print out in this new variable;
float CalculateBasicVolume(int choice)
{
switch (choice)
{
int result;
The variable you print later is the uninitialized double and not the int which actually contains the volume of the cone.
I have this code when executed it will let the user choose an option. once the user enters an option the program will be cleared and execute another program. below is the sample. at the bottom is another program of option 1
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"Please choose an option below: \n";
cout<<"1. Area of Shapes\n";
cout<<"2. Cost of your items\n";
cout<<"3. Flood Control\n";
cout<<"4. Fibonacci Numbers\n";
cout<<"5. Addition Table\n";
cout<<"6. Exit\n";
cin>> a;
system("pause");
return 0;
}
Here is the program for option 1:
#include <iostream>
using namespace std;
float circle (float a)
{
float z;
z = 3.141593*(a*a);
return (z);
}
float square (float b)
{
float y;
y = b * b;
return (y);
}
float rectangle (float c, float d)
{
float x;
x = c * d;
return (x);
}
float triangle (float e, float f)
{
float w;
w = (e * f) / 2;
return (w);
}
void exit ()
{
cout << "THANK YOU! GOODBYE!" <<endl;
}
int main()
{
float n;
float l;
float m;
float radius;
float side;
float length;
float width;
float base;
float height;
do
{
cout << "1 => Area of Circle" <<endl;
cout << "2 => Area of Square" <<endl;
cout << "3 => Area of Rectangle" <<endl;
cout << "4 => Area of Triangle" <<endl;
cout << "0 => Exit" <<endl;
cout << "Please enter number of your choice: ";
cin >> n;
{
if (n==0)
{
exit ();
system("pause");
return 0;
}
else if (n==1)
{
cout << "Enter radius of the circle: ";
cin >> radius;
l = circle (radius);
cout << "Area of the circle is: " <<l <<endl;
}
else if (n==2)
{
cout << "Enter side of the square: ";
cin >> side;
cout << "Area of the square is: " <<square (side) <<endl;
}
else if (n==3)
{
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter width of the rectangle: ";
cin >> width;
m = rectangle (length, width);
cout << "Area of the rectangle is: " <<m <<endl;
}
else if (n==4)
{
cout << "Enter base of the triangle: ";
cin >> base;
cout << "Enter height of the triangle: ";
cin >> height;
cout << "Area of the triangle is: " <<triangle (base, height) <<endl;
}
else
cout << "Invalid number. Please enter a valid number below" <<endl;
}
}
while (n!=0);
cout <<endl <<endl;
system("pause");
return 0;
}
If you truely want to replace the current program with another, look to the exec family of system calls.
Put each "program" in its own file (or not, but it's a good idea to keep them separate).
Rename each "program"'s main to something meaningful, like "areas".
Declare that function in a header.
Include that header in your "controller" program.
Call the corresponding "program function" from the "controller" based on the input you read.