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.
//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!
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.
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.
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.
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."
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." ;
}
}
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.
#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;
}