c++ Arithmetic word number conversion, please need advice

613 Views Asked by At

Thanks Joachim, but i can't get it to compile, i think it might be the way i set it up, so i removed the for loop like you said, and incorporated the functions in my calculator.cpp file. So when i compile the program, it results in the following error:

error: `ParseNumberWord' undeclared (first use this function)

Calculator.h

    #ifndef CALCULATOR_H
    #define CALCULATOR_H

    class Calculator {
    //    float a, b;
    public:
        int add(int, int);
        int subtract(int, int);
        int multiply(int, int);
        int divide(int, int);
        int ParseNumberDigit();
        int ParseNumberWord();

    private:
        int n1;
        int n2;
    };

    #endif  /* CALCULATOR_H */

Calculator.cpp

#include "Calculator.h"


int Calculator::add(int n1, int n2) {
    return (n1 + n2);
}
int Calculator::subtract(int n1, int n2) {
    return (n1 - n2);
}
int Calculator::divide(int n1, int n2) {
    return (n1 / n2);
}
int Calculator::multiply(int n1, int n2) {
    return (n1 * n2);
}

int Calculator::ParseNumberDigit(std::string &number)
{
    int n = 0;
    for (int i = 9; i >= 0 && !number.empty(); i--) {
        if (number.find(Hund[i]) == 0) {
            n = i * 100;
            number = number.substr(Hund[i].length());
        }
        else if (number.find(Ten[i]) == 0) {
            n = i * 10;
            number = number.substr(Ten[i].length());
        }
        else if (number.find(Teen[i]) == 0) {
            n = i + 10;
            number = number.substr(Teen[i].length());
        }
        else if (number.find(One[i]) == 0) {
            n = i;
            number = number.substr(One[i].length());
        }

        if (n != 0)
            break;
    }

    return n;
}


int Calculator::ParseNumberWord(const std::string &word)
{
    std::string number = word;

    std::transform(number.begin(), number.end(), number.begin(), [](char c) { return std::tolower(c); });
    // If the above line doesn't work, you have to do it the old way:
    // for (int x = 0; x < number.length(); x++) { //input to lower case
    //     number[x] = std::tolower(number[x]);
    // }

    int n = 0;

    while (!number.empty()) {
        // Parse the next "digit"
        n += ParseNumberDigit(number);

        // The '_' is used to bind together digits
        if (number[0] == '_') {
            number = number.substr(1);
        }
    }

    return n;
}

main.cpp

#include <iostream>
#include <sstream>
#include "Calculator.h"
#include <algorithm>

using namespace std;
string Calualtor();

int main() {
    int length, result, OnesR, TensR;
    Calculator calc;
    string word1a, word2a;
    string word1, word2;
//    int result, OnesR, TensR;
    char arithmetic;



std::string One[10] = {"zero", "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine"};
std::string Teen[10] = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
    "seventeen", "eighteen", "nineteen"};
std::string Ten[10] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
    "seventy", "eighty", "ninety"};
std::string Hund[10] = {"", "one_hundred", "two_hundred", "three_hundred",
    "four_hundred", "five_hundred", "six_hundred", "seven_hundred", "eight_hundred",
    "nine_hundred"};



    while (cin >> word1 >> arithmetic >> word2) {


        int n1 = ParseNumberWord(word1);
        int n2 = ParseNumberWord(word2);

        switch (arithmetic) { //determines which arithmetic operation to perform
            case '+':
                result = calc.add(n1, n2);
                break;
            case '-':
                result = calc.subtract(n1, n2);
                break;
            case '*':
                result = calc.multiply(n1, n2);
                break;
            case '/':
                result = calc.divide(n1, n2);
                break;
            default:
                cout << 0 << endl;

        }

        // for the teen array
        if (result <= 19 && result >= 11 || result <= 119 && result > 111) {
            result = result % 10;
            cout << Teen[result] << endl;

        } else {
            for (int i = 1; i <= 2; i++) //Save individual digits to individual variables.
            {
                switch (i) {
                    case 1:
                        OnesR = result % 10;
                        result = result / 10;
                        break;
                    case 2:
                        TensR = result % 10;
                        result = result / 10;
                        break;
                }

            }

            if ((OnesR || TensR) < 0) { //To print a negative result

                OnesR = OnesR * -1;
                TensR = TensR * -1;
                cout << "negative " + Ten[TensR] << One[OnesR] << endl;
            } else {
                cout << Ten[TensR] << One[OnesR] << endl;
            }
        }


    }
}
1

There are 1 best solutions below

4
On

First a small tip: The code you use to get the first and second number is the same, it would be a good idea to make this code into a function.

As for your problem, there are two of them that I see: The first is that no matter what "number"-word you match in the loop, you are always erasing from the One array, i.e. this line:

word1.erase(0, One[i].length());

should be inside the corresponding if-body and also be the using the correct array.

The second problem is that after you remove the current number-word, you do not actually check for the '_' character that supposedly bind digits together.

Edit:

The first thing you need to to is make the number-word arrays global. Then just create a function that takes a string, and returns a number:

#include <algorithm>

std::string One[10] = {"zero", "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine"};
std::string Teen[10] = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
    "seventeen", "eighteen", "nineteen"};
std::string Ten[10] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
    "seventy", "eighty", "ninety"};
std::string Hund[10] = {"", "one_hundred", "two_hundred", "three_hundred",
    "four_hundred", "five_hundred", "six_hundred", "seven_hundred", "eight_hundred",
    "nine_hundred"};

int ParseNumberDigit(std::string &number)
{
    int n = 0;
    for (int i = 9; i >= 0 && !number.empty(); i--) {
        if (number.find(Hund[i]) == 0) {
            n = i * 100;
            number = number.substr(Hund[i].length());
        }
        else if (number.find(Ten[i]) == 0) {
            n = i * 10;
            number = number.substr(Ten[i].length());
        }
        else if (number.find(Teen[i]) == 0) {
            n = i + 10;
            number = number.substr(Teen[i].length());
        }
        else if (number.find(One[i]) == 0) {
            n = i;
            number = number.substr(One[i].length());
        }

        if (n != 0)
            break;
    }

    return n;
}

int ParseNumberWord(const std::string &word)
{
    std::string number = word;

    std::transform(number.begin(), number.end(), number.begin(), [](char c) { return std::tolower(c); });
    // If the above line doesn't work, you have to do it the old way:
    // for (int x = 0; x < number.length(); x++) { //input to lower case
    //     number[x] = std::tolower(number[x]);
    // }

    int n = 0;

    while (!number.empty()) {
        // Parse the next "digit"
        n += ParseNumberDigit(number);

        // The '_' is used to bind together digits
        if (number[0] == '_') {
            number = number.substr(1);
        }
    }

    return n;
}

In main where you get the number, instead of your for-loop just use:

int n1 = ParseNumberWord(word1);
int n2 = ParseNumberWord(word2);

Note: The above functions are tested to work.