Hi,
I am doing a C++ self-study and I got stuck with this problem.
I want to have a code that asks the suer to enter two numbers and then it lists the numbers between these two numbers. It has also to print a message if these two numbers are equal.
Here is what I wrote:
#include <iostream>
int main()
{
int i, j;
std::cout << "Enter two numbers:" << std::endl;
std::cin >> i >> j;
std::cout << "You entered: " << i << " and " << j << "!" << std::endl;
if (i==j)
std::cout << "They are equal" << std::endl;
else {
if (i<j) {
std::cout << i << std::endl;
++i;
}
else if (i>j) {
std::cout << j << std::endl;
j++;
}
}
return 0;
}
Here is the output:
faizlo@faizlo-laptop:~/C++_tut$ ./range
Enter two numbers:
10
12
You etered: 10 and 12!
10
faizlo@faizlo-laptop:~/C++_tut$ ./range
Enter two numbers:
10
20
You entered: 10 and 20!
10
12
14
16
18
faizlo@faizlo-laptop:~/C++_tut$
Why do I get this? I mean I do not get all the numbers in the range specified by the two numbers initially given to the code?
I have also tried many alterations using while and for statements. My question is: Why it does stuck at the first if statement, and then quits?
Thanks,
faizlo