0
Save cin message with space to file.txt
i want to make chat history. I dont know how I can make the name of file like id_person +.txt and my second problem is how i can cin message with space to save to this file.txt. can you help me ? sorru for my english. im from poland.
3 odpowiedzi
0
/*
I am no expert, this is just an idea of what you can do, and this can probably be written better with all the exception handling etc. Hope this somehow helps. :)
*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string playerName = "SkeletonLord";
string chat;
getline(cin, chat);
playerName += ".txt";
ofstream chatFile;
chatFile.open(playerName.c_str());
chatFile << chat << endl;
chatFile.close();
return 0;
}
0
i have that:
void User::talking(int login){
string message;
string login2=login+".txt";
ofstream file;
file.open(login2.c_str(),ios::app);
if(plik.good()==true){
cout<<"you can leave your message";
getline(cin,message);
file << login <<": " << message;
plik.close();}
else cout << "error";}
and this isn't work. No file.txt make... maybe it's because login is int? and in program Where i should have to write message it's dont give me a cin function. i cant write anythink.
0
Yes, it might be because of the login being int, you can also make a loop to keep getting the line of chat, and it ends when the player leaves. It should look something like this:
while (player.Online == true) {
getline(cin, message);
}