+ 6
Manav Roy 😲😲 It'll be 10 times more easier if you named those variables in a better way. If I was you, I'd erase everything and start again with better variable names. And, define all the variables at the top, don't define at the middle sections, it's hard to keep track of them
17th Feb 2022, 11:41 AM
Rishi
Rishi - avatar
+ 4
I suspect the problem was in the for...loop starting from line 56. Can you describe me what that loop was for?
17th Feb 2022, 10:56 AM
Ipang
+ 4
Jayakrishna🇮🇳 I'm not sure, but we might see different output if we use manipulator e.g. setprecision() Floating point type is kinda complex for me to understand : )
17th Feb 2022, 1:05 PM
Ipang
+ 3
Manav Roy, If you only want to get fractional part of a floating point value, you can also use modf() from <cmath> http://www.cplusplus.com/reference/cmath/modf/. No hassle from string<->number conversions was needed. Except you have other things in mind of course ...
17th Feb 2022, 11:34 AM
Ipang
+ 3
Ipang for me also.. It almost half hour taken to understand where it coming, where Manav Roy did magic of logic of rounding? finally found it getting automatically through input.. That's why it is suggested to avoid using of float type values, if not necessary... Thx..
17th Feb 2022, 2:35 PM
Jayakrishna 🇮🇳
+ 2
_str2[i]=stoi(_strarray[L]); this line, causing the problem.. Accesing beyond array length and storing integer into a string .. _str2[i]=_strarray[L-1]; //this will be fine but what are you trying, may not fit. You have to Check yourself.. but you have lot of redundant checks.. your first for loop reversing number, 1234 to 4321 into array, second while is re doing reverse 1234 to 4321 .. why 2 loops.. you can access in reverse just... what are your next for loops doing I don't understand.. You only need to work with fraction part. No need to do anything with integer part. is not it? edit : Ipang with in cin, if I enter 1234.789 it reading it already as rounded value 1234.79 is it because of floating point issue in convertion?
17th Feb 2022, 11:55 AM
Jayakrishna 🇮🇳
+ 2
Manav Roy, Here we just use std::to_string() to build a string from a floating point value. We then check whether there is a '.' in the string, and if there is, we print a part of it starting from position of '.' plus one Now, what's your idea about rounding it? int main() { float N {123.456 }; //cin >> N; string sN { to_string( N ) }; cout << "Number as string -> "<< sN << '\n'; auto pos { sN.find( '.' ) }; if( pos != string::npos ) { cout << "Fractional -> " << sN.substr( pos + 1 ) << '\n'; } }
17th Feb 2022, 12:00 PM
Ipang
+ 2
Manav Roy , no worries. I try to find your code problem.. Meanwhile i found this. it's not with your code. That's the problem with using of float values.. It's different in different language based on implementation.. See this demonstration: https://blog.penjee.com/demonstration-of-floating-point-error-and-code-java/ I have some other sourses, I try to add.. my doubt to ask @ipang was is that related to c++ any in reading. but it may different in different languages. edit: old code : https://code.sololearn.com/cKii0a2b11M9/?ref=app
17th Feb 2022, 5:03 PM
Jayakrishna 🇮🇳
+ 1
Segmentation Fault occurs when program try to access memory location which is not allocated by the program
18th Feb 2022, 2:24 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar