Simple logic but getting errors?

The goal is to print N to N1 in descending order. I've did this b4 but now i'm getting runtime errors..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main() {
int i = 1, n;
int n1;
// Get number from the keyboard and initialize i.
cout << "Enter a number and press ENTER: ";
cin >> n;
cout << "enter another number and press ENTER: ";
cin >> n1;
while (n1 > n) {
cout << n1 << " " << endl;}
n1--;
system("PAUSE");
return 0;
}


I dont see what I did wrong?
Last edited on
You need to include <stdlib.h> for "System Pause".. also.... your program would go into an infinite loop. You need to include n1-- inside the while loop.
You don't include any check that n, n1 are what they are supposed to be. That is n1 > n at first.

You loop is infinite in this case though because your decreasing statement is outside the loop (line 13).

So you either get no loop at all (case n >= n1)
or an infinite one (case n1>n)

Translate your closing bracket in line 12 to line 13 and place some check on your 2 numbers.

Last edited on
Your code demonstrates that sometimes it is better to use for loop instead of while loop to escape your code error. So instead of

1
2
3
while (n1 > n) {
cout << n1 << " " << endl;}  // <== here your while loop is ended
n1--;


your could write simpler

for ( ; n < n1; n1-- ) cout << n1 << " " << endl;
Topic archived. No new replies allowed.