c++ - Cant get number input from string -


just trying numbers string , using code blocks. dont know if code blocks @ fault cause heard there issue when using stoi function.

just trying following :

string input;

cin>>input;// user defined such input="he gave 6 apples " ( taking 1 number no matter how big )    int a; a=stoi(input);// 6 

first, might not able use cin whole line. spaces disturbing. should use getline() instead.
second, prefer using int atoi(const char *). code:

#include <iostream> #include <string> #include <stdlib.h> using namespace std; int myfunction(string s) {     int i;     for(i = 0; < s.length(); i++)         if(s[i] >= '0' && s[i] <= '9')             break;     return atoi(&(s[i])); } int main() {     string s;     getline(cin, s);     int numfromline = myfunction(s);     cout << numfromline << endl;     return 0; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -