0

When I try to convert an int to string it produces weird results which I don't know where it comes from, here's a code snippet:

if (!ss.fail() && !ss.eof()) {
    ss.clear();

    string operand1 = "" + num1;
    string operand2 = "";
    getline(ss,operand2);   
    operand2 = trim(operand2);

    cout << num1 << endl << operand1 << endl;
    return expression_isvalid(operand1) && expression_isvalid(operand2) && operator_isvalid(c);

}

ss is a stringstream, num1 is an int, while c is a char.

basically the input is an expression like "1 + 1", num1 contains the first int it finds in that expression (using ss >> num1)

what I don't get is that this part

string operand1 = "" + num1; // assume input is "1 + 1" so num1 contains the value 1
...
cout << num1 << endl << operand1 << endl; 

outputs

1
exit

I have no idea where the "exit" comes from, the word changes depending on the input, "exit" becomes "it" when I input "3+1", and "ye," when I input "13+2".

DarkPotatoKing
  • 499
  • 2
  • 9
  • 17
  • 1
    probelm with writing probelm in the title? :) – BЈовић Nov 21 '13 at 16:28
  • 1
    Yup. And problem with overflowing buffers and stuff. (That random string seems to come from invalid memory to me -> UB.) –  Nov 21 '13 at 16:29
  • I'm not sure: do you want to convert input int to string, or create calc that gets string expression and return values as strings? – Konrad 'Zegis' Nov 21 '13 at 16:32
  • `"" + num1` --- this does **not** convert an integer to a string (in fact it results in an undefined behavior, which is why you are getting weird output). You neeed to find out how to convert an integer to a string correctly. You have two wild guesses left, after that you have to open a C++ manual and read. – n. m. could be an AI Nov 21 '13 at 16:35
  • `num1` is an `int`, and you're adding that to the logical base address of a `const (&char)[1]`. You realize that is *not* the way to append the *text* translation of an `int` to a string, right? You're essentially doing pointer math on a const char buffer. – WhozCraig Nov 21 '13 at 16:35
  • I want to convert the int into a string, basically this is a part of a recursive function that checks whether an expression is a valid infix expression – DarkPotatoKing Nov 21 '13 at 16:36
  • Does this answer your question? [Easiest way to convert int to string in C++](https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c) – Newton Yuan Dec 09 '21 at 20:58

2 Answers2

0

I suggest you to use strtoul function of std. Here you can find a good documentation.

An example may be:

static unsigned long stringToInt(const string& str) {
  const char* cstr = str.c_str();
  char* endPtr = 0;
  return ::strtoul(cstr, &endPtr, 0);
}
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
0

You can use stringstream to convert various types in string. I generally use the following template to do that:

template <typename T>
static std::string strfrom(T x)
{
    std::ostringstream stream;

    stream << x;

    return stream.str();
}

Then to convert an int i into a string i_str, just do:

i_str = strfrom<int>(i)
Antonin Portelli
  • 688
  • 5
  • 12