How would I get this to loop?

How would I get this program to loop, only when the user says exit.

#include <iostream>
using namespace std;
//Function prototype.
float FORLOOP();
float DOWHILELOOP();
float WHILELOOP();

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;


}
Hey, a little advice, you might want to use
1
2
3
4
Do
{
   //do something
} while (choice != 4);


Oh and why dont you put goto loop ? Lol
Last edited on
Also
1
2
3
cout << "you chose forloop\n\n"; // I think it is better to use std::endl;
a = forloop();
cout << "average is " << a

And uh, why are you declaring total in int main()??
Last edited on
Flaze07 wrote:
// I think it is better to use std::endl;

http://chris-sharpe.blogspot.com/2016/02/why-you-shouldnt-use-stdendl.html

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?

Also see: http://www.cplusplus.com/articles/z13hAqkS/
@cire thanks for teh website
Topic archived. No new replies allowed.