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.
// Exercises 3.1.1 - 3.1.2
#include <iostream>
usingnamespace 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
}
elseif(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;
}
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.