For Loop help please

Hello I'm suppose to write a program using (for loop) that asks the user to enter any amount of numbers, so that it can display the smallest and largest. My program successfully finds the largest, but it is always displaying 0 for the smallest, I think Im doing something wrong with the internalization but I dont know what to change it to, would appreciate any help

this is what I have ....


#include <iostream>


using namespace std;

int main()
{


int amount;
int count;
int number = 0;
int smallest = 0;
int largest = 0;

cout << "Enter total numbers to process: ";
cin >> amount;

for(count = 1; count <= amount; count++)
{



cout << "Enter number: ";

cin >> number;




if(number > largest)
{

largest = number;

}


if(number < smallest)
{

smallest = number;






}


}




cout << "The largest integer is " << largest <<endl;

cout << "The smallest integer is " << smallest <<endl;


system ("pause");
return 0;

}
You will have a lot easier time reading and understanding your program if you develop the tao of indentation:

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
#include <iostream>
using namespace std;

int main()
{
  int amount;
  int count;
  int number = 0;
  int smallest = 0;
  int largest = 0;

  cout << "Enter total numbers to process: ";
  cin >> amount;

  for(count = 1; count <= amount; count++)
  {
    cout << "Enter number: ";
    cin >> number;

    if(number > largest)
    {
      largest = number;
    }

    if(number < smallest)
    {
      smallest = number;
    }
  }
  
  cout << "The largest integer is " << largest <<endl;
  cout << "The smallest integer is " << smallest <<endl;

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

(And others will love you more if you use [code] tags.)

Line 9: what is smallest's initial value?

Good luck!
got it, thanks! I will use [code] tags from now on

I have switched
line 9: to int number = 999;

although it does the job at displaying the smallest as long as one of the numbers entered has 3 digits or less , I still dont think Im doing it correct because if all the numbers I enter are more than 3 digits it will display the 999 as the smallest. Is there any way that it can be for any number entered?
I was going to say "think about it", but it would be unfair because you don't know the largest number available.

It is provided in <limits>

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <limits>
using namespace std;

int main()
{
  int amount;
  int count;
  int number = 0;
  int smallest = numeric_limits <int> ::max();  // start with largest value possible
  int largest = numeric_limits <int> ::min();  // start with smallest value possible

Hope this helps.
ok I see. I didn't know about limits, but now I do. Thank you for helping!
Topic archived. No new replies allowed.