I have a problem with one variable in my code and it is throwing everything off. I use it (was global) all the time and when it works its way down my code and i try to cout it, it causes the console to not output anything and the code locks up.
player_number is what gets stuck, i changed it to a vairable in main and now refrence it in almost every function. Please help. Any suggestions
// Eucher Client.cpp : Defines the entry point for the console application.
#include "stdafx.h"
using namespace std;
const string network_directory = "\"i:\\Network Trash Folder\"";
const string file_network_directory = "i:\\Network Trash Folder";
int find_game(int pnumber_fg);
int server_full(int pnumber_sf);
int contact_server(int pnumber_cs);
void bet_info();
int get_cards(int pnumber_gc);
int make_move(int pnumber_mm);
string decode_cards(int card);
void wait_pCheck(int wait_until);
int file_pCheck(int pcheck_number);
void file_gData();
bool game_full, playing_game;
string server_name, player_name, players[4], card_name[2];
int card[2], players_ingame;
int main()
{
int player_number = 0;
//pick a server
player_number = find_game(player_number);
//bool to say while playing game do loop
playing_game = true;
while(playing_game == true)
{
system("CLS");
player_number = contact_server(player_number);cout << player_number; getch(); system("CLS");
bet_info();
player_number = get_cards(player_number); cout << player_number; getch(); system("CLS");
player_number = make_move(player_number); cout << player_number; getch(); system("CLS");
system("cls");
getch();
}
return 0;
}
int find_game(int pnumber_fg)
{
string server_list, display_list, temp;
bool bool_find_game = false, chat = false, bool_loop = true;
ofstream fout; //player_info;
ifstream fin; //server_file;
//gets the players name
cout << " Please Enter Your Name: ";
getline(cin, player_name);
//just for looks
system("CLS");
cout << "\n\n\n\n\n\n\n\n\n\n\t\t\t Finding Available Games";
for(int cnt1 = 0; cnt1 < 7; ++cnt1)
{
Sleep(200);
cout << ".";
}
//redu goto
redu:
system("CLS");
//displays a list of servers from the file server.grass
cout << " Available Games \n"
<< "*****************************\n";
display_list = "type " + network_directory + "\\TH\\server.grass";
system(display_list.c_str());
cout << "\n*****************************\n";
server_list = file_network_directory + "\\TH\\server.grass";
//while loop so if they type a name that is not in list server it asks again
while(bool_find_game == false)
{
//server_name == the server you are going to join
cout << "Please Pick a Game To Join: ";
getline(cin, server_name);
//opens the server.grass file
fin.open(server_list.c_str(), ios::in);
//nested while fin is not at the end of file
while(! fin.eof())
{
//if temp (string from .grass file) == the server name you typed
//this says if what you typed is on the list
if(temp == server_name)
{
//game_full says the game is not full
game_full = false;
//bool to say that you can jump out of the while loop
bool_find_game = true;
system("cls");
//goes into a function to see if the server is full and to
//get the player number you are
pnumber_fg = server_full(pnumber_fg);
//if the server is full
if(game_full == true)
{
fin.close();
fin.clear();
goto redu;
}
break;
}
fin >> temp;
}
fin.clear();
fin.close();
}
return pnumber_fg;
}
int server_full(int pnumber_sf)
{
ofstream fout;
ifstream fin;
string temp, temp_dir, last_temp;
bool test = false;
file_gData();
pnumber_sf = (players_ingame + 1);
temp = file_network_directory + "\\TH\\" + server_name + "\\gData.grass";
if(pnumber_sf < 5)
{
fout.open(temp.c_str(), ios::trunc);
while(! fout)
{
fout.open(temp.c_str());
}
fout << "players= " << pnumber_sf;
fout.close();
}
else
{
pnumber_sf = 0;
}
return pnumber_sf;
}
int contact_server(int pnumber_cs)
{
cout << "\n\n\n\n\n\n\n\n\n\n\t\tJoining Game (May take a moment game is in progress)";
ofstream fout;
ifstream fin;
string temp;
int tempint;
//contacts pCheck.grass
wait_pCheck(pnumber_cs);
//opens the chClient.grass file
temp = file_network_directory + "\\TH\\" + server_name + "\\chClient.grass";
fout.open(temp.c_str(), ios::trunc);
//keep opening untill it doesnt fail
while(! fout)
{
fout.clear();
fout.open(temp.c_str());
}
//send the the server your name and the 0
fout << player_name << " " << "0";
fout.close();
tempint = 0;
//while the number in the file still equals 0
temp = file_network_directory + "\\TH\\" + server_name + "\\chClient.grass";
while(pnumber_cs == 0)
{
//need to read back my new player number
fin.open(temp.c_str());
if(! fin)
{
fin.clear();
fin.open(temp.c_str());
}
fin >> pnumber_cs;
fin.close(); fin.clear();
}
return pnumber_cs;
}
void bet_info()
{
cout << "\n\n\n\n\n\n\n\n\n\n\t\t Gethering Game Information";
ifstream fin;
string temp;
int cnt = 4;
file_gData();
//Function to wait until pCheck.grass is the intiger you want
wait_pCheck(players_ingame);
//Pauses for 2 seconds to wait for server to catch up
Sleep(2000);
cnt = -1;
//opens bpINFO.grass (betting players information)
temp = file_network_directory + "\\TH\\" + server_name + "\\bpINFO.grass";
fin.open(temp.c_str());
//If the file !does not open. Keep trying to open until it does.
if(! fin)
{
fin.clear();
fin.open(temp.c_str());
}
temp = "-1";
//inputs players names into a variable
while(! fin.eof())
{
fin.clear();
if(temp != "-1")
{
cnt++;
players[cnt] = temp;
}
fin >> temp;
}
fin.close(); fin.clear();
}
int get_cards(int pnumber_gc)
{
ofstream fout;
ifstream fin;
string temp;
int tempint = 0, cnt;
cnt = -1;
file_gData();
wait_pCheck(pnumber_gc);
temp = file_network_directory + "\\TH\\" + server_name + "\\dCDS.grass";
fin.open(temp.c_str());
while(! fin)
{
fin.clear();
fin.open(temp.c_str());
}
while(! fin.eof())
{cnt++;
fin >> tempint;
card[cnt] = tempint;
card_name[cnt] = decode_cards(card[cnt]);
}
fin.close();
temp = file_network_directory + "\\TH\\" + server_name + "\\chClient.grass";
fout.open(temp.c_str(), ios::trunc);
if(! fout)
{
fout.open(temp.c_str(), ios::trunc);
}
fout << "0";
fout.close();
return pnumber_gc;
}
int make_move(int pnumber_mm)
{
system("CLS");
file_gData();
cout << "[" << player_name;
//RIGHT HERE the pnumber_mm gets stuck
cout << pnumber_mm;
cout << " ]" << endl << "----------------" << endl
<< card_name[0] << endl << card_name[1] << endl << "----------------" << "\n\n"
<< "What would you like to do";
getch();
//Function to wait until pCheck.grass is the intiger you want
wait_pCheck(players_ingame);
return pnumber_mm;
}
string decode_cards(int card)
{
string temp;
switch(card)
{
case 0:
temp = "2 of Clubs"; return temp;
case 1:
temp = "2 of Diamonds"; return temp;
case 2:
temp = "3 of Clubs"; return temp;
case 3:
temp = "3 of Diamonds"; return temp;
case 4:
temp = "4 of Clubs"; return temp;
case 5:
temp = "4 of Diamonds"; return temp;
case 6:
temp = "5 of Clubs"; return temp;
case 7:
temp = "5 of Diamonds"; return temp;
case 8:
temp = "6 of Clubs"; return temp;
case 9:
temp = "6 of Diamonds"; return temp;
case 10:
temp = "7 of Clubs"; return temp;
case 11:
temp = "7 of Diamonds"; return temp;
case 12:
temp = "8 of Clubs"; return temp;
case 13:
temp = "8 of Diamonds"; return temp;
case 14:
temp = "9 of Clubs"; return temp;
case 15:
temp = "9 of Diamonds"; return temp;
case 16:
temp = "10 of Clubs"; return temp;
case 17:
temp = "10 of Diamonds"; return temp;
case 18:
temp = "Jack of Clubs"; return temp;
case 19:
temp = "Jack of Diamonds"; return temp;
case 20:
temp = "Queen of Clubs"; return temp;
case 21:
temp = "Queen of Diamonds"; return temp;
case 22:
temp = "King of Clubs"; return temp;
case 23:
temp = "King of Diamonds"; return temp;
case 24:
temp = "Ace of Clubs"; return temp;
case 25:
temp = "Ace of Diamonds"; return temp;
case 26:
temp = "2 of Hearts"; return temp;
case 27:
temp = "2 of Spades"; return temp;
case 28:
temp = "3 of Hearts"; return temp;
case 29:
temp = "3 of Spades"; return temp;
case 30:
temp = "4 of Hearts"; return temp;
case 31:
temp = "4 of Spades"; return temp;
case 32:
temp = "5 of Hearts"; return temp;
case 33:
temp = "5 of Spades"; return temp;
case 34:
temp = "6 of Hearts"; return temp;
case 35:
temp = "6 of Spades"; return temp;
case 36:
temp = "7 of Hearts"; return temp;
case 37:
temp = "7 of Spades"; return temp;
case 38:
temp = "8 of Hearts"; return temp;
case 39:
temp = "8 of Spades"; return temp;
case 40:
temp = "9 of Hearts"; return temp;
case 41:
temp = "9 of Spades"; return temp;
case 42:
temp = "10 of Hearts"; return temp;
case 43:
temp = "10 of Spades"; return temp;
case 44:
temp = "Jack of Hearts"; return temp;
case 45:
temp = "Jack of Spades"; return temp;
case 46:
temp = "Queen of Hearts"; return temp;
case 47:
temp = "Queen of Spades"; return temp;
case 48:
temp = "King of Hearts"; return temp;
case 49:
temp = "King of Spades"; return temp;
case 50:
temp = "Ace of Hearts"; return temp;
case 51:
temp = "Ace of Spades"; return temp;
}
}
void wait_pCheck(int wait_until)
{
int temp = 0;
do
{
temp = file_pCheck(temp);
}while(temp != wait_until);
}
int file_pCheck(int pcheck_number)
{
///////////////////////////////////////////////////////////
//An Ambigious function that opens pCheck.Grass to check //
//What number player the server is currently on. //
///////////////////////////////////////////////////////////
string temp_p;
ifstream fin_p;
//opens pCheck to see if the number in pCheck == your number.
temp_p = file_network_directory + "\\TH\\" + server_name + "\\pCheck.grass";
fin_p.open(temp_p.c_str());
//if the file hasnt been created yet or is in use keep opening
while(! fin_p)
{
fin_p.clear();
fin_p.open(temp_p.c_str());
}
//gets the intiger from the pCheck.grass files
fin_p >> pcheck_number;
fin_p.close(); fin_p.clear();
//if the intiger is your player number, break loop
return pcheck_number;
}
void file_gData()
{
string temp_g;
ifstream fin_g;
temp_g = file_network_directory + "\\TH\\" + server_name + "\\gData.grass";
fin_g.open(temp_g.c_str());
//open the gData.grass file to grab the int after "player="
while(! fin_g.eof())
{
if(temp_g == "players=")
{
fin_g >> players_ingame;
fin_g.close(); fin_g.clear();
temp_g = " ";
break;
}
fin_g >> temp_g;
}
fin_g.close(); fin_g.clear();
}