Hey guys i'm working with this code but i can't get it to stop looping back into the menu any ideas??? Thanks !!

#include <iostream>
using namespace std;

unsigned int menu();

int main() {
	
	int i,num[20],sec[20],j,choice;  	
	
	cout<< "Please enter 20 integers";	
	for (i=0; i<20; i++) 	{
		cout<<"\nEnter next value:";		
		
		cin>>num[i]; 	}
	
	
	
	for (;;) {
		switch (menu()) {
		case 0:
			return 0;
		case 1:{
			cout<< num[i];
			   }
			   break;
		case 2:
			//some function to sort
			break;
		case 3:
			//some function to show array
			break;
		case 4:
			//some function to show adress of 1st el.
			break;
		}
	}
	return 0;
}

unsigned int menu() {
	unsigned int c = -1;
	while (c > 4) {
		cout <<"<0> Exit\n"
			"<1> Display their input\n"
			"<2> Sort the array\n"
			"<3> Show the sorted array\n"
			"<4> Show the address of the first element\n";

		cin >> c;
		if (c > 4) {
			cout << "Try again\n";
		}
	}
	return c;
}

Recommended Answers

All 5 Replies

Look at line 18: That is an infinite loop. Delete it and your program won't loop back.

Ok this may be a dumb question on line 22 i have 'case 1:' now right now i have a cout statement but it doesn't do anything so is it not possible to go from my menu back in to case 1 and then have it do a cout statement or an if statement?

Thank you by the way that was something really dumb for me to miss

use code tags, also.

Ok this may be a dumb question on line 22 i have 'case 1:' now right now i have a cout statement but it doesn't do anything so is it not possible to go from my menu back in to case 1 and then have it do a cout statement or an if statement?

First, the cout for case 1 is incorrect because the value of i is greater than the number of elements in the array. Otherwise it works ok for me, except you need to output the "\n" after the number so that the menu doesn't get screwed up again. cout << num[0] << "\n";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.