Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PART A Find and fix errors This program displays a menu and asks the user to make a selection. A do-while loop repeats the program

PART A Find and fix errors

This program displays a menu and asks the user to make a selection. A do-while loop repeats the program until the user selects item 4 from the menu.

The same problem is solved in three different ways: 1. - using a for loop 2. - usinf a while loop 3. - using a do-while loop NAME: */

#include #include using namespace std;

// This is a function declaration void displayMenu(void);

int main() { // Variables int choice // Menu choice

do { // Display the menu. displayMenu(); // This is a function call cout << "Enter your choice: " cin << choice; // Validate the menu selection. while (choice < 1 && choice >= 4 { cout << "Please enter a valid menu choice: "; cin >> choice; } // Process the user's choice. int i, power, n = 6; // variables needed for loops switch (choice) case :1 cout << "Your choice is the \"for loop\": "; for (i = 0, power = 1; i <= n, i++); { cout << "2 to " << i << " is " << power << endl; power *= 2; } case 2: cout << "Your choice is the \"while loop\": "; i = 0; power = 1; while (i < n) cout << "2 to " << i << " is " << power << endl; power *= 2; i++; break; case 3 cout << "Your choice is the \"do-while loop\": "; i = 0; power = 1; do { cout << "2 to " << i << " is " << power << endl; power *= 2; i++; } while (i <= n) break; case 4: cout << "Good bye! "; break; } // end switch } while (choice != 4);

return 0; } /*~*~*~*~*~*~*~*~*~*~*~ This function displays a menu */ void displayMenu(void) // This is a function definition { cout << " \t\tC++ Loops Menu: Print the powers of 2 using a " << "1. for loop " << "2. while loop " << "3. do - while loop " << "4. Quit the Program "; } /**~*~*~*~*~*~*~*~*~* OUTPUT

*/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Creating A Database In Filemaker Pro Visual QuickProject Guide

Authors: Steven A. Schwartz

1st Edition

0321321219, 978-0321321213

More Books

Students also viewed these Databases questions

Question

=+ (c) From (18.10) deduce T(4) = VIT.

Answered: 1 week ago

Question

3. List ways to manage relationship dynamics

Answered: 1 week ago