need help please.

I have been working on this code for 1 hour and i keep getting double counting. Please help me to check it.

#include <iostream>
using namespace std;

int main()

{

int n1;
int n2;


cout << "Please enter first number : " ;
cin >> n1;
cout << "Pleae enter second number : " ;
cin >>n2;


for (;n1 >= n2; n1--)
{
cout << n1 << endl;
}
for (;n1 <= n2; n1++)
{
cout << n1 << endl;
}
return 0;
}
This is the result i am getting. I should not be getting last 1 and 2.

Please enter first number : 10
Pleae enter second number : 2
10
9
8
7
6
5
4
3
2
1
2

Thank you for your help.



What are you supposed to do then?

n1 must be less than n2 in order for your first loop to stop.
Therefore, n1 == n2-1 when the second loop starts.
The counting suppose to stop at 2 not going to second loop at all. It suppose to execute first loop only when n1 >= n2 and print the results but loop still goes to second loop.
Why do you have the second loop, if you don't want to have it?

What is the real purpose of the program? To count from A to B whether A>B or A<B?
Here's the correct code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

using namespace std;

int main()
{
int n1;
int n2;
cout<<"Enter the first number"<<endl;
cin>>n1;
cout<<"Enter the second number"<<endl;
cin>>n2;
for (int i=n1; i>=n2; i--)
{
cout<< i <<endl;
}
return 0;
}


Also use code tags:
http://www.cplusplus.com/forum/articles/42672/
Last edited on
Thank You for you help. I got it working.
Topic archived. No new replies allowed.