#include <iostream>
#include <vector>
#include <string.h>
//add other libraries if needed
usingnamespace std;
vector<string> split(const string &expression) {
//returns a vector of strings that represent operands or numbers
vector<string> myvec;
int length = expression.length();
int i = 0;
string elem1;
string elem2;
while(i < length)
{
if((expression[i] != '*') && (expression[i] != '+'))
{
elem1 = elem1 + expression[i];
i++;
}
else
{
myvec.push_back(elem1);
elem2 = expression[i];
myvec.push_back(elem2);
elem1.clear();
i++;
}
}
myvec.push_back(elem1);
return myvec;
//implement your function here
}
int main () {
//test code
//ask the user to enter an expression
//call the split function
//display the split items (numbers and operands) on the console
string exp;
cout << "Enter an expression: " << endl;
cin >> exp;
vector <string> myvec = split(exp);
for (string s: myvec)
{
cout << s << endl;
}
//add more test cases if needed
return 0;
}
If you are going to be using C++ strings include <string>, not <string.h> (<cstring>).
The header you included is for manipulating C string char arrays.
Once you fix that you will run across a problem when extracting a std::string from the input stream with std::cin. If there are any spaces in the user supplied data std::cin extracts only the data up to first space, leaving the rest of the data in the stream.
To extract the entire input, with spaces, use std::getline(), found in the <string> header.