Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I have to parse string in C++ to get doubles out of it. String is in format like [(a,b)-(c,d)], where a, b, c and d are going to be double variables.
I was trying to use stringstrem, but it doesn't accept const char argument, so I have no idea right now how to solve it.
My code right now looks like:
ss >> "[(" >> a >> "," >> b >> ")-(" >> c >> "," >> d >> ")]";
But sadly it doesnt work :(
For example: [(1.2,3.4)-(6.5,7.4)]. And I want:
a=1.2; b=3.4; c=6.5; d=7.4;
With input streams, you need to read input to a variable. You could read into a char or into a string with std::istream::read. Ideally you would check the characters are actually as expected, to catch and probably reject say __1.2-3.4@#~6.5,7.8++
char
std::istream::read
__1.2-3.4@#~6.5,7.8++
char chr; ss >> chr >> chr >> a >> chr >> b >> chr >> chr >> chr >> c >> chr >> d >> chr >> chr;
Alternatively you might read it as a string, and then you can use the C++ <regex> functionality to match it, perhaps something like:
<regex>
^\[\((\d+\.\d+),(\d+\.\d+)\)-\((\d+\.\d+),(\d+\.\d+)\)\]$
This will give you 4 capture groups on success, which you can then convert to doubles. You can modify this if for example extra spaces are allowed, the decimal point is optional, etc.
If there is any variation in the format at all, for example sometimes you get 3 numbers, e.g. [(1.2,3.4)-(6.5,7.4)+(1.5,2)] you would need to do more in depth parsing than the >> operator can directly support.
[(1.2,3.4)-(6.5,7.4)+(1.5,2)]
>>
Possibly with logic based on what ss >> chr read, or by looping on a regex based on where the previous match ended (instead of matching the entire string at once) although things get a lot more advanced.
ss >> chr
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.