1

I have fixed it another way but I still want to know why stoi is not working here. This is the code:

#include <string>
#include<iostream>
std::string fakeBin(std::string str){
    for(int i=0;i<str.size();i++){
        int number=stoi(str[i]);
        std::cout<<number<<std::endl;
    }
    return "a";
}
int main()
{
fakeBin("12354234");
return 0;
}

while compiling

int number=stoi(str[I]);

I get this error:

'stoi' was not declared in this scope

Can you explain me why I am getting this error?

Martin
  • 3,238
  • 5
  • 40
  • 66

2 Answers2

0

You should write std:: stoi or you can add a line " using namespace std; " just after the headerfile.

There are another mistake : stoi work only strings .here str[i] is a character . so you have to create a empty string variable string s=""; and then s= s+ str[i] ; after this you can do std:: stoi(s);

  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Jan 26 '22 at 02:47
0

use std:: before stoi (std::stoi)

std::stoi arguments are not true, so you are not using correct syntax, try to fix it as follows:

int       stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
int       stoi( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );

and at least use c++11

Pouya
  • 1
  • 1