I'm trying to convert a two character string to an integer, but I get
error: invalid cast from type 'std::basic_string<char>' to type 'int'
when I run it. Here's the code.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Instruction
{
private:
vector<string> Inst;
public:
void readFile(string infile)
{
ifstream myfile (infile);
if (myfile.is_open())
{
while (getline(myfile, line))
{
Inst.push_back(line);
}
myfile.close();
}
else
cout << "Unable to open file." << endl;
}
void runProcess()
{
for (int i=0; i<Inst.size(); i++)
{
op_code = getOperation(Inst[i]);
I'll skip the rest of runProcess, as it is unimportant. Below it, I have
int getOperation(string inst)
{
return (int)inst.substr(2);
}
which is where I'm running into trouble. I've tried (int), stoi, and atoi. Nothing has worked.
I'm fairly new to C++, so it very well could be an issue with trying to pass in the string from a vector, but I'm not sure. If I need to post anything else let me know. Any help would be greatly appreciated.