0

I have a string of number and I want to convert each number into integer type then add it into a vector.

    #include<bits/stdc++.h>
    #include<string>
    #include<vector>
    using namespace std;

    int main() {
        vector<int> vec;
        string num;
        cin >> num;
        for(int i = 0; i <= num.length(); i++){
            vec.push_back(stoi(num[i]));
        }
        return 0;
    }

it said error: no matching function for call to stoi

soappy
  • 3
  • 1
  • related/dupe: https://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c – NathanOliver Jan 06 '23 at 15:13
  • `i <= num.length(); num[i]` that is nice buffer overflow, although likely safe due `\0`. – Quimby Jan 06 '23 at 15:15
  • 2
    `#include` -- Get rid of this header. That one header alone, plus the usage of `using namespace std;` makes the program, regardless of how small it is, unpredictable if you start to introduce identifiers that happen to match what is in the `std` namespace. – PaulMcKenzie Jan 06 '23 at 15:21
  • You have a string of characters, not numbers. What are you trying to do? Are you `push_back()`.ing the digits? – justANewb stands with Ukraine Jan 06 '23 at 15:28
  • @PaulMcKenzie please can you explain more for me, I just begin to programming – soappy Jan 06 '23 at 15:28
  • @justANewbstandswithUkraine I try to convert the string of characters into integer type using the function stoi() – soappy Jan 06 '23 at 15:33
  • 1
    @soappy [See this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). You will gain experience actually *typing in* the right include header instead of copying and pasting that `` header. Once your brain and fingers are trained to know what headers should be included, then all the better. Also, including the `` header and the `using namespace std;` makes your program susceptible to some weird compiler errors if your code happens to use identifiers like `swap`, `gcd`, `data`, and a whole host of other identifiers. – PaulMcKenzie Jan 06 '23 at 15:34
  • @Quimby how can I fix that problem and improve my code – soappy Jan 06 '23 at 15:52
  • @soappy `int i = 0; i < num.length(); i++` – Quimby Jan 06 '23 at 16:01
  • 1
    @Quimby -- `i <= num.length()` is **not** a buffer overflow. It still needs to be fixed, of course. – Pete Becker Jan 06 '23 at 16:03
  • @soappy -- to convert the **text** in `str` into an integer value, just call `std::stoi(str)`. You can read about `std::stoi` [here](https://en.cppreference.com/w/cpp/string/basic_string/stol). – Pete Becker Jan 06 '23 at 16:05

1 Answers1

0

First of all, you should definitely not use #include<bits/stdc++.h> and using namespace std; in your code. They use it in platforms like LeetCode and HackerRank but it really isn't a good practice. So, as you are starting out, try avoiding it. Check out namespaces in C++, this will give you a good idea.

Now, to your problem: i <= num.length(); here, the = will make you go out of range. Also stoi works on strings, not chars.

Here is some code to help you out.

#include <iostream>
#include <string>
#include <vector>

int main(void){
    std::vector <int> vec;

    std::string str;
    std::cin >> str;

    for(int i = 0; i < str.length(); i++){
        vec.push_back(std::stoi(str.substr(i, 1))); // stio requires you the pass a string.
                                                    // So passing a character like str[i] won't work.
                                                    // We will need to convert each character into a substring.
    }

    return 0;
}

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83