• Register
100 points
7 2
Welcome to Kodlogs, programming questions and answer website.

In C++ stoi() is used for converting string to integer. String library contains stoi(). stoi() parses str interpret its original content as integral number. The integral number is returned as int value. This function uses strol for the conversion.

But in the conversion of string to int, sometimes the compiler creates an error. Which looks pretty simple.

Cause

Stoi can be a useful function but there is an exception which can cause error. Stoi was introduced in C++11. But some compilers and old PCs do not have the latest version of the C++.

Solution

Since older IDEs and PCs lack the support of the C++ you have to update your IDE settings or perhaps the version of the compiler.

If after installation of the C++11 you still face the error. Then you try compiling your ++ program using this command in your terminal. 

Example

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
string end, init;
cout << "Introduction" << endl;
cout << "Start time (xx:yy)" << endl;
cin >> init;
string hours0 = init.substr(0,2);
int hours = stoi(hours0);
cout << hours << endl;
system("pause");
return 0;

}

 

100 points
7 2