So here is how the program should work: It should accept a string in which there is a full operation (ex. 23 + 34) it should split the string into three parts. The first and the third part should be the numbers and the second should be the operation sign. Then it should convert the first and third string into a double number (using atof). Then use the if statement and depending on the sign it should add, multiply, substract or divide the numbers.
Heres the code:
//Calculator V2
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
vector <string> myList;
string operation;
char add[2]="+";
char substract[2]="-";
char multiply[2]="*";
char divide[2]="/";
cout<<"Please write the operation you would like to do";
getline(cin,operation);
size_t pos = operation.find('+-*/^');
myList.push_back(operation.substr(0,pos));
myList.push_back(operation.substr(pos));
myList.push_back(operation.substr(pos+1));
double num1 = atof(myList[0].c_str());
double num2 = atof(myList[2].c_str());
if ( myList[1]==add ) {
cout<<"The result is " << num1 + num2<< "\n";
}
else if ( myList[1] == substract ) {
cout<<"The result is " << num1 - num2<< "\n";
}
else if (myList[1] == multiply) {
cout<<"The result is " << num1 * num2<< "\n";
}
else if (myList[1] == divide) {
cout<<"The result is " << num1 / num2<< "\n";
}
cin.get();
return 0;
}
It compiles fine, even though it gives me a warning: "character constants too long". So when I write the input it gives a runtime error and quits automatically.
Any help would be appreciated,
Thank You