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;
}
|