Why my stoi is not working on valid strings code is commented where is the error
Stoi is not working
@gaurav19063 why are you taking input with getline? It will take all the whitespaces too.
If you see the size of v
you will see that it is 1, which implies that 1 2 2 -1 -1 -1 3 -1 -1
is 1 string, thats why stoi function is giving error when you are passing this string
@Ishi @Ishitagambhir but the problem is when should I stop taking input because input size is not mentioned in the question. So I had to take the entire line and then use stoi. but the problem is not that the real problem is why and what are the inputs there on which my stoi is not working and if you will print the entire vector as strings the then you will find that all the elements are valid integers in string form.
@gaurav19063 the way to take input in this question is that
-1 represents that the node does not exist. so you return NULL when you encounter -1
but @Ishitagambhir you suggested me a good reason not to use the getline but my problem was on which input my stoi is giving error so i can avoid the error in future.
@gaurav19063 i already explained that,
when you pass this as an argument for stoi() it is giving error
@Ishitagambhir please see the code i commented everything there I stored all the tokenized strings in a vector and then one by one i am passing them.
@gaurav19063 why are you taking numeric data as a string in the first place? It is unnecessary, and even then you are insisting on taking input by getline, not cin.
You are given numeric data, take it as such. Dont create unnecesary complications by taking it as a different datatype and then converting it to int.
@gaurav19063 use inbuilt tokenizer function if you wish to do it this way, it will not give any errors. See the proper syntax and usage from gfg
hehe this is not the solution of my problem.But its okay you tried well. I appreciate it.
@gaurav19063 i think I have given an alternate solution with both ways, this shouldnt even be done this way, there is a technique to making the tree. If you still want to use string datatype, use inbuilt tokenizer.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.