How do I get a program to stop when using a while loop....

Could someone please explain the best way to get a program to quit when a user enters -1? I've tried several different ways of using a while loop and I just can't seem to get anything to work or it tells me it's not in a loop.

Thanks.


while ( answer < 0)

{
if (answer == -1)
break;
}

or
while (0)
[
if (answer == -1)
break;
}
Hello @newlearn33,

I do not know if
this is the best way
but
is a good one -i guess-
you can do something
like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Break.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;



int main(){

int choice=0; //start value: zero

        while(choice!=-1){ //while choice is not -1, do:
                cout<<"\nHi enter an option (-1 to quit): ";
                cin>>choice;
                cout<<"Your choice was: "<<choice<<endl;
                if(choice==-1) //if choice is -1, print:
                cout<<"Good bye!"<<endl;


        }//end while



return 0; //indicates success
}//end main 



Hi enter an option (-1 to quit): 10
Your choice was: 10

Hi enter an option (-1 to quit): 200
Your choice was: 200

Hi enter an option (-1 to quit): -1
Your choice was: -1
Good bye!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//...
int answer
cin >> answer;

while true;
{
if (answer == -1)
    {
     }
break;
else 
{
}

//......


Try using that I am not on mycomputer now so i cant give you the "exact" way but that is close
It is hard to give you an answer without knowing the exact problem you are trying to solve.

The way a while loop works is that it will keep executing while the test condition is true and stop as soon as it is false. It will run indefinitely if it is never false. If the test condition is never true the loop won't execute.

Many people who are new to programming usually create infinite loops as they make a loop that is always false.

Please post your exact problem and also your full program so we can take a look at it and advise you.

you have to ask the user at least once, so use a do.. while(..) loop.

In my opinion, do NOT do this kind of thing:

1
2
3
4
5
while(true)
{
...
}
Last edited on
break doesn't end a program. It simply exits the loop you are in regardless of whether or not the loops parameters are still true.

If you want to exit a program when a specific input is entered you could use return 1; instead of break.

Your main function is an integer function so when you return a value the program is done with that function. Since there are no more functions the program is complete.
Last edited on
closed account (Dy7SLyTq)
what are you talking about? while true is completely fine.
syntactically yes.
I've been toying around with the idea of using an infinite while(true) loop in a program. I think it would have some uses.

I mean if you want a program to continuously run regardless of inputs it would be an ok way to do things. I think the program that runs continuously would have to be linked to another program that calls/controls it though.

That way you could still get inputs for other "things" while the infinite loop program chugs along doing it's thing.


Not sure if i made that clear. I mean you would want the infinite loop to keep processing even if the program was waiting for user input.
Last edited on
I have added "in my opinion" :)
Last edited on
closed account (Dy7SLyTq)
Yet you also said syntatically. Its more than that. There are plenty of uses that make it better than do while
OK, have tried a few more things to no avail. I'm using this in conjunction with a Function so wondering if I'm not placing the loop in the correct place. I've tried putting it into the actual function since that's where the -1 would be entered but it didn't work. Tried placing it outside first thing in main, that didn't work. Put it in both places (had success with another program doing it that way) but that also didn't work. When I enter -1, I just get the message, "Invalid answer, please re-enter."

My basic generic code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void someFunction();

int main()
{

someFunction();

moreFunctions(y);


....

}

void someFunction()
{

while (arrayX[0] != -1)         // this is where I may have the problem.  
                                            // Am I forrmating the array   thing properly?
{

       if (arrayX[0] == -1)
       cout << "goodbye.";

cout << " Do this...";
cin >> arrayX[0];

if (hw1Array[0] <= 0 || hw1Array[0] >= 100)
{
cout << "Invalid answer.  Please re-enter." ;
}

}

Last edited on
I really don't understand what you are trying to do.

You really need to explain what it is you are trying to do and also post your whole program.

I can see problems with your function though.

The program will only enter the while loop if arrayX[0] is not equal to zero. So why have:

1
2
if (arrayX[0] == -1)
       cout << "goodbye.";


as arrayX[0] can never be -1 at this point of the program.

Could someone please explain the best way to get a program to quit when a user enters -1?


OP: That was your question in post 1, but now you are talking about functions, and assigning values to arrays.. what is you want exactly?


Yet you also said syntatically.

@dts: Yes, of course they are allowed. Would I hire someone who in an interview described while(true) as "completely fine" to use? Probably not :) but as i said, different people have different options.
Hire me Mutexe and I promise I won't do that :P
LOL :)

OP, something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{  
	int value(-2);

	do
	{
		std::cout << "Enter a number: ";
		std::cin >> value;

                // validate user input (check it's a number and within bounds etc etc)
		// Do some stuff here
		std::cout << "NUMBER: " << value << std::endl;

	}while(value != -1);


	std::cout<<"User entered -1. Program ended..\n";

	return 0;
}
OK, figured it out. Thanks.
Last edited on
sigh. i give up.
Topic archived. No new replies allowed.