# Thread: Sum letters in written numbers from 1 to 2008

1. ## Sum letters in written numbers from 1 to 2008

Hey there. Haven't been coding in a while, but I am doing challenges on DareYourMind.net and can't figure why I do not get the correct answer. I've picked quite a few numbers (~20-30) and calculated by hand the results and I get the same as the output from my program.

Basically, I have to input the total number of letters of all the written numbers from 1 to 2008.

Here's what I have coded, I commented it as much as possible for you guys:
Code:
```#include <iostream>

int main()
{
int total = 0, current = 0, temp = 0;

for(int i = 1; i < 2009; i++)
{
current = 0;

// if Xxxx is non-zero, then must add "one/two thousand"
if(i / 1000) current += 11;

// if xXxx is non-zero, then must add "one...nine"
// and if xxXX is non-zero, then must add "hundred and"
if(i / 100 && ((i / 100) % 10))
{
if(i % 100 == 0) current += 7; // hundred
else current += 10; // hundred and

temp = (i / 100) % 10;
switch(temp)
{
case 1: case 2: case 6: current += 3; break;
case 3: case 7: case 8: current += 5; break;
case 4: case 5: case 9: current += 4; break;
}
}

// if xxXx is non-zero, then add "ten...ninety"
// separate case for numbers between 11 and 19 for "eleven...nineteen"
if((i / 10) && ((i / 10) % 10))
{
temp = i % 100;
if(temp > 10 && temp < 20)
{
switch(temp)
{
case 11: case 12: current += 6; break;
case 16: case 15: current += 7; break;
case 17: current += 9; break;
case 13: case 18: case 14: case 19: current += 8; break;
}
}
else
{
temp = (i / 10) % 10;
switch(temp)
{
case 1: current += 3; break;
case 2: case 3: case 4: case 8: case 9: current += 6; break;
case 5: case 6: current += 5; break;
case 7: current += 7; break;
}
}
}

// make sure we're not in the "eleven...nineteen" range
// and xxxX is non-zero, then add "one...nine"
if((((i % 100) < 10) || ((i % 100) > 20)) && !(i % 10))
{
temp = i % 10;
switch(temp)
{
case 1: case 2: case 6: current += 3; break;
case 3: case 7: case 8: current += 5; break;
case 4: case 5: case 9: current += 4; break;
}
}
std::cout << i << " : " << current << "\t";
total += current;
}
std::cout << std::endl << total;
}```
Any help is much welcome !

Thanks (: 2. If you get the same result by hand as you get from your program, what's the problem? 3. I'm sorry I wasn't clear. I have tested my answer on the site's challenge verification page and it does not validate hence I have to be wrong somewhere but I have looked at it and can't find my mistake =/

I can link to the challenge if needed. 4. Originally Posted by Desolation I can link to the challenge if needed.

edit: You seem to be doing nothing for the numbers 1-9, or for the units column in numbers > 20.
Do blank spaces count?
Is it necessarily "hundred and"? (I, for example, would say "one hundred five" or "one hundred twenty four", not "one hundred and ...") 5. Here's the link to the challenge:
Hacker-Challenge | Dare Your Mind :: Challenges :: ONE plus TWO is 6

As for your comment, I will check this right now, thanks.

Edit: Only letters count (dashes and spaces don't). 6. I will try without "hundred and". English is not my native language so I thought the "and" was compulsory.

As for your first suggestion, it is dealt with in this part of the code:
Code:
```        // make sure we're not in the "eleven...nineteen" range
// and xxxX is non-zero, then add "one...nine"
if(((i % 100) < 10) || ((i % 100) > 20)))
{
temp = i % 10;
switch(temp)
{
case 1: case 2: case 6: current += 3; break;
case 3: case 7: case 8: current += 5; break;
case 4: case 5: case 9: current += 4; break;
}
}```
Basically it extracts the last two digits on the right and check wether they are between 10 and 20. If it is not and the last digit is non-zero then we add the number of letters contained in each written number.

I have double-checked all hard-coded values (i.e. "magic numbers").

Edit: Removed that last bit because it produced weird results. Sorry about that.

Edit: I have tried without "hundred and" and I still get the wrong output. 7. I looked at the challenge and they seem to want "hundred and ..." so that part's OK. But in the code you posted in #1, for the last if statement you have
Code:
`    if ((((i % 100) < 10) || ((i % 100) > 20)) && !(i % 10))`
That last term should not have "!". 8. I have rewritten it from scratch so that it is easier to spot mistakes. However, I get the exact same result and this time I'm pretty sure there is no possible mistake as the output seems correct to me. Here's the new code:
Code:
```#include <iostream>
#include <string>

int main()
{
std::string thousand = "thousand";
std::string hundreds[] = {"hundred", "hundredand"};
std::string tens[] = {"ten", "twenty", "thirty", "fourty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
std::string eton[] = {"eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
std::string units[] = {"one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};

int total = 0;
std::string tmp = "";
for(int i = 1; i < 2009; i++)
{
tmp = "";
if(i / 1000)
{
if((i/1000)%10 == 1) tmp += units;
else tmp += units;
tmp += thousand;
}

if(i / 100 && ((i/100)%10))
{
tmp += units[((i/100) % 10)-1];
if(i%100) tmp += hundreds;
else tmp += hundreds;
}

if(i / 10 && ((i/10) %10))
{
if((i % 100) <= 10 || (i % 100) >= 20)
tmp += tens[((i / 10) % 10)-1];
else
tmp += eton[(i%10)-1];
}
if(i % 10 && ((i % 100) <= 10 || (i % 100) >= 20))
tmp += units[(i%10)-1];

total += tmp.size();
std::cout << tmp << " : " << tmp.size() << "\n";
}
std::cout << std::endl << total;
}```
And I have posted the output on here because it is pretty large. 9. I've noticed there is no defined output for the challenge, so you may just be getting marked unfairly.

Try the simplest output:
Code:
`std::cout << tmp.size() << std::endl;`
I also think you misunderstand the goal. Aren't you supposed to sum the lengths of every written number and get one giant number? 10. The challenge does not validate code, it valides user input (i.e. you need to type in the answer). This number is summed in the "total" variable which is done right above the line you quoted and is outputted after the loop. 11. I made a small change to the output so that the digit representation is shown as well.

new output | Copy Paste Code

Edit: I have corrected a spelling mistake ("fourty" instead of "forty") but I still get the wrong output. Meh. 12. I'm saying that the judge will only look at a number and if you output too much you will be marked wrong. So make your only output the plain answer. If that doesn't work we know something else is wrong. 13. No offense, but have you actually looked at the link I have posted ?

The output is not judged. The sum of all written numbers is what is judged and it is the only information available to the verification process on the website. They will never ask for my code, for an executable file or for a specific output layout. It is a plain textbox which requires keyboard input (the sum of all written numbers). Popular pages Recent additions 