0

I have done a good hour or so of research, but I can't find a method that works for me that I actually understand, as I don't understand how buffers work. I've tried the to_string method, I've tried the .append() method, and obviously, a simple concatenation method.

My code:

 fileLog[fileLogIndex].append(playerTurn+":"+moveColumn);
 //fileLog[fileLogIndex] += playerTurn && ":" && moveColumn && ", ";

So. The purpose of this, or my idea, was to keep track of each move in a connect 4 game I wrote today. This was supposed to append each move, in the format aforementioned. PlayerTurn is stored as an int, 1 being player 1, 2 being player 2, and the moveColumn variable is the column that the user selected to drop their piece. If player 1 dropped a piece into column 4, the idea was to append "1:4, " to the variable and then write that line to a file.

As you can see, I have tried a few methods, like append, and even though you can't see, I have tried to to_string () method, but I either get an error, or "1:1" gives me a smiley face in the dos window, or a null in notepad++ when opening the file.

I don't want to use a method I don't understand, so I apologize if this is a repeat of another thread, I'm just tired of staring at this 1 line of code and getting nowhere with the methods I am trying. If I have to use a buffer to do this, fine, but can someone explain to me, in somewhat newbie terms, what each line is doing?

For the record, I am using Visual Studio 2010, and I'm 90% sure I don't have C++11, which I read somewhere is the reason to_string isn't working as expected for me..

Skjaz
  • 21
  • 1
  • 3
  • Can you show how you were using `to_string()`, and what the error was? – Benjamin Lindley Jun 16 '14 at 00:17
  • Primitive types cannot have their operators overloaded, so you have: `int`+`char*`+`int` also known as pointer to the string `":"` incremented by `playerTurn+moveColumn`, which is invalid if the sum is smaller 0 or bigger 2. – Deduplicator Jun 16 '14 at 00:22
  • Possible duplicate of [C++ concatenate string and int](http://stackoverflow.com/questions/191757/c-concatenate-string-and-int) and [C++ int to string, concatenate strings](http://stackoverflow.com/questions/18726568/c-int-to-string-concatenate-strings) – jww Jun 16 '14 at 00:27
  • @BenjaminLindley fileLog[fileLogIndex] += to_string(playerTurn) && ":" && to_string(moveColumn) && ", "; But I'm not even sure that ampersands were the right means to concatentate the way I did. I was just trying something and they didn't give any errors. I was getting the error: "Error: More than one instance of overloaded function "to_string" matches the argument list." – Skjaz Jun 16 '14 at 00:43
  • @jww I looked at the C++ Concatenate string and int page, and I tried a few of there methods, but, as I said before, I couldn't find a method that I understand and that would work. It seemed like a lot of the methods they mentioned didn't work with my C++ version, or I didn't understand them enough to get them to work. – Skjaz Jun 16 '14 at 00:49

1 Answers1

2

The C++ way to do this (i.e., without using C's buffers and sprintf) that doesn't use C++11's to_string is by constructing the string using an ostringstream:

#include <sstream>

ostringstream out;
out << playerTurn << ":" << moveColumn;
fileLog[fileLogIndex] += out.str();
jwodder
  • 54,758
  • 12
  • 108
  • 124
  • I saw this on the http://stackoverflow.com/questions/191757/c-concatenate-string-and-int but nobody mentioned the include file. I guess that was me being too lazy to research though. I'll try that quick and come back with a response. – Skjaz Jun 16 '14 at 00:47
  • Awesome. A very straight forward answer to my question. I will make a note of that somewhere. Thank you for your solution. – Skjaz Jun 16 '14 at 00:54