Problem in Output (do-while loop, printing series of numbers)

Here's the question,
Use while or do-while loop (you will ask user for the value till which he wants to print the series).
- To display the numbers 9, 98, 997, …

So, for this program, I made the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
using namespace std;
int main()

{
	long int a, b = 1, c = 10;
	cout << "Enter number until when you want the series: ";
	cin >> a;
	do
	{
		if (a == 0)
			break; 
		cout << c - b << ", ";
		c = c * 10;
		b++;
	} while (b <= a);
	
	cout << endl;
	system("pause");
}



However, I am facing one problem in it.. Program runs fine, but only if user entered value is 9 or less than 9.. For example,
On entering 9 as input, it gives
9, 98, 997, 9996, 99995, 999994, 9999993, 99999992, 999999991,

However, if I enter any number bigger than 9, it starts showing abnormal outputs.. Like, ?
On entering 10 as input, it gives
9, 98, 997, 9996, 99995, 999994, 9999993, 99999992, 999999991, 1410065398,

Or on entering 15, it gives,
9, 98, 997, 9996, 99995, 999994, 9999993, 99999992, 999999991, 1410065398, 1215752181, -727379980, 1316134899, 276447218, -1530494991,


Can anyone explain this abnormal behavior of the program and what should I do to fix this up? Thank you!
What operating system and compiler version are you using? 32 or 64 bit?

It looks like your overflowing the variable, remember all integral numbers have an upper and lower limit.

If you want higher values I suggest you consider unsigned long long. By the way your present program doesn't overflow until about 19 when using the online compiler (the gear icon to the right of the code window).

It is a 64 bit Windows 10, with x64-based processor. As of compiler, I am not sure which one is it... I am actually using Visual Studio 2017 version but IDK which compiler of C++ does it have..

Yes, thank you for the answer, it definitely was overflowing the variable.. Using unsigned long long int, I got it to work until 19 in my compiler as well. Tried the similar with the compiler here as well but the result still remained good until 19th number :P
Anyway, it's confirmed then that the problem is not with my compiler but it is because of the max value of int.. Thanks for the help :)
Topic archived. No new replies allowed.