Answer to Question #342817 in C++ for xam

Question #342817

Write a C++ program to call user defined function through switch

statement.

Sample Input/Output:


==========Main Menu==========

[S/s] – Square

[C/c] – Cube

[D/d] – Double

=============================


Enter your choice:d

Enter an Integer number:5

Double of 5 is 10


Do you want to try again? Y/y


==========Main Menu==========

[S/s] – Square

[C/c] – Cube

[D/d] – Double

=============================


Enter your Choice:s

Enter an Integer number:10

Square of 10 is 100

Do you want to try again? Y/y


==========Main Menu==========

[S/s] – Square

[C/c] – Cube

[D/d] – Double

=============================


Enter your Choice:C

Enter an Integer number:4

Cube of 4 is 64

Do you want to try again? N/n

Exit/Terminated


1
Expert's answer
2022-05-19T08:56:28-0400
#include <iostream>

using namespace std;

void Menu()
{
	cout << "==========Main Menu==========\n"
		<< "[S / s] - Square\n"
		<< "[C / c] - Cube\n"
		<< "[D / d] - Double\n";
}

int main() 
{
	char ch;
	int number;
	do
	{
		Menu();
		cout << "Enter your choice: ";
		cin >> ch;
		cout << "Enter an Integer number:";
		cin >> number;
		switch (ch)
		{
			case 'S':case's':
			{
				cout << "Square of " << number << " is " << number*number;
				break;
			}
			case 'C':case'c':
			{
				cout << "Cube of " << number << " is " << number*number*number;
				break;
			}
			case 'D':case'd':
			{
				cout << "Double of " << number << " is " << number*2;
				break;
			}
		}
	
		cout << "\nDo you want to try again? ";
		cin >> ch;
	} while (ch=='Y'|| ch == 'y');
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS