Thread: Arithmetic word number conversion problem

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Unhappy Arithmetic word number conversion problem

    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) result is a word eg (seven). But when the user enters eg(thirty_one + one) it should equal thirty_two, but i'm getting thirty_one, so it looks like its ignoring _one.
    What do i do????
    Also is there any way of putting the number to word conversion into the Calculator.h instead of main.cpp????

    Here is my code so far
    Code:
    #include <string>
    
    #ifndef CALCULATOR_H
    #define    CALCULATOR_H
    
    class Calculator {
    
    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);
    }
    
    #include <iostream>
    #include "Calculator.h"
    
    using namespace std;
    string Calualtor();
    
    int main() {
        int length;
        Calculator calc;
        string word1, word2;
        int result, OnesR, TensR;
        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 || word1.find(Teen[i]) == 0){
      
                    if (word1.find(Ten[i])==0)
                    {
                        n1 = i;
                        n1 = n1 * 10;
                        
                    
                    if (word1.find(Teen[i])==0)
                    {
                        n1 = i;
                        n1 = n1 + 10;
                    }
                    }
                    else{
                    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 || word2.find(Teen[i]) == 0){
                    
                    
                    if (word2.find(Ten[i])==0)
                    {
                        n2 = i;
                        n2 = n2 * 10;
                    
                    if (word2.find(Teen[i])==0)
                    {
                        n2 = i;
                        n2 = n2 + 10;
                    }
                    }
                    else{
                    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;
            }
          
     
            }
        }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    In this loop
    Code:
    for (int x = 0; x < length; x++) { //input to lower case
        word1[x] = std::tolower(word1[x]);
        word2[x] = std::tolower(word2[x]);
    }
    if the strings are not equal length, you may cause memory violation by accessing characters in word1 or word2 outside of its buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Phone number to word conversion program
    By lostmyshadow in forum C Programming
    Replies: 12
    Last Post: 04-21-2009, 02:31 PM
  2. Number-to-Word Algorithm
    By byte in forum C Programming
    Replies: 4
    Last Post: 09-23-2005, 12:59 PM
  3. Replies: 1
    Last Post: 03-06-2005, 10:12 AM
  4. complex number arithmetic
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2003, 09:04 AM
  5. C++ Word equivalent to number???
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2002, 12:35 PM