Arithmetic word number conversion

Need help, trying to perform arithmetic operations using words, so far i've got to the stage where if the user enters a number in words with the operation type eg(one + six), the result is printed out as an int. But i am unable to convert this int back to a word. eg one + six = seven.
Also is there any way of putting the number to word conversion into the Calculator.h instead of main.cpp??

Please HELP!!


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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <string>

#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);
private:
    int n1;
    int n2;
};
#endif	/* CALCULATOR_H */


#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 main() {
    int length;
    Calculator calc;
    string word1, word2;
    char arithmetic;
    string One[10] = {"zero", "one", "two", "three", "four", "five", "six",
        "seven", "eight", "nine"};
    
    
    while (cin >> word1 >> arithmetic >> word2) {
        
        int n1 = 0, n2 = 0;
        
        if(word1.length()>word2.length()){    //Determine the longest length
           length = word1.length() ;
        }
        else 
           length = word2.length();

        for (int x = 0; x < length; x++) {     //input to lower case
            word1[x] = std::tolower(word1[x]);
            word2[x] = std::tolower(word2[x]);
        }
        for (int i = 9; i > 0; i--) {        //checks to see if input matches 
            if (word1.find(One[i]) == 0) {   //the array if so, prints the 
                n1 = i;                      //position
                word1.erase(0, One[i].length());
                break;
            }
        }
        for (int i = 9; i > 0; i--) {
            if (word2.find(One[i]) == 0) {
                n2 = i;
                word2.erase(0, One[i].length());
                break;
            }
        }

        switch (arithmetic) {
            case '+':
                cout << calc.add(n1, n2) << endl;
                break;
            case '-':
                cout << calc.subtract(n1, n2) << endl;
                break;
            case '*':
                cout << calc.multiply(n1, n2) << endl;
                break;
            case '/':
                cout << calc.divide(n1, n2) << endl;
                break;
            default:
                cout << 0 << endl;
        }
    }
  
}
Last edited on
Instead of writing
 
cout << calc.add(n1, n2) << endl;
You can write
1
2
int result = calc.add(n1, n2);
cout << One[result] << endl;
You will need to extend that array to be as large as the largest (81) result using this method.
Thanks kbw

So this is what i have ended up with, there are still a lot of errors, but it can only read input from 0 to 9.
I still can't get it to print out ten but it does print ten_zero, which is not really what i want.

Need help, don't know what to do ??????

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include "Calculator.h"

using namespace std;
string Calualtor();

int main() {
    int length;
    Calculator calc;
    string word1, word2;
    int result, OnesR, TensR;
    //    float a = 0, c = 0;
    char arithmetic;
    string One[10] = {"zero", "one", "two", "three", "four", "five", "six",
        "seven", "eight", "nine"};
    string Teen[10] = {"","eleven", "twelve", "thirteen", "fourteen","fifteen", "sixteen", 
        "seventeen", "eighteen", "nineteen"};
    string Ten[10] = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
        "seventy", "eighty", "ninety"};
    string Hund[10] = {"", "one_hundred", "two_hundred", "three_hundred",
        "four_hundred", "seven_hundred", "eight_hundred", "nine_hundred"};


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

        int n1 = 0, n2 = 0;

        if (word1.length() > word2.length()) { //Determine the longest length
            length = word1.length();
        } else
            length = word2.length();

        for (int x = 0; x < length; x++) { //input to lower case
            word1[x] = std::tolower(word1[x]);
            word2[x] = std::tolower(word2[x]);
        }
        for (int i = 9; i > 0; i--) {
            if (word1.find(One[i]) == 0
                    || word1.find(Ten[i]) == 0) {
                n1 = i;
                word1.erase(0, One[i].length());
                break;
            }
        }
        for (int i = 9; i > 0; i--) {
            if (word2.find(One[i]) == 0
                    || word2.find(Ten[i]) == 0){
                n2 = i;
                word2.erase(0, One[i].length());
                break;
            }
        }

        switch (arithmetic) {
            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;

        }
        
        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;
        
            }
        }
        

        cout << Ten[TensR]+ "_" << One[OnesR] << endl;
        }
      
 
        }
    }

Now that you've decided to compress the array of names, which is reasonable, the old scheme of looking up the name mapping directly in an array just won't work.

You've gone for holding decimal units, with a special case for teens, so you need to lookup words and map them onto values. I've written the bit that reads in words for you. To write the string out, you need to lookup the hundreds, tens, teens and units. I leave that for you to do.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

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

    int result = 0;
    string word, line;
    cout << "Please enter a number: ";
    getline(cin, line);

    istringstream is(line);
    while (is >> word) {
        const string* iter;

        iter = find(One, One + 10, word);
        if (iter != (One + 10)) {
            result += (iter - One);
        }

        iter = find(Teen, Teen + 10, word);
        if (iter != (Teen + 10)) {
            result += 10 + (iter - Teen);
        }

        iter = find(Ten, Ten + 10, word);
        if (iter != (Ten + 10)) {
            result += 10 * (iter - Ten);
        }

        iter = find(Hund, Hund + 10, word);
        if (iter != (Hund + 10)) {
            result += 100 * (iter - Hund);
        }
    }

    cout << result << endl;

    return 0;
}

Last edited on
Topic archived. No new replies allowed.