Solving Cryptarithmetic problems using 'enum'?

Hey guys.

I have a problem in my book "Absolute C++" which I'm having trouble getting past.
Here is the text from the book:

In cryptarithmetic puzzles, mathematical equations are written using letters. Each letter can be a digit from 0 to 9, but no two letters can be the same. Here is a sample problem:
SEND + MORE = MONEY
A solution to the puzzle is S = 9, R = 8, O = 0, M = 1, Y = 2, E = 5, N = 6, D = 7
Write a program that finds solutions to the cryptarithmetic puzzle of:
TOO + TOO + TOO + TOO = GOOD
The simplest technique is to use a nested loop for each unique letter (in this case T, O, G, D). The loops would systematically assign the digits from 0-9 to each letter. For example, it might first try T = 0, O = 0, G = 0, D = 0, then T = 0, O = 0, G =0, D = 1, then T = 0, O = 0, G = 0, D = 2, etc. up to T = 9, O = 9, G = 9, D = 9. In the loop body test that each variable is unique and that the equation is satisfied. Output the values for the letters that satisfy the equation
.

Now, up to this point in the book there is only one way I can think off to solve this problem.is using enumeration to assign values to corresponding letters or strings.

My solution to this, which I thought would be simple enough to implement, would be to 'enum' the unique letters as explained in the above text.
I would think I could concatenate the numbers corresponding to the letters to create 'TOO' and add them using + as usual.
Then I would have 2 variables, a variable for sum before (TOO+TOO+TOO+TOO=) and after (=GOOD). Testing whether they match using a nested loop.
Would this be the correct way to do this?

I've been having trouble:
-Concatanating the numbers in the enumeration together (TOO)
-Getting the 'enum' for each unique character to increase in value in each loop while testing.

I may be thinking about this all wrong, but I'd really appreciate some help.



No.

Enums have contant values defined at compile time. Once they're set, they cannot be set again.
I think what need for this case is an std::map<char,int>.

http://www.cplusplus.com/reference/stl/map/
Thanks for the reply.

I've learnt about maps but I'm trying to move through the book using what has been given to me at each stage.
All thats has been explained up to this point in the book is enums, switches and loops.

I'm systematically working through the book to make sure there aren't parts I've missed. Revising for my exams.

Thanks again for any help.
Anyone?
Topic archived. No new replies allowed.