Simple Chat. Multiuser problem.
Dec 10, 2014 at 4:31am UTC
So I'm making a chat program... basically you open the .exe file and it asks for your nickname, then you can write and send a message which will be viewed by everyone online.
I have it working with just 1 user, my idea to make it work is:
Have two .exe wih the same code bar a few differences (IN the same projects folder).
I have the two projects in the same folder but the code creates TWO different example.txt. What I want is that from each code you can output the nickname to the SAME example.txt so I can print the 2 nicknames in 1 console. Not sure if I'm being clear enough. ANYWAY, other suggestions to make this program multi-user are appreciated, thanks for your time.
code 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
class USUARIO
{
public :
string nick[10];
};
class MENSAJE : public USUARIO
{
public :
char mensaje[20];
//bool todos = true;
char destino;
};
class user: public MENSAJE
{
public : string mp;
char destino;
};
/*void login()
{
USUARIO usuario;
cout << "Enter your nickname: ";
cin >> usuario.nick;
}
*/
void marco()
{
system("cls" );
cout << (char )201;
for (int i = 1; i <= 77; i++)
{
cout << (char )205;
if (i == 15)
{
cout << (char )203;
}
else if (i == 77)
{
cout << (char )187;
}
}
for (int k = 1; k <= 20; k++)
{
cout << (char )186;
for (int i = 1; i <= 77; i++)
{
cout << " " ;
if (i == 15)
{
cout << (char )186;
}
else if (i == 77)
{
cout << (char )186;
}
}
}
cout << (char )200;
for (int i = 1; i <= 77; i++)
{
cout << (char )205;
if (i == 15)
{
cout << (char )202;
}
else if (i == 77)
{
cout << (char )188;
}
}
}
void gotoxy(int x, int y)
{
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hcon, dwPos);
}
int main()
{
ofstream myfile;
myfile.open ("example.txt" , ios::out | ios::app );
if (myfile.is_open())
{
//else cout << "Unable to open file";
user usuario[4];
gotoxy(27, 9);
cout << "Enter your nickname: " ;
cin >> usuario[1].nick[1];
myfile<<usuario[1].nick[1]<<endl;
//=i0
//do1-10
//i++
//w i<10 y n[i]<>13
//vl=i
marco();
gotoxy(1,1);
cout<<usuario[0].nick[0]<<endl;
gotoxy(1,2);
cout<<usuario[1].nick[1]<<endl;
int a=1;
int x1=23;
int y1=1;
// for (int i=0; i<=4; i++){
gotoxy(1,23);
cout << "Escribir: " ;
cin>>usuario[0].mp;
gotoxy(23,1);
cout<<usuario[0].nick[0]<<": " <<usuario[0].mp<<endl;
//y1++;
//}
myfile.close();
gotoxy(1,23);
system("pause" );
system("cls" );
}
//return 0;
}
code 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#include <iostream>
#include <fstream>
#include <Windows.h>
//if (myfile.is_open())
// {
// main();
// myfile.close();
// }
using namespace std;
class USUARIO {
public :
string nick[10];
string mp;
};
class MENSAJE : public USUARIO {
public :
string mensaje;
// bool todos = true;
char destino;
};
class user: public MENSAJE
{
public : string mp;
char destino;
};
/*void login()
{
USUARIO usuario;
cout << "enter your nickname: ";
cin >> usuario.nick;
}
*/
void marco()
{
system("cls" );
cout << (char )201;
for (int i = 1; i <= 77; i++)
{
cout << (char )205;
if (i == 15)
{
cout << (char )203;
}
else if (i == 77)
{
cout << (char )187;
}
}
for (int k = 1; k <= 20; k++)
{
cout << (char )186;
for (int i = 1; i <= 77; i++)
{
cout << " " ;
if (i == 15)
{
cout << (char )186;
}
else if (i == 77)
{
cout << (char )186;
}
}
}
cout << (char )200;
for (int i = 1; i <= 77; i++)
{
cout << (char )205;
if (i == 15)
{
cout << (char )202;
}
else if (i == 77)
{
cout << (char )188;
}
}
}
void gotoxy(int x, int y){
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hcon, dwPos);
}
int main()
{
ofstream myfile;
myfile.open ("example.txt" , ios::out | ios::app );
if (myfile.is_open())
{
//else cout << "Unable to open file";
user usuario[4];
gotoxy(27, 9);
cout << "Enter your nickname: " ;
cin >> usuario[0].nick[0];
myfile<<usuario[0].nick[0]<<endl;
//=i0
//do1-10
//i++
//w i<10 y n[i]<>13
//vl=i
marco();
gotoxy(1,1);
cout<<usuario[0].nick[0]<<endl;
gotoxy(1,2);
cout<<usuario[1].nick[1]<<endl;
int a=1;
int x1=23;
int y1=1;
// for (int i=0; i<=4; i++){
gotoxy(1,23);
cout << "Escribir: " ;
cin>>usuario[0].mp;
gotoxy(23,1);
cout<<usuario[0].nick[0]<<": " <<usuario[0].mp<<endl;
//y1++;
//}
myfile.close();
gotoxy(1,23);
system("pause" );
system("cls" );
}
//return 0;
}
and here's the full working code with ONE user only:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#include <iostream>
#include <fstream>
#include <Windows.h>
using namespace std;
class USUARIO {
public :
char nick[10];
string mp;
};
class MENSAJE : public USUARIO {
public :
string mensaje;
// bool todos = true;
char destino;
};
/*void login()
{
USUARIO usuario;
cout << "Introduza su nickname: ";
cin >> usuario.nick;
}
*/
void marco()
{
system("cls" );
cout << (char )201;
for (int i = 1; i <= 77; i++)
{
cout << (char )205;
if (i == 15)
{
cout << (char )203;
}
else if (i == 77)
{
cout << (char )187;
}
}
for (int k = 1; k <= 20; k++)
{
cout << (char )186;
for (int i = 1; i <= 77; i++)
{
cout << " " ;
if (i == 15)
{
cout << (char )186;
}
else if (i == 77)
{
cout << (char )186;
}
}
}
cout << (char )200;
for (int i = 1; i <= 77; i++)
{
cout << (char )205;
if (i == 15)
{
cout << (char )202;
}
else if (i == 77)
{
cout << (char )188;
}
}
}
void gotoxy(int x, int y){
HANDLE hcon;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwPos;
dwPos.X = x;
dwPos.Y = y;
SetConsoleCursorPosition(hcon, dwPos);
}
int main()
{
USUARIO usuario;
gotoxy(27, 9);
cout << "Introduza su nickname: " ;
//=i0
//do1-10
cin >> usuario.nick;
//i++
//w i<10 y n[i]<>13
//vl=i
marco();
gotoxy(1,1);
cout<<usuario.nick<<endl;
int a=1;
int x1=23;
int y1=1;
do {
gotoxy(1,23);
cout << "Escribir: " ;
cin>>usuario.mp;
gotoxy(23,y1);
cout<<usuario.nick<<": " <<usuario.mp<<endl;
y1++;}
while (a==1);
gotoxy(1,23);
system("pause" );
system("cls" );
return 0;
}
Last edited on Dec 10, 2014 at 5:07am UTC
Topic archived. No new replies allowed.