1

I am trying to get a double to be a string through stringstream, but it is not working.

std::string MatlabPlotter::getTimeVector( unsigned int xvector_size, double ts ){
    std::string tv;
    ostringstream ss;
    ss << "0:" << ts << ":" << xvector_size;
    std::cout << ss.str() << std::endl;
    return ss.str();
}

It outputs only "0:" on my console...

I'm working on two projects, both with the same problem. I'm posting a different one, which runs into the same problem. It is posted here:
http://pastebin.com/m2dd76a63
I have three classes PolyClass.h and .cpp, and the main. The function with the problem is PrintPoly. Can someone help me out? Thanks a bunch!!

Kaoru
  • 11
  • 2
  • Please would you reformat your code using the "code" button? That'll make it easier to read and make it much more likely that you'll get some help. – razlebe Oct 19 '09 at 21:03
  • You haven't explained your problem - you mention a file, yet your code example contains no file. – coppro Oct 19 '09 at 21:06
  • Please post the code that writes the string to the file! – razlebe Oct 19 '09 at 21:06
  • Guys, before you submit your edits please check someone else hasn't already fixed it. It's at 7 edits already. – GManNickG Oct 19 '09 at 21:08
  • It works for me, but then I have a few other lines like "#include ". I suggest you try to isolate the problem in a minimal source code file, then post the whole thing. – Beta Oct 19 '09 at 21:11
  • Coppro, now I'm directed to you specifically :) *I already corrected the formatting.* That's twice you've edited over someone's edit. The reason this is a problem is at a certain point, the question will become community wiki, against the intentions of everyone here. – GManNickG Oct 19 '09 at 21:14
  • 2
    I suggest you all back off your edits and let the OP fix it up otherwise you'll more than likely end up editing out the problem. Be patient. :) – Troubadour Oct 19 '09 at 21:14
  • Use code formatting, not HTML tags. Prefix code with 4 spaces. Highlight your code and click the 1010 button to have it do it for you. – GManNickG Oct 19 '09 at 22:25
  • Try an "< – T.E.D. Oct 19 '09 at 22:28

5 Answers5

4

You're printing correctly, however your logic in the order of printing is incorrect. I modified it to work they way I think you wanted it to, let me know if this helps. http://pastebin.com/d3e6e8263

Old answer:

Your code works, though ostringstream is in the std namespace. The problem is in your file printing code.

Can I see your call to the function?

I made a test case:

// #include necessary headers
int main(void)
{
  std::string s;
  s = MatlabPlotter::getTimeVector(1,1.0);
}

The output I get is 0:1:1

Corey
  • 3,125
  • 1
  • 18
  • 14
2

The following code is 100% correct:

#include <iostream>
#include <sstream>
#include <string>

// removed MatlabPlotter namespace, should have no effect
std::string getTimeVector(unsigned int xvector_size, double ts)
{
    // std::string tv; // not needed
    std::ostringstream ss;
    ss << "0:" << ts << ":" << xvector_size;

    std::cout << ss.str() << std::endl;

    return ss.str();
}

int main(void)
{
    // all work
    // 1:
    getTimeVector(0, 3.1415);

    // 2: (note, prints twice, once in the function, once outside)
    std::cout << getTimeVector(0, 3.1415) << std::endl;

    // 3: (note, prints twice, once in the function, once outside)
    std::string r = getTimeVector(0, 3.1415);
    std::cout << r << std::endl;
}

Find where we differ, that's likely your source of error. Because it stops at your double, I'm guessing the double you're trying to print is infinity, NaN (not a number), or some other error state.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
1

I can't really help with the "no output" part of this, as you didn't show your code that tries to output this. As a guess, did you perhaps not put an EOL in there somehow? Some systems won't give any text output until they hit a newline. You can do this by tacking a << std::endl onto your line, or a '\n' to your string.

Since you didn't put down a using for it, you need to use the type std::ostringstream. This is similar to how you had to use "std:string" instead of just "string".

Also, were it me, I'd get rid of that temp variable and just return ss.str(); It is less code (to possibly get wrong), and probabaly less work for the program.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • True - but not the root cause of the problem. – razlebe Oct 19 '09 at 21:12
  • I see. I skipped that connection because you didn't actually post any code that does output. Question edited appropriately. – T.E.D. Oct 19 '09 at 21:17
  • Merely pointing out that this isn't the root cause, to prevent the guy who's asking the question from getting further confused...no offence intended. – razlebe Oct 19 '09 at 21:21
0

Well, I tried the code you linked to and it outputs

 B 4
 A 5
 B 4
 C 3
x^ + 5x^ + 3

for me before crashing although the crash happens after PrintPoly. From looking at the code this is what I'd expect it to print. Are you saying you get no integers appearing after the letters?

Troubadour
  • 13,334
  • 2
  • 38
  • 57
0

Thanks all for your input! Not sure of the exact error, but it must be some setting in XCode which is messing it up. I made a CMakeLists.txt file and compiled it from the terminal using
cmake -G XCode .. and produced an XCode project. I ran it, and now it works fine...now would anyone happen to know what might cause XCode to do this? I'm running version 3.2 with the following:
64-bit
Component versions
Xcode IDE: 1610.0
Xcode Core: 1608.0
ToolSupport: 1591.0

Kaoru
  • 11
  • 2