Write a program that ccontinues t

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)

Write a program that ccontinues 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
16
17
18
  #include<iostream>
using namespace std;

int main()
{
	int num;
	cout << "Enter any number other than 5: " << num;
	for (num = 0; num <= 10; num++){
		cout << "Yay! You're more patient than I am! U " << num;
		if (num == 5)
			cout << "Hey! You're not supposed to enter 5!";
		break;
	}

	system("pause>0");
	return 0;
}

MY CODE
1
2
3
4
5
6
7
8
9
int num;
	cout << "Enter any number other than 5: ";
	cin >> num;
	for (num = 0; num <= 10; num++){
		if (num == 5)
			cout << "Hey! You're not supposed to enter 5!";
	}
	system("pause>0");
	return 0;

UPDATE
no.
you need to get out of the loop if the user presses 5. Currently it looks like even if the user presses five on the first iteration it'll carry on prompting the user for numbers, rather than exit the program.
Either use 'break' or have a boolean flag you can set when 5 is pressed that gets out of the loop.

for the second part have an int set to 1, and increment this on each iteration, testing against this value. That's if i've understood what you've written correctly.

and line 4 should be:
 
for (num = 0; num < 10; num++){
Last edited on
The output is:
Enter any number other than 5: 1
Hey! You're not supposed to enter 5!

//and when you clicked another number it exists
//it's not loop
//i crei everteim
that makes no sense to me.
what's your current code?


ahh i've just noticed.. you need to prompt the user INSIDE your loop.
At the moment, you are only asking them once.
ohh ok let me try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	int num;
	for (num = 0; num <= 10; num++){
		cout << "Enter any number other than 5: ";
		cin >> num;
		if (num == 5)
			cout << "Hey! You're not supposed to enter 5!";
		break;
	}
	
	system("pause>0");
	return 0;
}

still the same :v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	int num;
	for (int a = 0; a <= 10; a++){
		cout << "Enter any number other than 5: ";
		cin >> num;
		if (num == 5)
			cout << "Hey! You're not supposed to enter 5!";
		break;
	}
	
	system("pause>0");
	return 0;
}

UPDATE
it is good habit to use braces for if and else's..
and read my comment about line 4 too. You are prompting the user 11 times, not 10.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	int num;
	OUTER:
	for (int a = 1; a <= 10; a++){
		cout << "Enter any number other than 5: ";
		cin >> num;
	INNER:
		if (num == 5)
		{
			cout << "Hey! You're not supposed to enter 5!";
		break;
	}

//error
wtf?

i just meant this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	int num;
	for (int a = 0; a < 10; a++){   // <- i have removed the '=' so 10 iterations now
		cout << "Enter any number other than 5: ";
		cin >> num;
		if (num == 5)
		{  // <-- IMPORTANT
			cout << "Hey! You're not supposed to enter 5!";
			break;
		}
	}

	system("pause>0");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
int num;
	for (int a = 0; a < 10; a++){ 
		cout << "Enter any number other than 5: ";
		cin >> num;
		cout << "Yay! You're more patient than I am! You win!";
		if (num == 5)
		{ 
			cout << "Hey! You're not supposed to enter 5!";
			break;
		}
	}

	cout << "Yay!You're more patient than I am! You win!";//how to put this? 
omg i get it now.. THANK YOU!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
	int num;
	for (int a = 0; a < 10; a++){ 
		cout << "Enter any number other than 5: ";
		cin >> num;
		if (num == 5)
		{ 
			cout << "Hey! You're not supposed to enter 5!";
			break;
		}
	}

	cout << "Yay!You're more patient than I am! You win!";

	system("pause>0");
	return 0;
}
Nice one.
By the way, even if your 'if' and 'esle''s only have one line I would still enclose the statement in braces, just so it's clear what is part of the if and what is not.
Last edited on
ok thank you again ^_^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int num;
	for (int a = 0; a < 10; a++){
		cout << "Enter any number other than 5: ";
		cin >> num;
		if (num == 5)
		{
			cout << "Hey! You're not supposed to enter 5!";
			break;
		}
	if (a == 9)
		cout << "Yay!You're more patient than I am! You win!";
		}
	system("pause>0");
	return 0;
}

//this is the right code... ;)
not really no. that last if test is not needed, and cout can be moved outside.
i just tried the previous and it's fail so we need the if sign :P
Topic archived. No new replies allowed.