Hi everyone I desperately need help with an assignment to calculate the area of shapes using functions. PLEASE be patient as I am very new and I am doing my best and can't seem to figure it out. I just need a helping hand in the right direction! Here is my code and again I'm new and I know it's a mess..
#include<iostream>
#include<iomanip>
using namespace std;
//function prototypes
float c(float);
int s(int);
int r(int);
void showWelcome();
void showMenu();
//main function
int main()
{
//declare variables as int. //menu choice from user
int x;
int choice;
int a,r,l,w;
float area;
const double PI = 3.14;
const int circle=1,
rectangle=2,
square=3,
quit=4;
cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
do
{
showWelcome(); // Show Welcome screen
showMenu(); // Display Menu
cin >> choice;
//Validate menu selection
while (choice < circle || choice > quit)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
//If user does not want to quit, proceed.
if (choice != quit)
{
switch (choice)
{
case circle:
cout << "Please enter the radius. ";
cin >> r;
float c;
break;
case square:
cout << " Please enter the base. " ;
int s;
break;
case rectangle:
cout << " Please enter the length. " ;
int r;
break;
}
}
} while (choice != quit);
return 0;
}
//Welcome Function
void showWelcome()
{
cout << "Welcome to calculating the area of shapes!" << endl << endl;
system ("pause");
}
//Menu Function
#include <iostream>
double area_of_circle(double radius);
int main()
{
double r;
std::cout << "Enter the radius: ";
std::cin >> r;
double area = area_of_circle(r);
std::cout << "The area of a circle with radius " << r << " is " << area << ".\n";
}
double area_of_circle(double radius)
{
constdouble pi = 3.14159;
return radius * radius * pi;
}
Line 47: What is the purpose of declaring variable c? Note that you also have a function by this name. Naming functions and variables the same is a poor practice. Very confusing to the reader. Use more meaningful names for your functions.
What I think you want here is:
1 2 3 4 5 6
case circle:
cout << "Please enter the radius. ";
cin >> r;
cout << "The area of the circle is " << circle_area (r) << endl;
break;
Your other cases would be similar.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thank you! Yeah sorry if it posted weird I was in a bit of a panic and posted on my phone, I should be on a computer soon to test these out. Thanks again
#include<iostream>
#include<iomanip>
usingnamespace std;
//function prototypes
double area_of_circle(double radius);
double area_of_square(double base);
double area_of_rectangle(double length);
void showWelcome();
void showMenu();
//main function
int main()
{
//declare variables as int. //menu choice from user
int choice;
int a,r,l,b;
constdouble PI = 3.14;
constint circle=1,
rectangle=2,
square=3,
quit=4;
cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
do
{
showWelcome(); // Show Welcome screen
showMenu(); // Display Menu
cin >> choice;
//Validate menu selection
while (choice < circle || choice > quit)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
//If user does not want to quit, proceed.
if (choice != quit)
{
switch (choice)
{
case circle:
double r;
cout << "Enter the radius ";
cin >> r;
double area = area_of_circle(r);
cout << "The area of the circle with radius " << r << " is " << area << " . \n";
break;
case square:
double b;
cout << " Enter the base ";
cin >> b;
double area = area_of_square(b);
cout << "The area of the square with base " << b << " is " << area << " . \n";
break;
case rectangle:
double l;
cout << " Please enter the length. " ;
cin >> l;
double area = area_of_rectangle(l);
break;
}
}
} while (choice != quit);
return 0;
}
//Welcome Function
void showWelcome()
{
cout << "Welcome to calculating the area of shapes!" << endl << endl;
system ("pause");
}
//Menu Function
void showMenu()
{
cout << "Please choose a shape to calculate the area " << endl << endl
<< "1. Circle" << endl
<< "2. Square" << endl
<< "3. Rectangle" << endl
<< "4. Quit"<< endl << endl;
}
double area_of_circle(double radius)
{
constdouble pi = 3.14;
return radius * radius * pi;
}
double area_of_square(double base)
{
return base * base
}
double area_of_rectangle(double length)
{
return l * l
}
You have been asked to use code tags. PLEASE DO SO. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I will not respond further until you apply code tags.
line 46, 53, 60: You're creating multiple defintions of area. A case branch does not create a new scope.
You have two choices.
1) Move the declaration of area outside the switch statement. Line 46 then becomes:
area = area_of_circle(r);
2) You can create a local scope for each case branch by using {}.
1 2 3 4 5 6 7 8
case circle:
{ double r;
cout << "Enter the radius ";
cin >> r;
double area = area_of_circle(r);
cout << "The area of the circle with radius " << r << " is " << area << " . \n";
}
break;
Lines 91 and 95 are missing ;
Line 95: l is not defined. Your argument name is length.
Two things.. It compiles fine now, which is great, but any idea as to why it would crash when square or rectangle is selected as a choice? Also, part two of this assignment is after it's up and running to then convert the code to have the variables "pass by reference." Honestly, I get the gist, but I've come this far and screwing something up this close to class lol, would the variables basically look like for example?
#include<iostream>
#include<iomanip>
usingnamespace std;
//function prototypes
double area_of_circle(double radius);
double area_of_square(double base);
double area_of_rectangle(double length);
void showWelcome();
void showMenu();
//main function
int main()
{
//declare variables as int. //menu choice from user
int choice;
constdouble PI = 3.14;
constint circle=1,
rectangle=2,
square=3,
quit=4;
cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
do
{
showWelcome(); // Show Welcome screen
showMenu(); // Display Menu
cin >> choice;
//Validate menu selection
while (choice < circle || choice > quit)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
//If user does not want to quit, proceed.
if (choice != quit)
{
switch (choice)
{
case circle:
double r;
cout << "Enter the radius ";
cin >> r;
double area = area_of_circle(r);
cout << "The area of the circle with radius " << r << " is " << area << " . \n";
system("pause");
}
}
break;
switch (choice)
{
case square:
double b;
cout << " Enter the base ";
cin >> b;
double area = area_of_square(b);
cout << "The area of the square with base " << b << " is " << area << " . \n";
system("pause");
}
break;
switch (choice)
{
case rectangle:
double l;
cout << " Please enter the length. " ;
cin >> l;
cout << " Please enter the width. " ;
double area = area_of_rectangle(l);
cout << "The area of the rectangle with length" << l << "is " << area << " . \n";
system("pause");
break;
}
}
while (choice != quit);
return 0;
}
//Welcome Function
void showWelcome()
{
cout << "Welcome to calculating the area of shapes!" << endl << endl;
system ("pause");
}
//Menu Function
void showMenu()
{
cout << "Please choose a shape to calculate the area " << endl << endl
<< "1. Circle" << endl
<< "2. Square" << endl
<< "3. Rectangle" << endl
<< "4. Quit"<< endl << endl;
}
double area_of_circle(double radius)
{
constdouble pi = 3.14;
return radius * radius * pi;
}
double area_of_square(double base)
{
return base * base;
}
double area_of_rectangle(double length)
{
return length * length;
}
switch (choice)
{
case circle:
{ double r;
cout << "Enter the radius ";
cin >> r;
double area = area_of_circle(r);
cout << "The area of the circle with radius " << r << " is " << area << " . \n";
system("pause");
break;
}
case square:
{ double b;
cout << " Enter the base ";
cin >> b;
double area = area_of_square(b);
cout << "The area of the square with base " << b << " is " << area << " . \n";
system("pause");
break;
}
case rectangle:
{ double l;
cout << " Please enter the length. " ;
cin >> l;
cout << " Please enter the width. " ;
double area = area_of_rectangle(l);
cout << "The area of the rectangle with length" << l << "is " << area << " . \n";
system("pause");
break;
}
}
Note that area_of_rectangle still only accepts one argument. rectangles have length and width. You prompt for width at line 68, but never input it or pass it to your area_of_rectangle function.
would the variables basically look like for example?
To correct your example:
double area_of_circle(constdouble & radius)
However, passing a simple variable (radius) by reference is somewhat pointless if you're not going to modify it. A better example would be: