-1
#include <iostream>
using namespace std;
int main ()
{
    string a;
    cin >> a;
    int b=10;
    cout << a+b;
    return 0;
}

I have a problem with the code above. I know it's wrong, but it shows my point.

I'm wondering if I get a number as a string, how can I have it as an integer? For example, I give 12 to the program after running. So a will be "12". Now I want the summation of 12 and the variable b. What should I do? How can I extract 12 as an integer from my string?

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
ADAN
  • 11
  • 1

1 Answers1

0

As mentioned in the comments above, you can use std::stoi.

#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string a;
    cin >> a;
    int b=10;
    cout << stoi(a)+b;
    return 0;
}
shauryachats
  • 9,975
  • 4
  • 35
  • 48