Cannot pass a String to Function()

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

Robert.
Last edited on
Declare it as
void URNFormat(std::string &ss)
Salem,
Thanks, but no, that did not do it...
Robert.
This code works fine in the body of main.cpp


Post how it was originally used, and how URNFormat() is now called.
This is how it is called:
ss = m1p.toString(); // code to display the first 25 Seeds in fixed-point notation.
URNFormat(ss);

Yes, the code does work just fine in the body of main.cpp
So what is the value of ss after the assignment?

Yes, the code does work just fine in the body of main.cpp


Yes, but I asked for this working code to be posted.
> 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"

Robert.
seeps,
this code works on the ss string in main, cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
            ss.erase (ss.begin()+1);
            ss.insert(0,"0.");
            std::string ex = ss.substr (ss.length()-1,1);
            if(ex == 2) ss.insert(2,"0");
            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";
Last edited on
What are the values of digit and extra - as these are global variables used by URNFormat.

You haven't got a local definition of these have you? which would be used if the code was local but not when using the URNFormat().

Also note that this code is not correct. ex is a type std::string which is incorrectly being compared to an int. The if statements should be like:

 
if(ex == "2") ss.insert(2,"0");

Last edited on
I'm bored with 20 questions.
http://sscce.org/

Post code we can copy/paste/run.
Making guesses from snippets isn't going to get it done.

And it doesn't need a string of 50 digits just to prove whether it works or not.
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
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");            // 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.
But as I mentioned before. This is WRONG:

 
if(ex == 2) ss.insert(2,"0");  


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.
Last edited on
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)

Robert.


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.

Robert.
Topic archived. No new replies allowed.