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 etered: " << 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;
}
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?
I would nest the if statement in {} brackets, not sure if that matters, probably me being OCD
Anyway, you cannot be certain that the first number is smaller that the second number entered, so i would do something like this..
int x, y;
if (i==j)
{
std::cout << "They are equal" << std::endl;
}
else {
if( i > j)
{
x = j;
y = i;
}
else {
x = i;
y = j;
}
//now you x and y (x being the smallest number)
for(x; x < y; x++)
{
std::cout << x << std::endl;
}
code might not be 100% but you should get the idea, will make life easier knowing the smallest number
Here is what I tried. Since I have two numbers and better than speculating which is which, I tried this:
#include <iostream>
int main()
{
std::cout << "Please specify two natural numbers:" << std::endl;
int i, j, k;
std::cin >> i >> j;
if (i >= j)
{
k = j;
j = i;
i = k;
}
for (int x = i; x <= j; ++x) std::cout << x << " ";
std::cout << std::endl;
return 0;
}
Still, I do not know why my second posted code (the one with if ( i==j ) ) doesn't work?
No matter what the two numbers are, it prints the message that the two numbers are equal!
Corona688, I have compiled it again, and I tried it again, as in post 3, here is what I got:
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 etered: 10 and 20!
10
12
14
16
18
faizlo@faizlo-laptop:~/C++_tut$
If you want to print all the numbers between the first and second including the first and second the following will work
#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 {
for (; i <= j; i++) {
std::cout << i << std::endl;
}
}
return 0;
}
If you want to print all the numbers but exclude the first and second:
#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 {
for (++i; i < j; i++) {
std::cout << i << std::endl;
}
}
return 0;
}
If you want to handle the case where the first number inputted is greater than the second number:
#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) {
int t = i;
i = j;
j = t;
}
for (++i; i < j; i++) {
std::cout << i << std::endl;
}
}
return 0;
}