For loops not printing

I'm making a simple program that will use a for loop to print numbers in between the two variables, after the program asks the user to input two numbers, it won't print anything out. I don't know what my problem is.

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
  // Exercises 3.1.1 - 3.1.2

#include <iostream>

using namespace std;

int main()
{
    int n1, n2;
    
    while(true)
    {
        //get user input for n1
        cout << "Enter a number: " << endl;
        cin >> n1;

        //get user input for n2
        cout << "Enter another number: " << endl;
        cin >> n2;

        //checks which loop to run then prints out the numbers in between n1 and n2
        if(n1 > n2)
        {
            for(int i = n1; i <= n2; i++)
            {
                cout << i << " ";
            }
            break; //stops the loop
        }
        else if(n1 < n2)
        {
            for(int i = n1; i >= n2; i--)
            {
                cout << i << " ";
            }
            break; //stops the loop
        }
        else
        {
            cout << "Invalid input, numbers can not be the same" << endl;
        }
    }

    return 0;

}
1
2
3
if(n1 > n2)
{
      for(int i = n1; i <= n2; i++)


If n1 is greater than n2, and i starts at n1 - i can never be less than n2. I think you just reversed either the if condition or the for loop from what you intended. Similar situation on the else if part.
Thank you, I don't even know how I made that mistake.
Topic archived. No new replies allowed.