what(): stoi | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what(): stoi

Hi , please help to find out the bug , the error does not make sense to me... https://pastebin.ubuntu.com/p/v2dTXmdmV5

22nd Apr 2021, 2:30 PM
Ali_combination
Ali_combination - avatar
14 Answers
+ 3
When reading an integer, the newline character entered after it will remain in the input buffer and block the first call to std::getline(), which treats it as a delimiter. You can clear the input buffer with a call to std::cin.ignore( std::numeric_limits< std::streamsize >::max(), '\n' ); which requires the <limits> header file to be included.
22nd Apr 2021, 3:06 PM
Shadow
Shadow - avatar
+ 3
Just include the <limits> header and put the line of code I gave you earlier between the input of 't' and the while loop. Input in C++ is not read into variables directly, but into an inbetween buffer (same for output operations). When you extract an integer via std::cin, it will take as many valid characters as possible to form an integer and assign that to your variable. On a console, you conclude that input by pressing enter, which is represented as a newline character '\n'. Since this is not part of the integer, it will remain in the input buffer until a new extraction occurs. The problem with getline() is that is doesn't ignore preceding whitespace in the buffer, like most operations do. When trying to read a string, it will first encounter the newline character left, and stop immediately, because it is programmed to stop at that delimiter. As a result, your string remains empty. The line of code I presented will extract that newline character to prepare the input buffer for getline().
22nd Apr 2021, 3:22 PM
Shadow
Shadow - avatar
+ 2
Sorry, my bad. I confused c++ with java 😅👍
22nd Apr 2021, 2:47 PM
Soumik
Soumik - avatar
+ 2
Martin Taylor Thanks for letting me know. Java is so different from cpp! 😅
22nd Apr 2021, 2:51 PM
Soumik
Soumik - avatar
+ 1
Soumik That's fine fella😅😂
22nd Apr 2021, 2:48 PM
Ali_combination
Ali_combination - avatar
+ 1
Ali, Do a check on contents of <split> elements array before invoking std::stoi() to convert them to <num1> and <num2>. Can you give me an input example to test with?
22nd Apr 2021, 2:53 PM
Ipang
+ 1
Ipang actually , without the while loop(without several test cases) , it works properly . But looks like there is some problem when using more than just one test case . Try it without while loop . This is an example 22 45 . The output must be 60+15=75 .
22nd Apr 2021, 2:58 PM
Ali_combination
Ali_combination - avatar
+ 1
Martin Taylor Thank you Martin❤
22nd Apr 2021, 3:32 PM
Ali_combination
Ali_combination - avatar
+ 1
The point you mentioned was really interesting to me..now im greedy to learn more about it..
22nd Apr 2021, 3:38 PM
Ali_combination
Ali_combination - avatar
+ 1
Martin Taylor Hi Martin , the notification of your last message did not appear in my phone... sorry I didnt know you sent the last meassage ... and thank you again for that .. I think I should study more about the syntax and topics like that .
23rd Apr 2021, 8:12 AM
Ali_combination
Ali_combination - avatar
0
Soumik hmm..there is a problem about stoi() .. not the while loop . Plus the while loop works properly .. the message : terminate called after throwing an instance of 'std::invalid_argument' what(): stoi timeout: the monitored command dumped core ...
22nd Apr 2021, 2:38 PM
Ali_combination
Ali_combination - avatar
0
Shadow Thanks for your help , but I really didnt get it . Perhaps bcz I have never heard anything about them . Can you please make the corrected code ?
22nd Apr 2021, 3:14 PM
Ali_combination
Ali_combination - avatar
0
Shadow Thanks for your precious help Shadow ... ❤ And how could you find out the solution for solving the problem? I mean how did you know that I should I include the line of code (std::cin.ignore...) ? I want to learn more and more about the syntax..
22nd Apr 2021, 3:34 PM
Ali_combination
Ali_combination - avatar
0
No way
24th Apr 2021, 11:19 AM
riadaltamimii
riadaltamimii - avatar