while loop issues

This is some homework I am trying to figure out what I did wrong to be honest I am very new to programing and I am doing a horrible in class and so I am turning here in hopes that someone can point me in the right direction.

the code does fine while I am just entering a positive number it continuously loops as it is supposed to then when I enter a negative number to stop the code other lines pop up that are not supposed to be there and I really can not for the life of me figure out how to get my code to reference the positive number after I have entered a negative number to end the program. I know I have a lot of issues sorry for asking but any help would really make my day Thank you for taking your time to look at 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
61
62
63
64
65
 #include <iostream>

using namespace std;

int main()
{
	bool first = true;
	short minimum;
	short input;

	cout << "This program will find the minimum positive number input. " << endl;
	cout << " Entering a negative number will stop the input." << endl;
	cout << " Please enter a positive number (negative number to quit):" << endl;
	cin >> minimum;
	
	
	
	while (minimum >= 0)
	{
		
		if (first = true)
		{
			if (minimum >= 1)
			{
				cout << " Please enter a positive number (negative number to quit):" << endl;
				cin >> minimum;
				input = minimum;
				first = false;
				
			}
		
			else
				{
				cout << " 0 is not a valid input." << endl;
				cout << " Please enter a positive number (negative number to quit):" << endl;
				cin >> minimum;
				}			
		}		
		else
		{
			if (minimum >= 1)
			{
				if (input < minimum)
				{
					input = minimum;
				}
			}
			else
			{
				cout << " 0 is not a valid input." << endl;
				cout << " Please enter a positive number (negative number to quit):" << endl;
				cin >> minimum;
			}	
		}
	}	
	if (minimum <= -1)
	{
		cout << "No minium number was Input." << endl;
	}
	else
		cout << " the minium number Input was " << input << endl;
		
	
	return 0;
}
You are making things unnecessarily complicated.

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

using namespace std;

int main()
{
	
	int minimum;
	int number;


	cout << "This program will find the minimum positive number input. " << endl;
	cout << " Entering a negative number will stop the input." << endl;
	cout << " Please enter a positive number (negative number to quit):" << endl;
	cin >> number;
	
	minimum = number; // this is our first input, and thus it is, so far, the minimum
	
	while(number > 0) // enter loop if first number entered was positive
	{
	    cin >> number; // get next number
	    
	    if(number <= 0) break; // if next number is negative or 0 , break out of the loop
	    
	    if(number < minimum)   // if the next number entered (positive) is less than the minimum so far
	        minimum = number; // make it the new minimum
	}
	
	cout << "The minimum positive input was: " << minimum << endl;
    
}



This program will find the minimum positive number input. 
 Entering a negative number will stop the input.
 Please enter a positive number (negative number to quit):
5
4
3
7
2
6
3
-5
The minimum positive input was: 2
Last edited on
thank you Arslan7041 it took me some time but I did get it thanks a lot.
Topic archived. No new replies allowed.