i want to my while loop stop after my function bool return false
i want to my while loop stop after my function bool return false
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 34 35 36 37 38 39
|
#include <iostream>
using namespace std;
bool numbers()
{
cout << "guess a number:\n";
int number = 5;
int a;
cin >> a;
if(a == 5)
{
cout << "correct!\n";
return true;
}
else
{
cout << "incorrect...try again\n";
return false;
}
}
\\(here is problem, how can i explain to while)
int main()
{
while (true)
{
numbers();
}
return 0;
}
|
If your function returns a value, then store that value and use it in the condition of the loop.
1 2 3 4
|
int main()
{
while( numbers() ) ; // keep calling the function till it returns false
}
|
thanks alot but i have to change true and false
1 2 3 4
|
int main()
{
while( !numbers() ) ; // keep calling the function till it returns true
}
|
Topic archived. No new replies allowed.