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

5
  • Can you show how you were using to_string(), and what the error was? Commented Jun 16, 2014 at 0: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. Commented Jun 16, 2014 at 0:22
  • Possible duplicate of C++ concatenate string and int and C++ int to string, concatenate strings Commented Jun 16, 2014 at 0: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." Commented Jun 16, 2014 at 0: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. Commented Jun 16, 2014 at 0:49

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

I saw this on the 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.
Awesome. A very straight forward answer to my question. I will make a note of that somewhere. Thank you for your solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.