You need to work with loops more.
You're copy/pasting a lot of code and just changing the index each time. If you use a loop instead, you can write the code once and use a variable for the index.
For example... you're doing this:
1 2 3 4 5 6 7 8 9 10 11
|
if (digit[0] == ASCII [0])
a=0;
if (digit[0] == ASCII [1])
a=1;
if (digit[0] == ASCII [2])
a=2;
if (digit[0] == ASCII [3])
a=3;
//...
if (digit[0] == ASCII [9])
a=9;
|
If you use a loop, you can shorten it dramatically:
1 2 3 4 5 6
|
int i; // your loop counter
for(i = 0; i < 10; ++i)
{
if(digit[0] == ASCII[ i ]) // notice here we use 'i' to index ASCII
a = i; // and here to set 'a'
}
|
The idea is that each time the loop runs, 'i' will increment, meaning it'll be 0 the first time, then 1, then 2, etc, etc. So in effect, this little bit of code does the same thing as your code above.
Also note that you can take this even further. Instead of making a, b, c, d, and e different variables, you can make them an array so that you can index them instead of having to reference each variable directly:
1 2 3 4 5
|
// int a, b, c, d, e; // bad
int nums[5]; // better
// a = 0; // bad
nums[0] = 0; // better because now we have an index.
|
An index isn't always better... but in this case it's nice because it means we can put this in another loop. Instead of copy/pasting code for a, b, c, etc... you can put all of that code in another loop (like the above) and use the same code for each var.
But even all of this isn't ideal. Take a look at what you're actually doing here:
1 2 3 4 5 6 7
|
if (digit[0] == ASCII [0]) // note: ASCII[0] == 48
a=0;
if (digit[0] == ASCII [1]) // note: ASCII[1] == 49
a=1;
if (digit[0] == ASCII [1]) // note: ASCII[2] == 50
a=1;
// etc
|
Basically you're converting the following:
48 -> 0
49 -> 1
50 -> 2
51 -> 3
52 -> 4
etc
Do you really need if for that? You should see a very simple pattern there. You don't need an 'if' statement at all for this.