I was wondering if I can stop the while loop in the 12th line somehow instead of using break; in the 24th line. I tried :
while(nr != 5 || t == 10)
but it won't stop the program after t == 10, it just keeps going.
Okay, I got the idea what u meant. I have another problem. Every time when I type any number that is higher than the current i i.e i = 4 and I type nr = 5 the code stops.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
int nr, t = 0, i = 0;
do
{
cout << "Type any number that isn't: " << i << endl;
cin >> nr;
if(nr == i) break;
else i++;
}
while(nr != i);
return 0;
}
#include <iostream>
int main()
{
int number ;
for( int cnt = 100 ;
std::cout << "type any number not higher than " << cnt << ": " &&
std::cin >> number && number <= cnt ;
++cnt ) ;
std::cout << "you typed in a wrong number. quitting\n" ;
}
Lines 9 and 10 together form the condition of the for loop.
In English, the condition would read:
if "type any number not higher than ... etc." is printed on stdout (line 9) and
if the user entered a number (first part of line 10) and
if the number is less than or equal to count (second part of line 10)
&& is the logical AND operator.
In effect, come out of the loop if a. the user entered something which is not a number (std::cin >> number failed)
or b. if the number entered was larger than cnt