//this series of if/else statements allows the //program to ignore leading zeros.



Just wondering some different ways to do this code without else if statements?



//this series of if/else statements allows the
//program to ignore leading zeros.
if (number < 10)
count = 5;
else if (number < 100)
count = 4;
else if (number < 1000)
count = 3;
else if (number < 10000)
count = 2;
else if (number < 100000)
count = 1;
else if (number < 1000000)
count = 0;
Here's a way, ugly, but works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
int main()
{
	int number = 150;
	int count;
	number < 10 ? count = 5:
	(
		number < 100 ? count = 4:
		(
			number < 1000 ? count = 3:
			(
				number < 10000 ? count = 2:
				(
					number < 100000 ? count = 1:
					(
						number < 1000000 ? count = 0:0
					)
				)
			)
		)
	);
	cout << count;
	while(1);
	return 0;
}
Topic archived. No new replies allowed.