How would I create a program that allows the program to choose which loop they want to use to enter 5 integers and calculate the average. The program should have a menu of loop choices for the user to choose from. Each loop should be contained in a separate function. The integers should be passed to the functions using references. Each loop should display a message identifying itself.(Ex. If the user selects while loop there will be a message saying you have selected the while loop. The menu shouldn't exit until the user specifies that they want to exit. I have to use a numbering system. Can someone please help!
default:
cout << "You made an illegal choice." << endl;
}
}
//This program will let the user choose 5 intgers
//The program will then tak ethe average of the numbers the user picks.
int a,b,c,d,e,sum,avg;
cout << "Enter five numbers:\n";
cin >> a >> b >> c >> d>>e;
sum = a + b + c + d + e;
cout << "The sum of the numbers is=" << sum;
avg = sum / 5;
cout << "\nThe average of the numbers is=" << avg;
I have no idea how you want to relate the 5 input nos to the choices - easy, hard, etc - this part have been commented out and you can insert it as desired. It might be better to make a function outside main() and offer this function as part of one of the choices
#include <iostream>
usingnamespace std;
int main()
{
bool fQuit = false;
int choice = 0;
while (!fQuit)
{
cout << "Difficulty levels\n";
cout << "1. Easy \n2. Normal \n3. Hard \n4. Exit \nChoice \n";
cin >> choice;
switch (choice)
{
case 1:
cout << "You picked Easy. \n";
break;
case 2:
cout << "You picked Normal. \n";
break;
case 3:
cout << "You picked Hard. \n";
break;
case 4:
cout << "You picked to Exit. \n";
fQuit = true;
break;
default:
cout << "You made an illegal choice.\n";
break;
}
}
//This program will let the user choose 5 intgers
//The program will then tak ethe average of the numbers the user picks.
/*
int a,b,c,d,e,sum,avg;
cout << "Enter five numbers:\n";
cin >> a >> b >> c >> d>>e;
sum = a + b + c + d + e;
cout << "The sum of the numbers is=" << sum;
avg = sum / 5;
cout << "\nThe average of the numbers is=" << avg;
*/
return 0;
}
But how would I get the whole program to loop including the average.
(Also you are right about the easy, hard etc.. I was rushing and copied and pasted the wrong program. This is the original)
// Menu Chooser
// Demonstrates the switch statement
#include <iostream>
using namespace std;
int main()
{
int choice = 0;
while (choice <= 0)
{
cout << "Difficulty levels\n\n";
cout << "1 - Do loop" << endl;
cout << "2 - For loop" << endl;
cout << "3 - Do while loop" << endl;
cout << "4 -Exit" << endl;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "You picked Do loop." << endl;
break;
case 2:
cout << "You picked For loop." << endl;
break;
case 3:
cout << "You picked Do while loop." << endl;
break;
case 4:
cout << "You picked to Exit." << endl;
break;
default:
cout << "You made an illegal choice." << endl;
}
}
//This program will let the user choose 5 intgers
//The program will then tak ethe average of the numbers the user picks
{
{
int sum = 0;
int avg = 0;
int a, b, c, d, e;
cout << "Enter five numbers:\n";
cin >> a >> b >> c >> d >> e;
sum = a + b + c + d + e;
cout << "The sum of the numbers is=" << sum;
avg = sum / 5;
cout << "\nThe average of the numbers is=" << avg;
do
{