Need help solving my for loop errors

I am creating a simple program to determine the largest and smallest number of a set of numbers entered by the user. So far this is what I have come up with. But upon testing I am getting a errors. Please help me fix my errors.

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

using namespace std;

int main()
{

	int largest, smallest, num1, num2;

	cout << "How many numbers do you want to enter " << endl;
	cin >> num1;



	for (int i = 1; i < num1; i++) // begining of for loop
	{
		cout << "Enter a number " << endl;
		cin >> num2;


		if (num2 > num1)
		{
			largest = num2;
			cout << "Enter a number " << endl;

		}
		
		if (num2 < num1);
		{
			smallest = num2;
			cout << "Enter a number " << endl;
			cin >> num1;
		}
		



		cout << "Your largest number is " << largest << endl;
		cout << "Your smallest number is " << smallest << endl;
		


	}

	return 0;
}
what errors are you getting ?
simply push the number in a vector , then sort it , then print the first and the last.
"The variable 'largest' is being used without being initialized"
There are several errors and problems in the program.

You should read this:

http://www.programming4beginners.com/tutorial/chapter08/minimum-value-algorithm
Last edited on
thank you for the article it helped. But im still unsure how to incorporate finding the greatest value into it,
Get rid of line 32

you don't need a ; on line 28

line 15 should be
for (int i = 0; i < num1; i++)

initialize your variables
smallest=2147483647, the rest =0

line 21 should be
if (num2 > largest)

line 28 should be
if (num2 < smallest)


Topic archived. No new replies allowed.