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
17 Réponses
+ 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.
+ 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().
+ 2
Sorry, my bad. I confused c++ with java 😅👍
+ 2
Martin Taylor Thanks for letting me know. Java is so different from cpp! 😅
+ 1
Soumik That's fine fella😅😂
+ 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?
+ 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 .
+ 1
Martin Taylor Thank you Martin❤
+ 1
The point you mentioned was really interesting to me..now im greedy to learn more about it..
+ 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 .
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
...
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 ?
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..
0
No way