The Riser
The Riser

Reputation: 329

no instance of overloaded function "stoi" matches the argument list

I'm trying to write a function that takes 4 characters,with the first and third characters being numbers,and the second and fourth characters being operators,the function converts the the first and third characters into integers,and calculates the output based on the operator between them (or doesn't do that,if the operator stored in the fourth character has a higher priority).

This is my attempt:

#include <iostream>
#include<string>



using namespace std;

string calculate(char ch1,char ch2,char ch3,char ch4);


int main() {
    int i = 1;
    string input = "4/1+1-2*2" ;
    string part;
    int leng;
    while(1){

        char cha1 = input[i - 1];
        char cha2 = input[i];
        char cha3 = input[i + 1];
        char cha4 = input[i + 2];
        part = calculate(cha1,cha2,cha3,cha4);
        if (part == "NULL") {
            i += 2;
}
        else{ input = input.replace((i-1),3,part); }
        leng = input.size();
        if (i == leng - 1) {
            i = 1;
        }
    }





}

string calculate(char ch1, char ch2, char ch3, char ch4){
    int in1;
    int in3;
    int result;
    string part;


    if (ch2 == '-') {
        if (ch4 == '*') {
            part = 'NULL';



        }
        else if (ch4 == '/') {
            part = "NULL";
        }
        else {
            in1 = stoi(ch1);
            in3 = stoi(ch3);
            result = in1 - in3;


            part = to_string(result);
        }

    }

    else if (ch2 == '+') {
        if (ch4 == '*') {
            part = "NULL";
        }
        else if (ch4 == '/') {
            part = "NULL";
        }
        else {
            in1 = stoi(ch1);
            in3 = stoi(ch3);
            result = in1 + in3;

            part = to_string(result);

        }
    }


    else if (ch2 == '*') {
        if (ch4 == '*') {
            part = "NULL";
        }
        else if (ch4 == '/') {
            part = "NULL";
        }
        else {
            in1 = stoi(ch1);
            in3 = stoi(ch3);
            result = in1 * in3;

            part = to_string(result);
        }
    }

    else if (ch2 == '/') {
        if (ch4 == '*') {
            part = "NULL";
        }
        else if (ch4 == '/') {
            part = "NULL";
        }
        else {
            in1 = stoi(ch1);
            in3 = stoi(ch3);
            result = in1 * in3;


            part = to_string(result);

        }
    }



    return part;
}

The program probably won't work as intended in it's current state,but I'll worry about that later,for now I want to deal with the stoi() function,because for every line that contains this function,I get the error in the title.

I want to know what I'm doing wrong,and what this error message exactly means to avoid getting it in the future.

Thank you in advance.

Upvotes: 1

Views: 5416

Answers (2)

walnut
walnut

Reputation: 22152

std::stoi expects a std::string as argument, but you are giving it a single char.

There is no direct conversion from char to std::string, so you need to be explicit about it:

stoi(string(1, ch1));

Here string(1, ch1) creates a string of length 1 containing only the character ch1.

Alternatively, if you are sure that ch1 is a digit at that point (stoi will throw if it isn't) you can simply subtract '0', since the digits are guaranteed to be correctly ordered in the character set:

ch1 - '0'

Or rather, you probably want to pass a std::string directly to your function, instead of multiple individual chars. You can use the .substr member function to get substrings from a string.

Upvotes: 2

benroberts999
benroberts999

Reputation: 393

std::stoi takes a std::string as its argument, but you are giving it a char.

You can directly convert char's to ints via a cast like this:

int num = ch1 - '0';

(You may want to write a function to do this, and use proper c++ style casts)

Or, covert the char to a string, or use strings to start with

Example:

int main() {
  char ch1 = '9';
  int in1 = ch1 - '0';
  std::cout << in1 << "\n";
}

Upvotes: 1

Related Questions