Not sure if this is right.

closed account (yR9wb7Xj)
Not sure if I'm heading in the right direction. I just attempted to do the first part, I found this article here on the forums for beginners and I'm giving it a shot. I am also not sure how to approach the second part or third part.

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 ask the user to enter any number other than 5 until the user enters the number 5. Then tell the user "Hey, you weren't suppose to enter 5!" and exit the program.

* Modify the program so that after 10 iterations if the user still hasn't entered 5 it 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" 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
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main() {

	int num;



	do{
		cout << "Enter any number other than 5." << endl;
		cin >> num;


	}while(num!=5);
if(num !=5)
{
	cout << "Good job!" << endl;
}
else
	cout << "Hey you weren't suppose to enter 5!" << endl;

	return 0;
}
Last edited on
I'm sure there are simpler ways to do this. But here... I kinda went straight for the third part.

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
#include <iostream>

int main()
{
	int num, i = 0;
	bool isTen = false;
	
	do
	{
		std::cout << "Enter any number other than " << i++ << ": ";
		std::cin >> num;
		
		if (i == 11)
		{
			isTen = true;
			break;
		}
	} while (num != i - 1);
	
	if (isTen == true)
	{
		std::cout << "Wow, you are more patient than I am. You win.";
	}
	else
	{
		std::cout << "Hey, you weren't supposed to enter " << i - 1 << ".";
	}
	
	return 0;
}
Last edited on
closed account (yR9wb7Xj)
I'm a little confuse in your code. Why did you use a boolean bool isTen = false; in order to write this program? I also see you used a counter as i = 0;. From my understanding, you're telling the user to input a number based on the cout statement. Why are you also outputting this --->i++ in your cout statement line 10. Actually I'm lost throughout your code. Could you please explain a little more what each line is doing, and as to why you did this, what was the reason behind it etc. I know what do while does and if and else, but why did you do it that way in your conditions.
Last edited on
Ahh, sorry. Hmm...
bool isTen = false Boolean variables can only take two values - true or false.
So with that we can check if the question has been asked 10 times.

with if (i == 11) { isTen = true; break; } we check if i has reached "10". We're actually comparing to"11" because i increments right after we input.

Incrementing is when you increase the value of a variable by 1. For example i = i + 1;. That is exactly what i++; is. i++ is the same as i = i + 1;

That is also the reason why the while condition is num != i - 1;, because the program will output, for example, Enter any number other than 3: and right after that i will be incremented, it will become 4 (not 3 anymore). So we have to compare our input to i-1 in order for the loop to break;. break; basically means "stop the loop and get out".

So yes, when we don't enter the same number as i (the one that was asked not to enter) for ten times, i will become equal to 11 and the boolean variable isTen will become true and we will break out of the while loop.

So then we have the if..else statement. If (this is true) then execute... else (if it's not true) execute this. That's basically the logic behind it.

So if isTen = true; then it will print "Wow, you are more patient than I am. You win.

But if isTen = false; (which means it isn't true), then else will execute and "Hey, you weren't supposed to enter i-1 (which is the number i has reached before the while loop was breaked).

I tried to explain as best as I can. I hope you understand better now :)
closed account (yR9wb7Xj)
Thank you this really did help.
Haha, good, because I don't really understand my explanations lol Glad you did
closed account (yR9wb7Xj)
Hey Sasauke, can you enable your private messages on? I would like to send you a pm.
I didn't know there were private messages lol Yeah, I did it
closed account (yR9wb7Xj)
Oh, lol yeah I just sent you a private message.
Here is a much simpler solution to the same problem:

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
#include <iostream>

int main()
{
	int num;
	
	for (int i=0; i<=10; i++)
	{
		std::cout << "Enter any number other than " << i << ": ";
		std::cin >> num;
		
		if (num == i)
		{
			std::cout << "Hey, you weren't supposed to enter " << i << ".";
			break;
		}
	
		if (i == 10)
		{
			std::cout << "Wow, you are more patient than I am. You win.";
			break;
		}
	}
	
	return 0;
}
closed account (yR9wb7Xj)
Thank you! This is a lot easier to understand what is going on. Good to know there is more than one way to do this!
This is also a simpler solution

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 num;
	
	for (int i=0; i<=10; i++)
	{
		std::cout << "Enter any number other than " << i << ": ";
		std::cin >> num;
		
		if (num == i)
		{
			std::cout << "Hey, you weren't supposed to enter " << i << ".";
			return 0 ; 
		}
	
	}
	
	std::cout << "Wow, you are more patient than I am. You win.";
	return 0;
}
closed account (yR9wb7Xj)
Thanks obscure!
Topic archived. No new replies allowed.