Loops and average

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!
Last edited on
Sure, would be happy to help. Just post what you've done on your own first and we'll take it from there ...
Without any code or mention of progress, there isn't much we can do to help. A resource to look at for control mechanisms would be: http://www.cplusplus.com/doc/tutorial/control/.

#include <iostream>
using namespace std;

int main()
{

int choice = 0;
while (choice<!0)
{
cout << "Difficulty levels\n\n";
cout << "1 - Easy" << endl;
cout << "2 - Normal" << endl;
cout << "3 - Hard" << endl;
cout << "4 -Exit" << endl;

cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "You picked Easy." << endl;
break;
case 2:
cout << "You picked Normal." << endl;
break;
case 3:
cout << "You picked Hard." << 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 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;
}
Good, you made some progress. Note:
1. to loop through the switch use a bool as shown
2. re repeated endl's: http://stackoverflow.com/questions/9651311/buffer-flushing-n-vs-stdendl
3. use proper indentation in your code to make it easy to read

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

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
49

#include <iostream>
using namespace 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
{

} while (avg>0);

return 0;


}

}
}
gunnerfunner
^
use arrays for the 5 numbers, then you can use loops in the program for average as well as input
Topic archived. No new replies allowed.