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??

#include <string>

#ifndef CALCULATOR_H
#define CALCULATOR_H

class Calculator {
//    float a, b;
public:
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;
}
}

}

But i am unable to convert this int back to a word. eg one + six = seven.

how about using the array filled with the words where the integer answer will be a counter to get the position/index of the word counterpart of the number

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 ??????

#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 '+':
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;
}

}
}

Remove zero from One and make One[0] ""

Put "ten" in Teen[0] instead of in Tens;

Declare an array of three ints called digits to hold the digits in result (will work for value of result 0-999

Declare a string to hold the string version of result

If result is zero display "zero"
Otherwise
If hundreds placeholder is above zero user the placeholder value as an index into One and append that and a space and the word hundred to the string name and
If the tens placeholder is:
A) zero, then use the ones placeholder as an index into One and append that to the string name
B) one, then use the tens placeholder as an index into Teen and append that to the string name
c) above one, then use the tens placeholder as an index into Ten and append that to the string name and then use the ones placeholder as an index into One and append that to the string name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.