I have this loop i made and just cant figure out why it wont stop running..can some one help ..I just need it to say what decision i made then stop...or display the ol' press any key to exit app..God Bless..

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	std::cout << "How old are you: " << std::endl;
	std::cout << "\n";
	std::cout << "[1]. add" << std::endl;
	std::cout << "[2]. subtract" << std::endl;
	std::cout << "[3]. divide" << std::endl;
	std::cout << "[4]. multiply" << std::endl;

	int ages[4];
	ages[0]='1';
	ages[1]='2';
	ages[2]='3';
	ages[3]='4';
	
	do {
		std::cin >> ages[4];
		std::cin.get();
	if (ages[4] == 1)
		std::cout << "You choose [add]..." << std::endl;
	else if (ages[4] == 2)
		std::cout << "You choose [subtract]..." << std::endl;
	else if (ages[4] == 3)
		std::cout << "You choose [divide]..." << std::endl;
	else if (ages[4] == 4)
		std::cout << "You choose [multiply]..." << std::endl;
	else
		std::cout << "Sorry!!! choose one of the four listed above!!!" << std::endl;
	}
	while (ages[4] != 1,2,3,4);
	

}
Salem commented: Stop hijacking other peoples threads! -6

Recommended Answers

All 8 Replies

What are you trying to accomplish with the above code? You seem to have misunderstood the uses of arrays, and its definitely not needed in this implementation... Apart from that, your code just doesn't make sense...

I would strongly recommend you take this tutorial of the C++ language, its uses and implementation, here.

And after going through that tutorial, see this. It shows you how to implement a simple, working menu.

ages[0]='1';
ages[1]='2';
ages[2]='3';
ages[3]='4';

won't put the numbers 1, 2, 3 and 4 in the array :)
You'll have to write

ages[0]=1;
ages[1]=2;
ages[2]=3;
ages[3]=4;

Or you can truncate it by directly initializing the array: int ages[4] = {1,2,3,4}; while (ages[4] != 1,2,3,4); should be: while (1<=ages[4] && ages[4]<=4); Instead of writing:

if (ages[4] == 1)
  std::cout << "You choose [add]..." << std::endl;
else if (ages[4] == 2)
  std::cout << "You choose [subtract]..." << std::endl;
else if (ages[4] == 3)
  std::cout << "You choose [divide]..." << std::endl;
else if (ages[4] == 4)
  std::cout << "You choose [multiply]..." << std::endl;
else
  std::cout << "Sorry!!! choose one of the four listed

a switch is the preferred way to do this:

switch(ages[4])
{
case 1:
    std::cout << "You choose [add]..." << std::endl;
    break;
case 2:
    std::cout << "You choose [subtract]..." << std::endl;
    break;
case 3:
    std::cout << "You choose [divide]..." << std::endl;
    break;
case 4:
    std::cout << "You choose [multiply]..." << std::endl;
    break;
default:
    std::cout << "Sorry!!! choose one of the four listed << std::endl;
}

Actually you don't have to specify std:: each time before cout, endl, etc... as you're already including the following line in your code: using namespace std; :P

Correction of something in my previous post:

...
while (ages[4] != 1,2,3,4);
should be:
while ([B]!([/B]1<=ages[4] && ages[4]<=4[B])[/B]);
...

If you want something like an option menu, try this:

/**
@description: A simple option menu
*/
int opt = 0;
cout << "Available options: 1, 2, 3" << endl << endl;
do
{
    cout << "What option do you want?";
    if(!(cin>>opt))
    {
        cin.clear();
        cin.ignore(1024, '\n'); // remove bad input from the stream
        opt = 0;
    }
    switch(opt)
    {
    case 1:
        // code for option 1
        break;
    case 2:
        // code for option 2
        break;
    case 3:
        // code for option 3
        break;
    default:
        cout << "Please choose a valid option!" << endl << endl;
    }
} while (!(1<=opt && opt<=3));

first of all you need to learn what array are.you have created an array of

int ages[4];

it means that there are 4 elements in this array,which are

ages[0];
ages[1];
ages[2];
ages[3];

there are total 4 elements ,since arrays start from 0 so you have elements from ages[0] to ages[3].the element ages[4] that you are trying to access is not even initialized.
it will give you an "array out of bound" error!


that is why you program doesnot terminate;it run an infinite loop.

if you have made and array of 5 members like

int ages[5];
//then you can access 
ages[4];

and another thing is,when you have to check multiple conditions then you use the
OR ->"||"
And -> "&&"
operators.
you donot check all the conditions like you did!
it should be like:

while(ages[4]!=1 || ages[4]!=2 || ages[4]!=3 || ages[4]!=4);

i hope that helps!.....;p

it should be like:

while(ages[4]!=1 || ages[4]!=2 || ages[4]!=3 || ages[4]!=4);

Possible, but the preferred way of doing this is using a range like this: while(!(1<=ages[4] && ages[4]<=4)); as I'd already mentioned :)
(It's much more practical if you use this approach, imagine that you've to check for all numbers from 0 to 100 manually, using your approach, I wish you the best with it :P)

yes,i agree thats impractical but i think its better to teach 'em the basics first.

yes,i agree thats impractical but i think its better to teach 'em the basics first.

And a simple statement which uses some conditional operators aren't the basics or what?

hm....i think you are right!...

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.