geometry calculator,part1 and 2

Jul 12, 2016 at 7:57am
Part #1:
Write a program which displays the following menu:
Geometry Calculator:
1. Calculate the area of a circle
2. Calculate the area of a rectangle
3. Calculate the area of a triangle
4. Quit

If the user enters 1, have the user enter the radius of a circle, then calculate the circle's area.
If the user enters 2, have the user enter the length and width of a rectangle, then calculate its area.
If the user enters 3, have the user enter the length of the triangle's base and its height, and calculate its area.
If the user enters 4, have Part 1 of the program end.

Validations: the user must enter a valid number from the menu, and all other entries must be greater than zero.

Display the area of the chosen shape after the user selects a choice (areas should be displayed to 3 decimal points).

Part #2:
After coding part #1, code your program so the user enters your full name, including the appropriate spaces. Then have the user enter your height in feet and inches, in decimal-like format (see Explanation below). Assume any height is possible.

Then display your name and display your height in inches, as a whole number (no decimals).
Hint: to avoid mixing “int” and “double” in calculations, variables should be all data type “double”.

Explanation: here’s how to enter your height in decimal like format: 5 feet 11 inches would be entered as 5.11; 6 feet 4 inches would be entered as 6.04; 5 feet 6 inches would be entered as 5.06 (not 5.6).

Note: use math functions in the calculations where possible, e. g., pow, sqrt, abs, and use combined operators where possible.

Execute your program three times and have the user select each of the three choices below, in order:
-enter choice #3: base = 6, height = 15.8; in Part 2, enter your name and your height.
-enter choice #1: radius = 10.5; in Part 2, enter King Kong and 200.06
-enter choice #2: length = 2.6, width = 11.1; for part 2, enter Shirley Temple and 3.11

Provide three screen prints, for the three choices above (which should also show the results of Part #2).

can't get to do part 2,please help
Jul 12, 2016 at 7:58am
can't get to do part 2,please help


Please post your code, we are not just going to write it for you :+)
Jul 12, 2016 at 7:02pm
closed account (48bpfSEw)
A simple way to create a programm is to use the given text and find the keywords what the computer has to do
and mark anything else as a comment. Voilà you have your first raw modell : a pseudo code!

It is not important to know the exact commands of c++. Write instead a "empty" function which you can fill with the right commands.

The second step is to use the syntax of the language.




/*
Part #1:
Write a program which displays the following menu:
*/

1
2
3
4
5
6
DISPLAY(
Geometry Calculator:
1. Calculate the area of a circle
2. Calculate the area of a rectangle
3. Calculate the area of a triangle
4. Quit)



/*
If the user enters 1, have the user enter the radius of a circle, then calculate the circle's area.
If the user enters 2, have the user enter the length and width of a rectangle, then calculate its area.
If the user enters 3, have the user enter the length of the triangle's base and its height, and calculate its area.
If the user enters 4, have Part 1 of the program end.
*/

/*
Validations: the user must enter a valid number from the menu, and all other entries must be greater than zero.
*/

1
2
3
4
5
ValuateUserInput ()

IF_USERINPUT (1, calcRadius)
IF_USERINPUT (2, calcRectangle)
etc



/*
Display the area of the chosen shape after the user selects a choice (areas should be displayed to 3 decimal points).
*/


-----------------------


1
2
3
4
5
6
7
8
9
calcRadius()
  INPUT ("Radius", radius);
  area = pi * radius^2;
  PRINT ("Area of circle is: ", area)

calcRectangle ()  
  INPUT ("Length", length);
  area = length^2;
  PRINT ("Area of square is: ", area)





************************* STEP 2

c++fy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DISPLAY("Geometry Calculator:");
DISPLAY("1. Calculate the area of a circle");
DISPLAY("2. Calculate the area of a rectangle");
DISPLAY("3. Calculate the area of a triangle");
DISPLAY("4. Quit");


void DISPLAY (char* pszString) {
  std::cout << pszString << endl;
}

void INPUT (char* pszCaption, float &flValue) {
  DISPLAY (pszCaption); 
  std::cin >> flValue;
}



etc.

Next step is to write layers to the already written and prooved functions
e.g.

1
2
3
4
5
6
7
8
9
10
void INPUT (char* pszCaption, float &flValue, bool boCheckAllowedRange, float flRangeFrom, float flRangeTo) {

  INPUT (pszCaption, flValue);    
  
  if (!boCheckAllowedRange)
    return;
    
  if (flValue < flFrom)
    return WARNING ("Value is not in range: ", flRangeFrom, flRangeTo); 
}


etc.


Next step is to shrink the parameter lists and use class or structs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
struct INPUT_DATA {
public:
  char*   pszCaption; 
  float   flValue; 
  bool    boCheckAllowedRange; 
  float   flRangeFrom;
  float   flRangeTo;
  
  InputData(  
    char*   _pszCaption, 
    float*  _ptrflValue,
    bool    _boCheckAllowedRange,    
    float   _flRangeFrom,
    float   _flRangeTo) {
    
      pszCaption              = _pszCaption;
      flValue                 = _flValue; 
      boCheckAllowedRange     = _boCheckAllowedRange; 
      flRangeFrom             = _flRangeFrom;
      flRangeTo               = _flRangeTo;           
  }
};

void INPUT (INPUT_DATA &inputData) {

  INPUT (inputData.pszCaption, inputData.flValue);    
  
  if (!inputData.boCheckAllowedRange)
    return;
    
  if (inputData.flValue < inputData.flFrom)
    return WARNING ("Value is not in range: ", flRangeFrom, flRangeTo); 
}


main:

  INPUT_DATA inputData (
        "Input radius", 
        1.0,  // default value
        true, // check range
        0.1,
        9.9);
        
  INPUT (inputData);




there are more steps, but this is enough for the beginning. ^^


ok, a little trailer:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class InputCalcOutput_Common {
public:
  string  strCaption; 
  
  void Input (int    &v);
  void Input (float  &v);
  void Input (string &v);
  
  void Output (string sCaption, string &v);
  
  virtual CheckRange (void) = 0;
};

  
class InputCalcOutput_Radius : public InputCalcOutput_Common {
public:
  float flValue;
  float flMin, flMax;
  float flArea, flCircum;
  
  void Input (bool boCheckRange = false) { 
    Input (flValue); 
    
    if (boCheckRange)
      CheckRange();
    }  
  
  void CheckRange () { 
    if (flValue < flMin || flValue > flMax)
      throw exception ("Range invalid!");
    }
  
  void Calculate () {
    flArea    = flValue * ...;
    flCircum  = flValue * ...;
  }

  void Output () { 
    Output("Area: ", flArea);
    Output("Circum: ", flCircum);
    }
    
  void Execute() {
    Input();
    Calculate();
    Output();
  }
};



Topic archived. No new replies allowed.