Hi

i did the simple part of my project and got it to run but now i have to add in prototypes. can someone please help

bool validRadius(double);
bool validLengthAndWidth(double, double);
bool validBaseAnd Height(double, double);

I will also create functions which will calculate the area of the various geometric objects. These functions will be called from within each of the valid data cases in your switch-case selection construct. In each of the different cases, validate the data first and call the function if the data is in fact valid. If called, each of the functions will then calculate their respective areas and then return the value of its calculation to be printed within the case ( or write another function to do the printing.).

Use the following prototypes:

double areaCircle(double);
double areaRectangle(double, double);
double areaTriangle( double, double);


my programs

#include <iostream>
#include <iomanip>
#include <cmath>     // needed for pow function
using namespace std;

int main()
{
    int choice;      //menu choice
    double PI= 3.14159;
    double area, radius, base, num1, length, height, width;
    
    while (choice != 4)
    {
    cout << "\t\tGeometry Calculator Menu\n\n";
    cout << "1. Calculate the Area of a Circle\n";
    cout << "2. Calculate the Area of a Rectangle\n";
    cout << "3. Calculate the Area of a Triangle\n";
    cout << "4. Quit the Program\n\n";
    cout << "Enter your choice (1-4): ";
    cin >> choice;

    switch (choice)
    {
           case 1:
                  cout << "Enter the radius: ";
                  cin >> radius;
                  area = 3.14159 * pow(radius, 2.0);
                  cout << "Area = " << area << "\tsquare units" << endl;
                  break;
          
           case 2:
                do
                {
                    cout << "Enter length: ";
                    cin >> length;
                    while (length < 0 )
                    {
                        cout << "Please enter a positive length value: " << endl;
                        cin >> length;
                    }
                    cout << "Enter width: ";
                    cin >> width;
                   while (width < 0 )
                   {
                        cout << "Please enter a positive width value: " << endl;
                        cin >> width;
                   }
                   }
                   while ( length < 0 || width < 0 );
                   area = length * width;
                   cout << "Area of rectangle is: " << area << endl;
                  break;
           
           case 3:
                   do
                {
                    cout << "Enter base: ";
                    cin >> base;
                    while (base < 0 )
                    {
                        cout << "Please enter a positive base value: " << endl;
                        cin >> base;
                    }
                    cout << "Enter height: ";
                    cin >> height;
                   while (width < 0 )
                   {
                        cout << "Please enter a positive height value: " << endl;
                        cin >> height;
                   }
                   }
                   while ( height < 0 || base < 0 );
                   area = base * height * .5;
                   cout << "Area of triangle is: " << area << endl;
                  break;
           
           case 4:
                  void exit (int exitcode);
                  break;
                
           default:
                  cout << "**Error: Menu option must be 1, 2, 3 or 4." << endl;
                  cout << "Please try again." << endl;
                  break;

    }
      
}

      system("pause");
      return 0;
}

Recommended Answers

All 6 Replies

#include<iostream>

bool validRadius(double);  //function prototype


int main()
{
   
    double radius = -1;
    while(!
              validRadius(radius) //function call
            )
     {
        cout << "Enter radius" << '\n';
        cin >> radius;
      }
      cout << radius;
}

//function definition
bool validRadius(double r)
{
    bool result;  //declare return variable

    //determine value of return variable
    if(r < 0 || r > 35000)
       result = false;
    else 
       result = true;
     
     return result;
}

In this example the spacing is intentionally exaggerated to more clearly indicate where the function call is. Otherwise, that spacing is atrocious.

Why are you defining your own PI when cmath comes with M_PI which is alot more accurate than yours?

Chris

Why are you defining your own PI when cmath comes with M_PI which is alot more accurate than yours?

Maybe she just heard about it first time from u :D

Alas, M_PI constant name is not defined in the current C++ standard. For example, VC++ defines it (in <math.h> header - <cmath> is a wrapper for it) only if _USE_MATH_DEFINES macros is defined at the moment of <cmath> compilation.
Well, define your own pi if you wish:

#if !defined(M_PI)
#define M_PI 3.14159265358979323846
#endif

Why are you defining your own PI when cmath comes with M_PI which is alot more accurate than yours?

Chris

i am using what i learned in class. its just a intro course.

#include<iostream>

bool validRadius(double);  //function prototype


int main()
{
   
    double radius = -1;
    while(!
              validRadius(radius) //function call
            )
     {
        cout << "Enter radius" << '\n';
        cin >> radius;
      }
      cout << radius;
}

//function definition
bool validRadius(double r)
{
    bool result;  //declare return variable

    //determine value of return variable
    if(r < 0 || r > 35000)
       result = false;
    else 
       result = true;
     
     return result;
}

In this example the spacing is intentionally exaggerated to more clearly indicate where the function call is. Otherwise, that spacing is atrocious.

:'(

this is a new language to me so are you saying to replace

case 1:

cout << "Enter the radius: ";

cin >> radius;

area = 3.14159 * pow(radius, 2.0);

cout << "Area = " << area << "\tsquare units" << endl;

break;

with what you wrote on top? im sorry if this is a silly question. thanks

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.