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

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

I think, the mistake you are doing is, you are comparing characters to a string.

For Ex: if(myList[1] == multiply) will compare myList[1] which as a character to string "*".

Try changing that char multiply[2] = "*" to char multiply '*' And, I don't understand the reason why you initialise multipy as a string with 2 characters?

>And, I don't understand the reason why you initialise multipy as a string with 2 characters?
Each character string needs a null-terminator.
But I too see no point in using a string here.
It would be better to just use a character variable instead, but I would just directly compare with the operator, no variables needed then.

To the OP:
Instead of reading the whole line, and then doing a risky conversion, you could simply do the following to get the whole input:

double operand_one;
double operand_two;
char operator;

cin >> operand_one >> operator >> operand_two;

But this is not the 'perfect' way, the 'perfect' way would be: just get the whole string, and parse it, but I guess you aren't at that level now.

Could you also please explain me why you need a vector?
(I don't see any place in your post where you mentioned that it was a part of your assignment to use vectors)
And why you include unnecessary header files?
All these include directives are unnecessary if you use the above approach to get the 'expression':

#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>

And if you continue using you approach, you can still remove the following lines:

#include <cstdio>
#include <cstring>
commented: Good advice. +21

>And, I don't understand the reason why you initialise multipy as a string with 2 characters?
Each character string needs a null-terminator.

Ya, right Tux, I figured it after posting (old, tubelight! :P)

And, here I think parsing a string is what is in the OP's mind. As, tux said, using vectors here is of no use. You can write this program in about half the no. of lines...

Thanks for the help guys. I can believe how easy was it, thanks to tux example.

I will write my program with your example.

Thanks

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.