Implementing the Numerical System

I am attempting to create a program that does the following:

1. I input a string (a possible magickal name for me).
2. The program parses the string of text and associates each letter with a number according to this chart: http://s2.postimg.org/mmkybria1/pythagorean_theorem.jpg
3. The program takes the numbers associated with each letter in each word, adds them all together, then adds the digits of that number together, does that to each word, then takes those resulting numbers, adds them together, then adds the digits of that number together, and that's the final number. My birth number is four, so if the final number from the string of text is not four, it cannot be my magickal name. See this video for information on how this is supposed to be done, if I was not clear (which I'm most certain that I wasn't because of all the confusing, criss-crossing patterns): http://www.youtube.com/watch?v=xC0LyO25Res

The current code I have is partial, but I'm currently stuck and do not know how to move forward:
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
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstdio>

int retrieveValueOfString(std::string str) {
    int a, j, s = 1;
    int b, k, t = 2;
    int c, l, u = 3;
    int d, m, v = 4;
    int e, n, w = 5;
    int f, o, x = 6;
    int g, p, y = 7;
    int h, q, z = 8;
    int i, r = 9;
    int numberArray[str.length()]; /*
                                    Assuming this is the correct way
                                    to initialize this array.
                                   */

    for(int ctr = 0; ctr < str.length(); ctr++) {
        /*
            Assuming there should be a few if statements
            to check and see which letters of the string
            represent which numbers, then add the
            inside a while loop here, but have been
            confused ever since I've started this.
        */
    }
}

int main() {
    std::string x = retrieveValueOfString("SpellcastingCricket");
    std::cout << x;
    getchar();
}
Last edited on
Could you post what you have so far so we know where to start?
Yay295: Done.
You don't need to #include <cstdio> since you're using iostream. I would include <vector> though.

lines 5-13: You are only initializing the third variable in each row, not the first two.

line 14: That is not the correct way to initialize that. I would use a vector here (which is why I said to include <vector>) std::vector<int> numberArray ( str.length ( ) );

line 31: x should be an int, not a string. You can even just cout the function and not bother with a variable x.

Is there any purpose to having the majority of your code in a side function instead of in main?
Last edited on
int a, j, s = 1;
int b, k, t = 2;
int c, l, u = 3;
int d, m, v = 4;
int e, n, w = 5;
int f, o, x = 6;
int g, p, y = 7;
int h, q, z = 8;
int i, r = 9;
This won't work. Besides the fact that you're only initializing every third variable, it doesn't establish a mapping from letters to numbers because the variable names don't exist when the program runs.
First, you need to find a way to define the mapping
'a' -> 1
'b' -> 2
'c' -> 3
'd' -> 4
...
There actually is a very simple mathematical expression that does just this. Failing that, there are other more explicit methods.
Then you need to, for each character in the string, figure out which of the arrows apply to the character. How you do this will depend directly on how you defined the mapping. Some methods are easier than others.

On another note, this can be done without using any arrays.
Topic archived. No new replies allowed.