Home > Mobile >  Why aren't the words getting calculated properly in C ?
Why aren't the words getting calculated properly in C ?

Time:08-10

I was creating the program for my friend who is interested in numerology and wants a way to calculate the value of words faster. But even though there are no errors and/or warnings, but the words' values aren't being calculated correctly. For example, "mark" has a value of 9, but the program shows 2. If you can figure out what the problem is, then pls help me. Thank you so much!

My Code:

#include <iostream>
#include <string>

int value{0};


void clear();

int main(void)
{
    int number{0};
    std::string response;


    bool run = true;
    while (run)
    {
        clear();

        value = 0;
        number = 1;
        response = "";


        std::cout << "How many words to evalute?:\n> ";
        std::cin >> number;

        std::cin.ignore();
        clear();

        std::string* pPhrase = new std::string[number];
        int* pValue = new int[number];


        for (int i = 0; i < number;   i) // could replace "number" with "sizeof(pPhrase)/sizeof(pPhrase[0])"
        {
            std::cout << "Enter Word #" << i 1 << " (or type your full phrase):\n> ";
            std::cin >> pPhrase[i];

            for (char j : pPhrase[i])
            {
                value = 0;
                j = std::tolower(j);

                if (j == 'a' || j == 'i' || j == 'j'
                    || j == 'q' || j == 'y')
                value  = 1;

                if (j == 'b' || j == 'k' || j == 'r')
                value  = 2;

                if (j == 'c' || j == 'g' || j == 'l'
                    || j == 's')
                value  = 3;

                if (j == 'd' || j == 'm' || j == 't')
                value  = 4;

                if (j == 'e' || j == 'h' || j == 'n'
                    || j == 'x')
                value  = 5;

                if (j == 'u' || j == 'v' || j == 'w')
                value  = 6;

                if (j == 'o' || j == 'z')
                value  = 7;

                if (j == 'f' || j == 'p')
                value  = 8;
                
                pValue[i] = value;
                value = 0;
                std::cout << '\n';
                clear();
            }

        }

        
        std::cin.ignore();
        std::cin.clear();

        std::cout << "\n\n";

        for (int i = 0; i < number;   i)
            std::cout << "Value of \"" << pPhrase[i] << "\": " << pValue[i] << '\n';


        //std::cin.ignore();
        std::cin.clear();

        std::cout << "Would you like to evaluate another phrase? (Y/n):\n> ";
        std::getline(std::cin, response);

        delete[] pPhrase;
        delete[] pValue;


        if (response[0] == 'y' || response[0] == 'Y'
            || response.empty() || response[0] == ' ')
        {
            std::cout << "\n\n";
            continue;
        }

        break;

    }

    std::cout << "Exiting...";
    system("killall Terminal");

    std::cout << "\n\n\n";
    return 0;
}


void clear()
{
    system("clear");
}

CodePudding user response:

There are 2 issues:

  1. On line 70: pValue[i] = value; should be pValue[i] = value;
  2. int* pValue = new int[number]; does not initialize the array. To initialize all values in pValue to 0:
    int* pValue = new int[number]{}; // <- now initialized
    

After making these changes, the word "mark" should return 9.

CodePudding user response:

You could make your program run faster by using a lookup table:

static const int table[26] = {  
/* a */ 1, /* b */ 2, /* c */ 3, /* d */ 4, /* e */ 5,  
/* f */ 8, /* g */ 3, /* h */ 5, /* i */ 1, /* j */ 1,  
/* k */ 2, /* l */ 3, /* m */ 4, /* n */ 5, /* o */ 7,  
/* p */ 8, /* q */ 1, /* r */ 2, /* s */ 3, /* t */ 4, 
/* u */ 6, /* v */ 6, /* w */ 6, /* x */ 5, /* y */ 1,
/* z */ 7,  
};  
// ...
if (isalpha(j))
{
    const int table_index = j - 'a';
    const letter_value = table[table_index];
    pValue[i] = letter_value;
    //...
}

Print the assembly language for your code fragment, then print the assembly language for the above code fragment. Compare.

  •  Tags:  
  • c
  • Related