0

.Please need your support. String to int without stoi function

https://code.sololearn.com/cCbw5kO5VppU/?ref=app I am trying to convert string to int without stoi. All looks good at least the last part of code. Can anyone try to help. Thank you.

5th Jun 2022, 6:02 AM
TeaserCode
8 Réponses
+ 3
Also, you should use the standard library as much as possible. I understand you're doing this as practise, but even when you're practising, focus on practising one thing and use the standard library for the rest. This way, you'll learn to use the std library while getting your practise. For example, in this code, I've implemented my own strToInt() function just like yours, but I've used the STL for doing everything else, which results in a much cleaner code. https://code.sololearn.com/cCF6BGgRCKcg/?ref=app
5th Jun 2022, 7:12 AM
XXX
XXX - avatar
+ 2
On line 67, you've not specified a size for the array `num`, so it creates an array of size 1 (sinze you have specified 1 number in the initializer list). So when you try to access an index greater than 0 of `num`, it results in a segementation fault. Also, the syntax string arr[count + 1]; Is non-standard and hence invalid on a lot of compilers. The expression inside the [] brackets MUST be constant. For example, ``` int arr[10]; // OK const int size = 10; int arr2[size]; //OK int size2 = 10; int arr3[size2]; // NOT OK ``` You should consider using std::vector instead. https://www.cplusplus.com/reference/vector/vector/
5th Jun 2022, 7:12 AM
XXX
XXX - avatar
0
Thank you for your advise. I haven't remembered to inicialise num array to 0. Now I get correct result. I don't use STL because the compiler where I am solving the code doesn't allow to use stoi function so I get to find another way..
5th Jun 2022, 8:28 AM
TeaserCode
0
TeaserCode Just out of curiosity, which compiler are you using which doesn't allow STL? Is it some coding practise website?
5th Jun 2022, 8:50 AM
XXX
XXX - avatar
0
// A good substitute for stoi is sscanf. #include <iostream> #include <cstring> using namespace std; int main(int argc, char *argv[]) { std::string a="12348 5372 89123"; std::cout << "string: \"" << a << "\"\n"; std::cout << "num: "; std::size_t spc = 0; const char *s = a.c_str(); do { int n; // convert string to integer if (sscanf(&s[spc], " %d", &n)) std::cout << n << ' '; spc = a.find(' ', spc + 1); } while (spc!=std::string::npos); std::cout << std::endl; return 0; }
5th Jun 2022, 11:04 AM
Brian
Brian - avatar
0
I has been doing one coding program on Codewars, named Weight for Weight. When I use stoi function it reports "exception what stoi". One more question there are some problems where you use functions like ceil or floor. But I use one of them as it said. The result is wrong because it fails one for one value like "correct is 344" but I get 343 or 342, which is wrong. So output must be strictly 344.
5th Jun 2022, 12:07 PM
TeaserCode
0
TeaserCode when you tried to call stoi did you mistakenly pass a size_t value to the second argument? It should be a pointer to a size_t value. It is impossible to tell what went wrong with the ceil or floor calculation without seeing the code. Floating point can be very sensitive to the order of calculations and precision used.
5th Jun 2022, 2:49 PM
Brian
Brian - avatar
0
Ok, I am going to check it out.
5th Jun 2022, 4:17 PM
TeaserCode