For Loops and If Statements

Hello, new to the forum and was wondering if someone would be kind enough to help me.

I need the user to enter 10 numbers. If the numbers they enter are in ascending order (previous number has to be smaller than what the user enters next) it allows the user to keep on going until they enter the 10th number which the output would be a success. If the user enters a number that is smaller than the previous, I need it to break and the output to be failed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(){
	int num;
	int g = -1;
	// Count
	for (int i = 0; i < 10; i++)
	{
		cout << "Please Enter Your Number: ";
		cin >> num;
		// Store
		if (g < num)
		{
			g = num;
			cout << "Success!\n";
		}
		else {
			cout << "Failure!\n";
			break;
		}

	}
}


My problem here is that it keeps saying success after the user presses enter and I need the final number to output success only. Any help would be awesome.
If you don't want the output to occur during the loop, then don't write the output code inside the loop code.
When I take it out of the loop, it says I can't have a break outside of it.

Also, when I took it out of the loop, the program still says success, no matter what numbers I enter.
How did you take it outside of the loop? Do you want me to guess? You need to show your current code.

Your original code works fine for handling the fail case, you only needed to change the code regarding the success case.
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
#include <iostream>
using namespace std;

int main(){
	int num;
	int g = -1;
	// Count
	for (int i = 0; i < 10; i++)
	{      // For Loop Begins
		cout << "Please Enter Your Number: ";
		cin >> num;
	// Store
		if (g < num)
		{
			g = num;
		}
		else {
			cout << "Failure!\n";
			break;
		}

	}     // For Loop Ends

	if (g <= num)
		{
			cout << "Success!\n";
		}
}


This is the closest I have come to getting it to work, but when I type in two of the same name numbers (ex. 10 and 10). I get Success and Fail. I'm pretty sure I mixed up a sign somewhere, but I can't figure out where.
Aren't lines 13 and 24 supposed to be the same? Why did you use <= on line 24?
I've just been trying a variety of things, even when I take out line 24 and just leave cout success, I still get the same problem.
Stop trying random things and think carefully. Think about what line 13 does, and then think about how it relates to line 24. You currently only need to change (not remove) line 24.
Last edited on
I still can't figure it out. Any other hints?
Last edited on
Line 24 should match line 13.
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>
using namespace std;

int main(){
	int num;
	int g = -1;
	// Count
	for (int i = 0; i < 10; i++)
	{      // For Loop Begins
		cout << "Please Enter Your Number: ";
		cin >> num;
		// Store
		if (g < num)
		{
			g = num;
		}
		else {
			cout << "Failure!\n";
			break;
		}

	}     // For Loop Ends

	if (g < num)
	{
		cout << "Success!\n";
	}
	system("PAUSE");
	return 0;
}


I tried that, but it doesn't output success once I actually input 10,20,30....100.
Oh, I misunderstood what you were asking.

You should use a boolean for this. Start with the boolean being true, and when you reach your fail state (lines 17-20) set it to false. After the loop just use it in the if statement (line 24) to know whether you successfully entered all the data.
I haven't learned about boolean yet... I am just curious if you think it is possible to make this without using it?
You can, but it's effectively the same as using a boolean.

A boolean is just a data type that can only either be true or false. You use a boolean every time you write an if statement.

This code:
1
2
if(g < num)
{
is the same as this code:
1
2
3
bool my_boolean = (g < num);
if(my_boolean)
{
Topic archived. No new replies allowed.