0% found this document useful (0 votes)
7 views8 pages

Bajade Labtask5

EDP Labtask 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Bajade Labtask5

EDP Labtask 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LABTASK 5

1.)How many sets of switch structures were used in the program? What was the data type
used in the selection structure that allows input letters?

The program uses one switch structure. The data type used in the selection structure to
allow input letters is `char`.

2.)In the main menu selection, if the expression to be inputted is a number instead of
characters, which portion of the program needs to change

If the input should be a number instead of characters, you would need to change how the
`option` variable is handled. Specifically, you need to change the `option` variable from
`char` to `int`, and update the `switch` cases to handle integers. Here’s how the
modified portion of the code would look:

```cpp

Int main() {

Int option;

While (true) {

Cout << “\nMain List Option\n”;

Cout << “[1] Perimeter\n”;

Cout << “[2] Area\n”;

Cout << “[3] Surface Area\n”;

Cout << “[4] Exit\n”;

Cout << “Enter Option: “;


Cin >> option;

If (option == 4) {

Cout << “Exiting program…” << endl;

Break;

Switch (option) {

Case 1: {

Double side;

Cout << “Square\n”;

Cout << “Enter side measurement of the square: “;

Cin >> side;

Double perimeter = 4 * side;

Cout << fixed << setprecision(2);

Cout << “The perimeter of a square with a side measurement of “ << side << “
inches is “ << perimeter << “ inches” << endl;

Break;

Case 2: {

Double b1, b2, height;

Cout << “Trapezoid\n”;

Cout << “Enter the first base measurement of the trapezoid: “;

Cin >> b1;


Cout << “Enter the second base measurement of the trapezoid: “;

Cin >> b2;

Cout << “Enter the height measurement of the trapezoid: “;

Cin >> height;

Double area = 0.5 * (b1 + b2) * height;

Cout << fixed << setprecision(2);

Cout << “The area of the trapezoid with base measurements “ << b1 << “ and “ <<
b2 << “ and height “ << height << “ is “ << area << “ square inches” << endl;

Break;

Case 3: {

Double side;

Cout << “Cube\n”;

Cout << “Enter side measurement of the cube: “;

Cin >> side;

Double surfaceArea = 6 * side * side;

Cout << fixed << setprecision(2);

Cout << “The surface area of the cube with a side measurement of “ << side << “
inches is “ << surfaceArea << “ square inches” << endl;

Break;

Default:

Cout << “Invalid option, please try again!” << endl;

}
Char back;

Cout << “Press <K/k> to go back to the main menu or any other key to exit: “;

Cin >> back;

If (back != ‘K’ && back != ‘k’) {

Cout << “Exiting the Program, Thank you!” << endl;

Break;

Return 0;

```

3.)What happens if the default statement in a switch structure is comitted?

If the `default` statement is omitted in a `switch` structure, nothing will happen if none of
the cases match the Input. The program will simply exit the switch block without executing
any statements. This can lead to situations where invalid input is not properly handled or
no feedback is given to the user.

4.)How do you enable the user to input either lowercase `a` or uppercase `A`? Write the
complete C++ code.

My current program already allows for both lowercase `a` and uppercase `A` because it
handles both cases in the `switch` statement.

```cpp
#include <iostream>

#include <iomanip>

Using namespace std;

Int main() {

Char option;

While (true) {

Cout << “\nMain List Option\n”;

Cout << “[A] Perimeter\n”;

Cout << “[B] Area\n”;

Cout << “[C] Surface Area\n”;

Cout << “[D] Exit\n”;

Cout << “Enter Option: “;

Cin >> option;

If (option == ‘D’ || option == ‘d’) {

Cout << “Exiting program…” << endl;

Break;

Switch (option) {

Case ‘A’:

Case ‘a’: {
Double side;

Cout << “Square\n”;

Cout << “Enter side measurement of the square: “;

Cin >> side;

Double perimeter = 4 * side;

Cout << fixed << setprecision(2);

Cout << “The perimeter of a square with a side measurement of “ << side << “
inches is “ << perimeter << “ inches” << endl;

Break;

Case ‘B’:

Case ‘b’: {

Double b1, b2, height;

Cout << “Trapezoid\n”;

Cout << “Enter the first base measurement of the trapezoid: “;

Cin >> b1;

Cout << “Enter the second base measurement of the trapezoid: “;

Cin >> b2;

Cout << “Enter the height measurement of the trapezoid: “;

Cin >> height;

Double area = 0.5 * (b1 + b2) * height;

Cout << fixed << setprecision(2);

Cout << “The area of the trapezoid with base measurements “ << b1 << “ and “ <<
b2 << “ and height “ << height << “ is “ << area << “ square inches” << endl;

Break;
}

Case ‘C’:

Case ‘c’: {

Double side;

Cout << “Cube\n”;

Cout << “Enter side measurement of the cube: “;

Cin >> side;

Double surfaceArea = 6 * side * side;

Cout << fixed << setprecision(2);

Cout << “The surface area of the cube with a side measurement of “ << side << “
inches is “ << surfaceArea << “ square inches” << endl;

Break;

Default:

Cout << “Invalid option, please try again!” << endl;

Char back;

Cout << “Press <K/k> to go back to the main menu or any other key to exit: “;

Cin >> back;

If (back != ‘K’ && back != ‘k’) {

Cout << “Exiting the Program, Thank you!” << endl;

Break;

}
Return 0;

```

In this code, the `switch` statement allows for both lowercase and uppercase input for
each case option (`A/a`, `B/b`, `C/c`). This ensures that the user can enter either
lowercase or uppercase letters to select an option.

OBSERVATION

I observed that the program efficiently handles various geometric calculations such as
perimeter, area, and surface area based on user input. It uses a single `switch` structure
to manage user menu selections, allowing for both uppercase and lowercase letter inputs
(`char` type), enhancing user-friendliness. The program correctly handles input validation
by checking if the input scores are within the valid range of 0 to 100, ensuring robustness.

The `while` loop In the main function is well-implemented, allowing the menu to be
displayed repeatedly until the user decides to exit. Each geometric calculation is
encapsulated within a case block, and the use of `fixed` and `setprecision(2)` ensures
that the output is formatted to two decimal places, providing clear and precise results.

Additionally, the program prompts the user to return to the main menu or exit after each
operation, adding to the overall user experience. However, the repetition of input validation
code could be refactored into a separate function to improve readability and
maintainability. Overall, the program is functional and meets its objectives, with clear
output and user guidance.

You might also like