views:

175

answers:

5

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!!

+4  A: 

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

Bruce
+1  A: 

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.
True - but not the root cause of the problem.
sgreeve
I see. I skipped that connection because you didn't actually post any code that does output. Question edited appropriately.
T.E.D.
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.
sgreeve
+2  A: 

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.

GMan
A: 

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
A: 

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