0

I am new to C++ programming. I have a string and an integer and I want to merge them into one char array and after that I want to split it and get the string and the integer. I worked a piece of code and it almost works. The only problem is that it sometimes creates some garbage at the end of the string. I searched this forum but I didn't get a suitable solution. What am I doing wrong? Is there a simpler solution? Thank you in advance. Here is my code:

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main() {
    cout << "string: ";
    char my_string[64];
    cin >> my_string;
    int i = 666;
    char i_size[8];
    sprintf( i_size, "%9d", i );
    strcat( my_string, i_size );
    cout << my_string << endl;
    char temp1[9] = { 0 };
    strncpy( temp1, my_string + ( strlen( my_string ) - 9 ), 9 );
    int in = atoi( temp1 );
    cout << "int = " << in << endl;
    char temp2[strlen( my_string ) - 9];
    strncpy( temp2, my_string, strlen( my_string ) - 9 );
    cout << "string = " << temp2 << "|" << endl;
    return 0;
}

And here is the output:

[corneliu@localhost prog-build]$ ./prog
string: e
e      666
int = 666
string = e|
[corneliu@localhost prog-build]$ ./prog
string: wewewewe
wewewewe      666
int = 666
string = wewewewe�@|
[corneliu@localhost prog-build]$
4
  • 3
    Your code uses C functions and C headers. These are considered non-idiomatic C++ and should be avoided. Commented Sep 18, 2013 at 13:04
  • 1
    Just a first glance: cin >> my_string; this is UNSAFE. Either specify the maximum amount to read via cin >> setw(64) >> my_string; (via <iomanip>) or even better: use a std::string from <string>. (Oh, and there are more of them: sprintf and strcat.) Commented Sep 18, 2013 at 13:05
  • char temp2[strlen( my_string ) - 9]; this -- variable length arrays -- is a gcc extension, or C99, or C++1y, but not currently allowed in C++ (98/03/11). Even in C++1y, I'd rather say it's not recommended. Either std::string or std::vector or even new char[strlen(my_string)-9] should be better. Commented Sep 18, 2013 at 13:13
  • I guess the actual problem comes from this: char temp2[strlen( my_string ) - 9];. It seems to me the space for the terminating \0 is missing in the array. Note: strncpy does not terminate the destination with a \0 if the buffer is too small. Commented Sep 18, 2013 at 13:18

1 Answer 1

1

Your working with fixed sizes and if they are larger then 7 chars in your case, it will produce garbage.

Guess since you want to code in C++, you should stay away from C "string-functions"

#include <iostream>
#include <string>

using namespace std;
void main( ... )
{
   int my_int = 666;
   cout << "string: ";
   string my_string;
   cin >> my_string;

   // concat - separated by semicolon in this example
   stringstream ss;
   ss << my_string << ";" << my_int;

   cout << "string and int in one string: "
         << ss.str().c_str() << endl;

   // split
   string tmpStr = ss.str();
   size_t found = tmpStr.find(";");
   while( found != string::npos  )
   {
      cout << tmpStr.substr(0, found).c_str() << endl;
      tmpStr = tmpStr.substr( found+1 );
      found = tmpStr.find(";");

      // this case is true for the last part
      if( found == string::npos )
         cout << tmpStr.c_str() << "\n";
   }

   return 0;
}

Depending on what you want to do with it you may need to try to convert each string-parts to an integer and if it fails you know its a string otherwise you can assign it to an integer.

I think, even this approach is not what i would really do in productive code, it should give you a hint how to do what you want to do from there.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, if you include <string>, it shouldn't be necessary to use c_str() if you want to output a std::string via an ostream.
yeah @DyP i forgot, "bad habit" to always use wcout since in most of my projects due to unicode support we only use wcout :P Always thought (w)cout doesn't like strings only chars... but didn't compile this have no proper enviroment atm
You can always use an online (www) compiler, like this one or that one. Insertion and extraction operators for string are defined in the header <string>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.