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