So, I wrote my first program, and it worked fine. Go me. Then, I went back, and had to use functions, so I just turned it into 3 very simple (or so I thought) functions. But I did something wrong, because now when I run the program, I just get nothing...or the process returned 0. Can anyone show me where I went wrong? I'm running out of time. This is due now in one hour, and I thought I was done.
Thanks. (Also, one other question: how do I cut and paste my programs to have color so they're easier to read?)
//This is a program that calculates the area of
//a geometric shape.
#include <iostream> //Including prototypes
#include <cmath>
#include <iomanip>
using namespace std;
int shape, radius, length, width, base, height; //Declaring variables
float pi = 3.14159;
void displayMenu(); //Declaring function prototypes
void readShape();
void calculateArea();
void displayMenu() //Function definition 1
{
cout << "Geometry Calculator" << endl //Displays menu
<< "1. Calculate the Area of a Cirlce" << endl
<< "2. Calculate the Area of a Rectangle" << endl
<< "3. Calculate the Area of a Triangle" << endl
<< "4. Quit" << endl
<< "Enter your choice (1-4):" << endl;
}
void readShape() //Function definition 2
{
cin >> shape; //Read shape
}
void calculateArea() //Function definition 3
{
if (shape == 1) //If shape is equal to 1
{
cout << "What is the radius of the circle? (Then press Enter key.)" << endl; //Ask for radius
cin >> radius; //Read radius
double areaOfCircle = pow(radius,2) * pi; //Set variable to PI * radius squared
cout << "The area of a circle with a radius of " <<radius <<" is " << endl
<< fixed<< setprecision(2) << areaOfCircle; //Tells you what your area is
}
else
{
if (shape == 2) //If shape is equal to 2
{
cout << "Please enter the length of the rectangle and press the Enter key." << endl; //Ask for length
cin >> length; //Read length
cout << "Please enter the width of the rectangle and press the Enter key." << endl; //Ask for width
cin >> width; //Read width
float areaOfRectangle = length * width; //Set variable to length * width
cout << "The area of a rectangle with a length of " << length
<< " and a width of " << width << " is " << endl
<< areaOfRectangle; //Tells you what your area is
}
else
{
if (shape == 3) //If shape is equal to 3
{
cout << "Please enter the triangle's base and press the Enter key." << endl; //Ask for base
cin >> base; //Read base
cout << "Please enter the triangle's height and press the Enter key." << endl; //Ask for height
cin >> height; //Read height
cout << "The area of a triangle with a base of " << base
<< " and a height of " << height << " is " << endl;
float areaOfTriangle = base * height * .5; //Sets variable to base * height * .5
cout << areaOfTriangle << endl; //Tells you what your area is
}
else
{
cout << "This program will now terminate." << endl; //If shape isn't 1-3, it must be 4, so the program ends.
Thank you! That took care of it. Hmmm, in my teacher's notes, though, he did add those voids. So, do you ever put those up there, or was that just a mistake he made while lecturing?
Oh, and one other thing I forgot to ask: He hasn't gone over it yet, but we are supposed to email him our source code and the output file. That means that I have to add code to create an output file, correct? Then, would I have to run the program for each menu option to show him that the program works? If I do that, will the output file just keep appending for each time I run the program? How exactly does one do that?
When calling a function, you don't precede it with it's type, otherwise the program will think that you are declaring function prototypes.
As for your second post, if your teacher asked you to email him the source code, then do it. I don't think you should email him the output file, as he can test it using his compiler.
Thank you for clearing that up.
As for the teacher, I do have to email him an output file, because he told us that AND the source code are the requirements. Maybe he's not planning on running the code on his own computer? Too bad, because I'd rather not have to do it, but I am looking in the book to see how to do that now.