I am trying to build a function that will process a string, and return the updated string to main.cpp.
My string is built from number in Scientific Notation, 100+ digits long, from an Arbitrary_Precision Math Package.
It is converted to a string using a utility included in the Package.
I have proven code that will format the string to Fixed-Point notation, that I wish to pass to the Function(), but I cannot find a way to pass the string to the new Function()
Here is my code:
std::string ss;
unsigned int digits, extra;
void URNFormat(std::string ss){
ss.erase (ss.begin()+1); // erase the decimal point
ss.insert(0,"0."); // insert a leading zero and decimal point
std::string ex = ss.substr (ss.length()-1,1); // find the value of the exponent
if(ex == 2) ss.insert(2,"0"); // insert an equivalent number of zeros
if(ex == 3) ss.insert(2,"00");
if(ex == 4) ss.insert(2,"000");
if(ex == 5) ss.insert(2,"0000");
if(ex == 6) ss.insert(2,"00000");
if(ex == 7) ss.insert(2,"000000");
if(ex == 8) ss.insert(2,"0000000");
if(ex == 9) ss.insert(2,"00000000");
ss.erase(ss.begin()+(digits-extra+2),ss.end());
std::cout <<""<< ss <<"\n";
}
The function is called, but the string it attempts to work on is empty...
This code works fine in the body of main.cpp
I would appreciate any help anyone can give me.
I have spent hours trying to sort this out...
A typical number in scientific notation:
5.629691106608218474023826326020018108879010541044445640677221954570203589640145660536005343364216831538E-5
A typical number converted to Fixed-Point notation:
0.0000096611491480334489172150499446143183597107796359643579756458586783233444815104890768690251952665
> ss = m1p.toString(); // code to display the first 25 Seeds in fixed-point notation.
> URNFormat(ss);
My guess is that you're expecting your crappy global variable with the same name as the parameter to be updated.
> std::string ss; //!! remove this bloody thing!!!
> unsigned int digits, extra;
> void URNFormat(std::string ss){
Salem,
Thanks, I did try that,
removed the global std::string ss;
reinstated as a local to main.cpp
and tried it gain after your prompt, but still the function() is working on a blank string?
Agh....
Robert.
SeePlus
This is the value (typical) of ss after the assignment to a string:
(std::string) ::ss = "1.5587170268770183953889261471778878558532596548758717681011209488206574318029296438020681919071689600043E-1"
seeplus, salem...
Salem, I made the change you proposed, moved the definition of
std::string ss; from global to main.cpp,
and moved the declaration of
unsigned int digits, extra;
from main.cpp to global,
And I will be damned, the code in the function now works just fine, when called from main.cpp as
ss = m1p.toString(); // display Seeds in fixed-point notation.
URNFormat(ss);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
unsignedint digits, extra;
void URNFormat(std::string ss){
ss.erase (ss.begin()+1); // erase the decimal point
ss.insert(0,"0."); // insert a leading zero and decimal point
std::string ex = ss.substr (ss.length()-1,1); // find the value of the exponent
if(ex == 2) ss.insert(2,"0"); // insert an equivalent number of zeros
if(ex == 3) ss.insert(2,"00");
if(ex == 4) ss.insert(2,"000");
if(ex == 5) ss.insert(2,"0000");
if(ex == 6) ss.insert(2,"00000");
if(ex == 7) ss.insert(2,"000000");
if(ex == 8) ss.insert(2,"0000000");
if(ex == 9) ss.insert(2,"00000000"); // complexity to handle smaller Seeds is unwarrented,
ss.erase(ss.begin()+(digits-extra+2),ss.end());
std::cout <<""<< ss <<"\n";
}
seeplus, salem..
thank you both, for your time...
such a subtle change, from hours and hours of pondering over a broken (or poorly written code) to a successful function().
why is it so hard at times.
a million thank you's to you both, for your prompt and obvious helpful responses..
Robert.
This should not compile. ex is of type std::string and 2 is a digit. The two can't be compared! Whatever compiler you're using should have flagged this.
You need:
if(ex == "2") ss.insert(2,"0");
to compare ex as a type std::string with a string constant.
Also this is why you don't use global variables - you pass what is needed as parameters to a function.
seeplus
quote:
if(ex == 2) ss.insert(2,"0");
should not compile...
unquote
Thanks for your insight: it does compile (no complaints) and runs perfectly.
For your reference this is my setup:
Xcode Version: 12.5.1 (12E507)
macOS Version: 11.5 (20G5052c)
Created as a macOS Xcode Command Line Tool Project, Language C++,
Build settings:
Clang Code Generation: Debug Information, Compiler default,
Clang C Language Dialect: gnu11,
Clang C++ Language Dialect: GNU14 [-std=gnu14++]
C++ Standard Library: libc++ (LLVM C++ standard library with C++ support)
it does compile (no complaints) and runs perfectly.
Have you got an operator overload for operator==(const std::string, int) in the code that wasn't included with the provided code? If there is such an operator defined, that would explain why it works when not standard C++.
seeps,
No, there is no operator==(const std::string, int); in the project.
And a search shows there is only instance of std::string, and that is the local declaration of ss, std::string ss;
Am happy with code as it is for now, it works, and produces the output I am interested in.
Thanks for your help and encouragement.