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!\



// Menu Chooser
// Demonstrates the switch statement

#include <iostream>
using namespace std;

int main()
{
int choice = 0;
while (choice<!0)
{
cout << "loops\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 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;
}
Last edited on
If you plan on using for-loop or while loop, you should use an array and not declare multiple variables like a,b,c,d,e.
If you haven't learned functions yet, and I suspect you haven't, you're looking for something like this:

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
int num=-;sum=0;avg=0;   ////SEE CHANGE HERE!
int choice = 0;
while (choice<!0)
{
cout << "loops\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;

//put do loop here

cout << "Enter a number: ";
 cin >> num;
 sum+=num;

//end do loop here

cout << "The sum of the numbers is=" << sum;
avg = sum / 5;
cout << "\nThe average of the numbers is=" << avg;
break;


/////ALL OTHER CASES ARE SIMILAR ACCORDING TO THE TYPE OF LOOP 
 

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>

void while_loop(int &, int &, int (&)[5]);
void for_loop(int &, int &, int (&)[5]);
void do_while_loop(int &, int &, int (&)[5]);

unsigned short int get_choice(unsigned short int val = 0)
{
	std::cout << "Choice: " << std::flush;
	while (!(std::cin >> val) || (val < 1 || val > 4))
	{
		std::cin.clear();
		std::cin.ignore(1000000, '\n');
		std::cout << "Choice: " << std::flush;
	}
	return val;
}

void get_numbers(int (&numbers)[5])
{
	std::cout << "\nEnter five numbers:\n";
  for (auto & c:numbers)
	{
		while (!(std::cin >> c))
		{
			std::cin.clear();
			std::cin.ignore(1000000, '\n');
			std::cout << "Enter five numbers:\n";
		}
	}
}

int main()
{
	while (true)
	{
		std::cout << "Loops" << "\n\n";
		std::cout << "1 - while loop\n"
		             "2 - for loop\n"
		             "3 - do while loop\n"
		             "4 - exit\n\n";
		unsigned short int choice = 0;
		choice = get_choice();
		if (choice == 4)
		{
			std::cout << "\nYou picked to exit.\n";
			return 0;
		}
		int numbers[5], sum = 0, average = 0;
		get_numbers(numbers);
		std::cout << '\n';
		switch (choice)
		{
		case 1:
			while_loop(sum, average, numbers);
			break;
		case 2:
			for_loop(sum, average, numbers);
			break;
		case 3:
			do_while_loop(sum, average, numbers);
		}
		std::cout << "\nAverage: " << average << "\n\n";
		std::cout << "Press enter to continue...\n";
		std::cin.ignore();
		std::cin.get();
		for (int n = 0; n != 50; ++n)
		{
			std::cout << '\n';
		}
	}
	return 0;
}

void while_loop(int &sum, int &average, int (&numbers)[5])
{
	std::cout << "You picked while loop.\n";
	unsigned short int ind = 0;
	while (ind != 5)
	{
		sum += numbers[ind];
		++ind;
	}
	average = sum / 5;
}

void for_loop(int &sum, int &average, int (&numbers)[5])
{
	std::cout << "You picked for loop.\n";
	for (unsigned short int ind = 0; ind != 5; ++ind)
	{
		sum += numbers[ind];
	}
	average = sum / 5;
}

void do_while_loop(int &sum, int &average, int (&numbers)[5])
{
	std::cout << "You picked do while loop.\n";
	unsigned short int ind = 0;
	do
	{
		sum += numbers[ind];
		++ind;
	}
	while (ind != 5);
	average = sum / 5;
}
Last edited on
Topic archived. No new replies allowed.