You are not logged in. Please login at www.codechef.com to post your questions!

×

what is wrong with stoi

include<bits stdc++.h="">

include<string.h>

include<stdlib.h>

using namespace std;

int main() { string str1="10"; int i=0; while(str1[i]!='!') { i++; } // string str2=str1.substr(i+1,str1.length()); // cout<<str2; int k; k= stoi( str1 ); cout<<k; return 0; }

asked 23 Mar '15, 21:03

ankit777's gravatar image

1★ankit777
4919
accept rate: 12%


You have to run while loop till '\0' character or simply NULL as last character in a string is NULL. Just correct that.

Using stoi()

int main() {

    string str1="10"; 
    int i=0; 
    while ( str1[i]!='\0' ) 
    { 
        i++; 
    } 
    // string str2=str1.substr(i+1,str1.length()); 
    //  cout << str2; 
    int k; 
    k = stoi(str1); 
    cout << k; 
    return 0; 
}

Here is the link to your updated code.

link

answered 23 Mar '15, 21:13

rjohari23's gravatar image

3★rjohari23
779214
accept rate: 14%

edited 23 Mar '15, 21:14

thankssssssss!

(23 Mar '15, 21:35) ankit7771★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,736
×334
×30
×26
×19
×12
×2

question asked: 23 Mar '15, 21:03

question was seen: 1,586 times

last updated: 23 Mar '15, 21:35