### keeps on going and going and does not stop

How do I get this to stop looping continuously? Seems like it's the same problem for each selection:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236`` ``````#include #include #include using namespace std; #define LOWERBOUND -999.99 #define UPPERBOUND 999.99 #define LOWFACT 0 #define UPPERFACT 14 char Menu(char menuchoice); int getInteger(string, int, int); double getReal(string, double, double); void doLarger(); double calcLarger(double, double); void doDivisible(); bool isDivisible(int dividend, int divisor); void doRange(); double calcRange(double num1, double num2, double num3, double num4); void doPower(); double calcMtoN(double M, int N); void doFactorial(); int calcFactorial(int number); int main() { char choice; choice = Menu(choice); //Call menu function while(choice != 'Q' && choice != 'q') { if(choice == 'L' || choice == 'l') { doLarger(); } else if(choice == 'D' || choice == 'd') { doDivisible(); } else if(choice == 'R' || choice == 'r') { doRange(); } else if(choice == 'P' || choice == 'p') { doPower(); } else { doFactorial(); } } return 0; } //***************************************************************************************************************************************** int getInteger(string prompt, int lowerBound, int upperBound) { int userValue; cout << prompt << "(" << lowerBound << "-" << upperBound << ")"; cin >> userValue; while(userValue < lowerBound || userValue > upperBound) { cout << userValue << " is invalid. The value must be between " << lowerBound << "and" << upperBound << ". Try again: "; cin >> userValue; } return userValue; } double getReal(string prompt, double lowerBound, double upperBound) { double userValue; cout << prompt << "(" << lowerBound << "-" << upperBound << ")"; cin >> userValue; while(userValue < lowerBound || userValue > upperBound) { cout << userValue << " is invalid. The value must be between " << lowerBound << "and" << upperBound << ". Try again: "; cin >> userValue; } return userValue; } void doLarger() { double num1, num2, largernum; num1 = getReal("Enter the first real value: ", -999.99, 999.99); num2 = getReal("Enter the second real value: ", -999.99, 999.99); largernum = calcLarger(num1, num2); cout << "The largest number is: " << largernum << "." << endl; } double calcLarger(double num1, double num2) { double max; if (num1 < num2) { max = num2; cout << endl; cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << max << " is the larger number"; //the second number the user inputs will be shown on the screen here } else { max = num1; cout << endl; cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << max << " is the larger number"; //the first number the user inputs will be shown on the screen here } } void doDivisible() { int num1, num2; bool vertict; num1 = getInteger("Enter the first real value: ", -100.00, 100.00); num2 = getInteger("Enter the second real vale: ", -100.00, 100.00); } //**************************************************************************** bool isDivisible(int dividend, int divisor) { if (dividend % divisor) { cout << endl; cout << dividend << " IS NOT divisible by " << divisor; //will appear when two numbers the user inputs are not divisible by each other } else { cout << endl; cout << dividend << " IS divisible by " << divisor; //will appear when two numbers the user inputs are divisible by each other } } //************************************************************************** void doRange() { double num1, num2, num3, num4, range; num1 = getReal("Enter the first real value: ", -100.00, 100.00); num2 = getReal("Enter the second real value: ", -100.00, 100.00); num3 = getReal("Enter the third real value: ", -100.00, 100.00); num4 = getReal("Enter the fourth real value: ", -100.00, 100.00); range = calcRange(num1, num2, num3, num4); cout << "The range is: " << range << "." << endl; } //*************************************************************************** double calcRange(double num1, double num2, double num3, double num4) { double numlarge, nummin, range; } //******************************************************************************* void doPower() { double num1, value; int num2; num1 = getReal("Enter the value of M", 1.00, 100.00); num2 = getInteger("Enter the value of N",1, 100); value = calcMtoN(num1, num2); cout << num1 << " to the power of " << num2 << "is" << value << endl << endl; } //************************************************************************************ double calcMtoN(double M, int N) { int i; double num3 = 1.0; for (i=1; i <= N; i += 1) { num3 += M; } return num3; } //************************************************************************************* void doFactorial() { int num, fact; num = getInteger("Enter a number: ", LOWFACT, UPPERFACT); fact = calcFactorial(num); cout << "The factorial of " << num << "is" << fact << endl << endl; } //****************************** int calcFactorial(int number) { int i, h; for(h=1; i <= number; i++) { h *= i; } return h; } //****************************************** char Menu(char menuchoice) { cout << "Make a selction from the list below." << endl //user makes a selction from the following menu << "\nL) Determine the 'L'arger of 2 real numbers" << endl << "D) Check if an integer is 'D'ivisible by another integer" << endl << "R) Find the 'R'ange of a set of 4 real numbers" << endl << "P) Find the value of the expression, M to the Nth 'P'ower" << endl << "F) Find the value of a 'F'actorial"<< endl << "\nQ) 'Q'uit" << endl << "\nWhat would you like to do? "; cin >> menuchoice; while (menuchoice != 'l' && menuchoice != 'L' && menuchoice != 'd' && menuchoice != 'D' && menuchoice != 'r' && menuchoice != 'R' && menuchoice != 'p' && menuchoice != 'P' && menuchoice != 'f' && menuchoice != 'F' && menuchoice != 'q' && menuchoice != 'Q') { cout << "***** INVALID SELECTION *** Choose again from (L, D, R, P, F, or Q): " ; //user has to rechoose a letter until they pick one from the menu cin >> menuchoice; } } ``````

Last edited on
Which loop??
well maybe it's not looping but it keeps going on and on and on and the program does not stop after lets say I chose P and I entered my values. Then it keeps asking for me to enter another number under the same choice. Happens to all of them..
yes, but which loops specifically? Line numbers, please?
I believe its the stuff after return 0. I'm thinking it might be the voids since the choice keeps popping up asking me to "enter a value". Sorry about not being specific enough.
I think you'd have an easier time with do-while loops.
Someone told me what the problem is. "TThe problem with your program is that the Menu function isn't written correctly. You have:

as your function header, which tells the compiler that the Menu function takes an argument. In this assignment, it should not be passed an argument. It should be:

and the function header should be:

The menuchoice that you are using in the function should be declared inside of the function, just like you would declare a variable inside main() when you want to use it in there. Then at the end of the Menu function, you need to return the value that menuchoice is holding.

Once you make this change to the Menu function, that will change your calling statement in main. You currently have:

it will now be:

The other problem is that you have to have a second call to the Menu function at the bottom of the while loop in main. You still have to follow the priming/secondary read pattern that we've talked about in lecture. The first call to Menu that is outside of the loop is the priming read. The one at the bottom of the loop will the secondary read."

I have it to:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240`` ``````#include #include #include using namespace std; #define LOWERBOUND -999.99 #define UPPERBOUND 999.99 #define LOWFACT 0 #define UPPERFACT 14 char Menu(); int getInteger(string, int, int); double getReal(string, double, double); void doLarger(); double calcLarger(double, double); void doDivisible(); bool isDivisible(int dividend, int divisor); void doRange(); double calcRange(double num1, double num2, double num3, double num4); void doPower(); double calcMtoN(double M, int N); void doFactorial(); int calcFactorial(int number); int main() { char choice; choice = Menu(); //Call menu function while(choice != 'Q' && choice != 'q') { if(choice == 'L' || choice == 'l') { doLarger(); } else if(choice == 'D' || choice == 'd') { doDivisible(); } else if(choice == 'R' || choice == 'r') { doRange(); } else if(choice == 'P' || choice == 'p') { doPower(); } else { doFactorial(); } } choice = Menu(); return 0; } //***************************************************************************************************************************************** int getInteger(string prompt, int lowerBound, int upperBound) { int userValue; cout << prompt << "(" << lowerBound << "-" << upperBound << ")"; cin >> userValue; while(userValue < lowerBound || userValue > upperBound) { cout << userValue << " is invalid. The value must be between " << lowerBound << "and" << upperBound << ". Try again: "; cin >> userValue; } return userValue; } double getReal(string prompt, double lowerBound, double upperBound) { double userValue; cout << prompt << "(" << lowerBound << "-" << upperBound << ")"; cin >> userValue; while(userValue < lowerBound || userValue > upperBound) { cout << userValue << " is invalid. The value must be between " << lowerBound << "and" << upperBound << ". Try again: "; cin >> userValue; } return userValue; } void doLarger() { double num1, num2, largernum; num1 = getReal("Enter the first real value: ", LOWERBOUND, UPPERBOUND); num2 = getReal("Enter the second real value: ", LOWERBOUND, UPPERBOUND); largernum = calcLarger(num1, num2); cout << "The largest number is: " << largernum << "." << endl; } double calcLarger(double num1, double num2) { double max; if(num1 < num2) { max = num2; cout << endl; cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << max << " is the larger number"; //the second number the user inputs will be shown on the screen here } else { max = num1; cout << endl; cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << max << " is the larger number"; //the first number the user inputs will be shown on the screen here } } void doDivisible() { int num1, num2; bool vertict; num1 = getInteger("Enter the first real value: ", -100.00, 100.00); num2 = getInteger("Enter the second real vale: ", -100.00, 100.00); } //**************************************************************************** bool isDivisible(int dividend, int divisor) { if (dividend % divisor) { cout << endl; cout << dividend << " IS NOT divisible by " << divisor; //will appear when two numbers the user inputs are not divisible by each other } else { cout << endl; cout << dividend << " IS divisible by " << divisor; //will appear when two numbers the user inputs are divisible by each other } } //************************************************************************** void doRange() { double num1, num2, num3, num4, range; num1 = getReal("Enter the first real value: ", -100.00, 100.00); num2 = getReal("Enter the second real value: ", -100.00, 100.00); num3 = getReal("Enter the third real value: ", -100.00, 100.00); num4 = getReal("Enter the fourth real value: ", -100.00, 100.00); range = calcRange(num1, num2, num3, num4); cout << "The range is: " << range << "." << endl; } //*************************************************************************** double calcRange(double num1, double num2, double num3, double num4) { double numlarge, nummin, range; } //******************************************************************************* void doPower() { double num1, value; int num2; num1 = getReal("Enter the value of M", 1.00, 100.00); num2 = getInteger("Enter the value of N",1, 100); value = calcMtoN(num1, num2); cout << num1 << " to the power of " << num2 << "is" << value << endl << endl; } //************************************************************************************ double calcMtoN(double M, int N) { int i; double num3 = 1.0; for (i=1; i <= N; i += 1) { num3 += M; } return num3; } //************************************************************************************* void doFactorial() { int num, fact; num = getInteger("Enter a number: ", LOWFACT, UPPERFACT); fact = calcFactorial(num); cout << "The factorial of " << num << "is" << fact << endl << endl; } //****************************** int calcFactorial(int number) { int i, h; for(h=1; i <= number; i++) { h *= i; } return h; } //****************************************** char Menu() { string choice; cout << "Make a selction from the list below." << endl //user makes a selction from the following menu << "\nL) Determine the 'L'arger of 2 real numbers" << endl << "D) Check if an integer is 'D'ivisible by another integer" << endl << "R) Find the 'R'ange of a set of 4 real numbers" << endl << "P) Find the value of the expression, M to the Nth 'P'ower" << endl << "F) Find the value of a 'F'actorial"<< endl << "\nQ) 'Q'uit" << endl << "\nWhat would you like to do? "; cin >> choice; while (choice != "l" && choice != "L" && choice != "d" && choice != "D" && choice != "r" && choice != "R" && choice != "p" && choice != "P" && choice != "f" && choice != "F" && choice != "q" && choice != "Q") { cout << "***** INVALID SELECTION *** Choose again from (L, D, R, P, F, or Q): " ; //user has to rechoose a letter until they pick one from the menu cin >> choice; } return choice; } ``````

Something still not working right.
never mind. I figured out what was wrong. Thanks for the suggestions though!
What ended up being wrong? I have the same issue with a similar program.
Topic archived. No new replies allowed.