Skylar
Skylar

Reputation: 11

Looping the formula for the area of a rectangle

I'm at my wits end on this assignment. I need the user to be able to do a few things.

  1. Enter which shape they want to hold sheep (by pushing 1 or 2).
  2. Enter how many corrals they want; 3 being the max.
  3. Output the area and number of sheep for each corral.

The circle formula works, but the rectangle pauses after you enter the number of corrals. The output for the rectangle section does not show up at all. I've tried a combination of using while, switch, case, and else, and I can't get it to work.

How do I get the formula section to appear, and how do I use "while" to loop the rectangle formula 3 times? Any help?

// ConsoleApplication16.cpp : Defines the entry point for the console    application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>

using namespace std; 

int main()
{
const double pie = 3.141592;
double r;
double area = 0;
double length;
double width;
int shape;
double sheep = 2.54;
int corral;

cout << " Which shape do you want? " << endl;
cout << " 1. Circle.   2. Rectangle. ";
cin >> shape;
cout << " How many corrals do you want? " << endl;
cin >> corral;

while (corral <= 3)

    if (shape == 1)
    {
        cout << "Input radius. " << endl;
        cin >> r;
        cout << fixed << setprecision(2);
        area = pie * pow(r, r);
        cout << endl;

        cout << " Here's the area. << " endl;
        cout << area << endl;
        cout << fixed << setprecision(1) << "Number of sheep " << area / sheep << endl;
    }


else (shape = 2);
        {   
        cout << "Enter lenght and width. " << endl;
        cin >> length;
        cin >> width;
        area = length * width;

        cout << "Here's the area. " << endl;
        cout << area << endl;
        cout << "Number of sheep" << endl;
        cout << fixed << setprecision(2);
        cout << area / sheep;
        }




return 0;

}

Upvotes: 0

Views: 920

Answers (2)

Nigloo
Nigloo

Reputation: 76

else (shape = 2);

I think wanted to write

else if (shape == 2)

Upvotes: 1

Marcia Ong
Marcia Ong

Reputation: 781

I would recommend you to first write a method that computes the area. Then use a loop to call the method the number of times 1,2 or 3.

Logic

1)Ask user for radius input

2)Call compute area method or just compute the area

btw you don't use "if else" like this (shape = 2);

if (condition) 
{}
else
{}

if you need more condition checks

if (condition){}
else if (condition) {}
else {}

Upvotes: 1

Related Questions