int main()
{
// This program will show my name, Id # and the class period.
//Function Protoype
int choice;
int total = 0;
float a, b, c;
//Menu Chooser
//Choose the type of loop you want to use to average this program
while (choice != 4)
{
cout << "Loops\n\n";
cout << "1 - Do loop\n";
cout << "2 - For loop\n";
cout << "3 - Do while loop\n\n";
cout << "4 -Exit\n\n";
cout << "Choice: ";
cin >> choice;
// Demonstrates the switch statement.
//When you pick the loop, the cases is how you get the average of your numbers.
int average = 0;
{
switch (choice)
{
case 1:
{
a = DOWHILELOOP();
cout << "\You have chosen Dowhile loop.";
cout << "\The average is:" << a;
}
break;
case 2:
{
b = FORLOOP();
cout << "\You have chosen For Loop.";
cout << "\The average is:" << b;
}
break;
case 3:
{
c = WHILELOOP();
cout << "\You have chosen While Loop.";
cout << "\The average is:" << c;
}
break;
case 4:
{
cout << "Goodbye";
}
break;
default:
{
cout << "You made an illegal choice.\n";
}
break;
}
}
}
return 0;
}
float FORLOOP()
{
//This is the for loop.
float average;
int num = 0;
int total = 0;
for (int i = 0; i < 5; i++)
{
cout << "Eneter an Intger:";
cin >> num;
total += num;
}
//average
average = total / 5;
return average;
}
float DOWHILELOOP()
{
int count;
int num;
int total;
float average;
count = 0;
total = 0;
do
{
cout << "Please enter an intger:";
cin >> num;
total += num;
count++;
} while (count < 5);
average = total / 5;
return average;
}
float WHILELOOP()
{
float average;
int count;
int num;
int total;
count = 0;
total = 0;
while (count < 5)
{
cout << "Enter an Intger:";
cin >> num;
total += num;
count++;
}
//Taking the avg.
average = total / 5;
return average;
OP: Generally all-caps identifiers should be avoided as they are used for #define macros which will trash your code should there ever be any conflict. The value of choice is unspecified when the while loop is first encountered, and while it's not very likely that choice is 4 at that point, it certainly isn't impossible.
How does what your code is doing now differ from what you want it to do?