While Loop

Okay, so im trying to get my code so that it would keep asking for numbers with the while loop until I input a negative number. Then it will output the lowest of all of the numbers inputed. I messed up somewhere but I just don't know how to fix it.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 #include <iostream>

using namespace std;

int main()
{
	int var;
	
	cout << " This program will find out the minimum positive value entered " << endl;
	cout << " Entering a negative number will stop the imput" << endl;
	cout << " Please enter a positive number (negative number to quit): " << endl;
	cin >> var;



	bool first = true;
	while (var > 0)
	{
		cout << " Please enter another positive number (negative number to quit): " << endl;
		cin >> var;
		if (var > 0)
		{
			

			bool first = false;

		}
		else if (var == 0)
		{

			cout << " Error! 0 is not a valid imput " << endl;

			bool first = false;
		}
		else if (var < 0)
		{
			bool first = true;


		}
	}
		
		if (first = true)
		{
			cout << "no valid numbers were input so there is not a minimum value. " << endl;

		}
		else
			cout << var << endl;
	

	






	return 0;
}


any ideas? Honestly im lost.

NOTE: this is unfinished
Last edited on
Let's look at lines 21 - 27:

1
2
3
4
5
6
7
if (var > 0)
		{
			

			bool first = false;

		}


You're creating a new variable called first, which is not the same as referring to the other first created on line 16. This new variable has a more localized scope, and will cease to exist outside of that if branch. The same is true for lines 28 - 34 and 35 - 40 and the variables created in those branches.

Let's look at line 43:

if (first = true)

This is not doing what you think it's doing. Remember, = is assignment, == is the binary equality operator.

On line 31, you print an error message if the user enters zero. If you've decided that zero is an invalid input, then you need to make sure that this is also accounted for before / when you enter the while loop. If the first value you enter is a zero, your program will happily accept it.

You'll also need a way to store the values that were entered. An array or a vector maybe.
Last edited on
Topic archived. No new replies allowed.