While( user == gullible )

I was just wondering is this a good way of writing the code for this exercise:
While( user == gullible )
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)

Write a program that continues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.

★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.

★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int num = 0;
	int count = 0;

	while (num != -99)
	{

		cout << "Please enter a number other then " << count << ':';
		cin >> num;

		if (num == count){
			cout << "You were not suppose to enter that number!\n";
			break;
		}
		count++;
	}
I dont really like how i used the while (num!= -99) in order to run the loop. Is there a better way of doing this?

1
2
3
4
5
6
7
8
9
int num = 0, count = 0 ;

do
{
    cout << "Please enter a number other than " << count << ": " ;
    cin >> num ;
} while ( num != count++ ) ;

cout << "You were not supposed to enter that number!\n" ;
int num = 0, count = 0 ;

do
{
cout << "Please enter a number other than " << count << ": " ; right here count val is 0
cin >> num ; i enter 0
} while ( num != count++ ) ; so num is 0 and count++ will be 1. 0 is != 1 which is true so isnt it suppose to run again? Someone please explain since im quite sure im not understanding something




cout << "You were not supposed to enter that number!\n" ;
When count is 0, count++ evaluates to the value before the increment. So, it evaluates to 0, but count is then equal to 1.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// http://ideone.com/aJJv72
#include <iostream>

int main()
{
    int count = 0;
    std::cout << "postfix\n";
    std::cout << count++ << '\n';    // postfix increment
    std::cout << count << '\n';

    count = 0;
    std::cout << "prefix\n";
    std::cout << ++count << '\n';     // prefix inrement
    std::cout << count << '\n';
}
Last edited on
Topic archived. No new replies allowed.