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

The code you posted cannot display the output posted.

The simple solution is use a FOR loop.

I have compiled the code again, and it does give me the output above!

I have compiled the code again, and it does give me the output above!

So did I and I got

C:\>x
Enter two numbers:
10 20
You entered: 10 and 20!
10

C:\>

Try looking at the code you're compiling and compare it to the posted code.

Using a for loop would be recommended.

Example:

if (i<j) {
      for (i=i, i==j, i++)
std::cout << i << std::endl;

    }

I think...

faizlo
You need to loop the code to increment from the lowerest number to the highest one.

Ummm, Imhiya in your for loop i think you made a mistake.
it should be

for(i=i;i<=j;i++)

:-)

here is the full executable program

nxtym understand what a loop performs, nd if also.....

#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 {
while (i!=j){i=i+1;
if (i<j) 

    {


std::cout <<i<< std::endl;

    }

}

}
return 0;



}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Comments
Do NOT post working answers for questions. You don't get the grade.

It's very simple when you use a for loop just read:

#include<iostream.h>
#include<conio.h>
void main()
{clrscr();           //clear screen
int i,j,a,b,k;
cout<<"enter the 2 nos between which you want the numbers to be displayed\n";
cin>>a>>b; //2 nos bet which other nos will be displayed
if(a==b)
cout<<a;  //only one number
else
if(a!=b)
{
if(a>b)  //keeping the bigger number as b for convenience
{k=a;
a=b;
b=k;
}       //else b is already bigger is obvious
k=a;     //equating k to the smaller number
for(i=0;i<(b-a);i++)
{++k;
cout<<"\n"<<k;
}
}          //it will keeping incrementing k till it is equal 
           //to b,when k=b,loop will be exited
getch();    //to view output(takes in any character)
}
Comments
Do NOT post working answers for questions. You don't get the grade.
This question has already been answered. Start a new discussion instead.