For Loop Help

Hey guys,

I do not know how I go about doing things after testing out things with this code. If you guys could help me out, I would gladly appreciate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int number, large = 0, small = 0;
	cout << "How many numbers do you want to enter" << endl;
	cin >> number;

	for (int i = 0; i < number; i++)
	{
		cout << "Enter a number" << endl;
		cin >> large >> small;
	}

	cout << "The largest number was " << large << endl;
	cout << "The smallest number was " << small << endl;

	return 0;
}


***Edit**

The output program should look like this:
How many numbers do you want to enter
5
Enter a number
3
Enter a number
7
Enter a number
1
Enter a number
12
Enter a number
55
The largest number was 55
The smallest number was 1
Press any key to continue . . .


-- I apologize I forgot to include this.
Last edited on
Create 2 variables to keep track of the smallest and biggest number, and replace if the number you entered is bigger
1
2
3
4
5
6
if(num1< large){
   num1= large;
}
if(num2>small){
  num2 = small;
}
Last edited on
Your simple program will not work as it appears to be intended to work.

You do not need the <string> include as you are not using any std::strings, this is harmless though.


Now this will most likely be overkill and fly over your head, but this is an example of what I think you're trying to do.

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>
#include <limits>
int main()
{
  int number = 0;
  // lowest possible number a 32-bit signed int can hold
  int max_number = -2147483648;

  // largest possible it can hold
  int min_number = 2147483647;

  std::cout << "How many numbers to test?" << std::endl;
  std::cin >> number;

  // while the input is incorrect, we try again.
  // if its correct the next part is skipped
  while((!std::cin) || (number < 1))
  {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Invalid entry... try again." << std::endl;
    std::cin >> number;
  }

  // clean up input stream errors  
  std::cin.clear();
  
  std::cout << "Please enter " << number << " numbers, each one proceeded by the the enter key..." << std::endl;
  
  // temporary variable to store input in until we can test it against
  // min and max numbers
  int tmp;
  for(int i=0;i<number;++i)
  {
    std::cin >> tmp;

    // error check like before
    while(!std::cin)
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      std::cout << "Invalid entry... try again." << std::endl;
      std::cin >> tmp;
    }
    
    // we must test if input is greater than max_number
    // max_number starts out at the lowest possible value it can hold
    // so no matter what, any tested numbers will put it to a sane value
    if(max_number < tmp)
      max_number = tmp;

   // same thing for min_number, just the other way around
    if(min_number > tmp)
      min_number = tmp;
  }

  std::cout << "Largest entered number was: " << max_number
            << "\nSmallest entered number was: " << min_number << std::endl; 
  return 0; 
}
Last edited on
@Hengry How would I fix my code with variables you gave. Sorry, I'm pretty new and don't quite understand c++ yet.

@Hydranix Your code worked completely fine; however, I think that it's too complicated for me. I don't even understand some of those. I appreciate the work, though. Is there like a simpler way of doing it?
Hi

I'm a beginner also and have just found this site, so this was a good problem to look at at. I came up with this:

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

int main()
{
    int totalNumber, large = 0, small = 0, number;
    cout << "How many numbers do you want to enter" << endl;
    cin >> totalNumber;
    
    for (int i = 0; i < totalNumber; i++)
    {
        cout << "Enter a number" << endl;
        cin >> number;
        
            if (small == 0)
            {
                small = number;
            }
            if (number < small)
            {
                small = number;
            }
            if (number > large)
            {
                large = number;
            }
        
    }
    
    cout << "The largest number was " << large << endl;
    cout << "The smallest number was " << small << endl;
    
    return 0;
}

I just added a couple of extra lines to your code, changed the first variable to totalNumber (it made more sense to me) then added 'number' as the current input number. I added some If statements to check to see if the number was larger or smaller than the one before and replace if so.

It works, however I'm three days into my C++ journey, so I imagine there is a much better and more structured way of doing it.

I guess an array could have been set up, all the numbers put in then the array iterated to check for the largest and smallest but it seemed a bit cumbersome.
Last edited on
@SaladDodger your solution doesn't handle negative values properly:
How many numbers do you want to enter
2
Enter a number
-50
Enter a number
-100
The largest number was 0
The smallest number was -100

I rewrote your code. Now I believe the code is better.

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>
#include <string>
#include <limits> // special library that includes the numeric_limits functions you will see below

using namespace std;

int main()
{
	int quantity = 0; // The quantity of numbers you want to enter is initialized with "0"
	int number = 0; // number is initialized with "0"
	int large = numeric_limits <int>::min(); // large gets the smallest integer value
	int small = numeric_limits <int>::max(); // small gets the largest integer
	cout << "How many numbers do you want to enter" << endl;
	cin >> quantity;

	for (int i = 0; i < quantity; i++)
	{
		cout << "Enter a number" << endl;
		cin >> number;

		if (number<=small) // if the number is smaller than the "smallest" number...
			small = number;
		if (number>=large)
			large = number; // if the number is greater than the "largest" number...
	}

	cout << "The largest number was " << large << endl;
	cout << "The smallest number was " << small << endl;

	return 0;
}

Last edited on
@ufrnkiddo: I think your method is a little extreme. The first time you set small and large can be immediately after reading the first number and you can set them to that first number. How do you know it's the first number? Test for ( i == 0 ) inside the loop, immediately after reading the number.
Last edited on
@Chervil , just goes to show that I've got a way to go yet!

I must confess I never gave any thought to negative values, but I see what you mean now. I'll have another go and try and amend it.
Just an opinion, I'd consider ufrnkiddo's approach quite reasonable. Checking for the first loop iteration is also ok, I don't have a strong preference between the two, though adding an extra if statement, somehow seems less aesthetically satisfying, even if the effect on efficiency is unimportant.
Topic archived. No new replies allowed.