Remove String from Queue of Strings

I have been stuck on this for about a week and can't seem to find an answer. I need to remove a string that is typed in by a user (command prompt) from a queue of strings (which have all been added by a user).

Here's what I have:

definition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string Game::remove(string playerName)
{
	string result;
	GameNode * pCrtNode;
	pCrtNode = new GameNode;
	(*pCrtNode).player.name=playerName;
	if(pLastToPlay==NULL)
	{
		result =  "There is no one playing.\n";
	}
	else
	{
		GameNode * pTemp = (*pCrtNode).pPrev;
		pCrtNode = (*pCrtNode).pNext;
		(*pCrtNode).pPrev = pTemp;
						
		result = playerName + " has been removed.";
	}
	
	return result;
}


declaration

string remove(string playerName);


Entire code from my four files:

--Player.h--


1
2
3
4
5
6
7
8
9
10
11
#pragma once

#include <string>
using namespace std;


class Player
{
public:
	string name;
};


Game.h

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
#pragma once

#include "Player.h"
#include <string>
using namespace std;


class Game;

class GameNode
{
private:
	friend class Game;
	Player player;
	GameNode * pNext;
	GameNode * pPrev;
};



class Game
{

public:

	Game(){pLastToPlay=NULL; pNextToPlay=NULL;}
	~Game(){}

	string transducer(string & command);
	string add(string playerName);
	string remove(string playerName);
	string play();
	string playround();



private:
	
	GameNode * pLastToPlay;
	GameNode * pNextToPlay;

};

//#endif 


Game.cpp


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
#include "Game.h"



string Game::transducer(string & command)
{
	string result;
	if(command=="HELP")
	{
		result="The following commands are available:\n";
		result+="\tADD <player>\n";
		result+="\tREMOVE <player>\n";
		result+="\tPLAY\n";
		result+="\tPLAYROUND\n";
		result+="\tHELP\n";
		result+="\tQUIT\n";
	}
	else if(command=="QUIT")
	{
		result="Goodbye";
	}
	else if(command.substr(0,3)=="ADD")
	{
		string param = command.substr(4,9999);
		result=add(param);
	}
	else if(command.substr(0,6)=="REMOVE")
	{
		string param = command.substr(7,9999);
		result=remove(param);
	}
	else if(command=="PLAY")
	{
		result=play();
	}
	else if(command=="PLAYROUND")
	{
		result=playround();
	}
	else
	{
		result="ERROR: The command <" + command + "> is not recognized.";
	}
	return result;
}


string Game::add(string playerName)
{
	string result;
	GameNode * pNewNode;
	pNewNode= new GameNode;
	(*pNewNode).player.name=playerName;
	if(pLastToPlay==NULL)
	{
		(*pNewNode).pNext=pNewNode;
		pLastToPlay=pNewNode;
	}
	else
	{
		(*pNewNode).pNext = (*pLastToPlay).pNext;
		(*pLastToPlay).pNext = pNewNode;
	}
	result = playerName + " has been added.";
	return result;
}

string Game::remove(string playerName)
{
	string result;
	GameNode * pCrtNode;
	pCrtNode = new GameNode;
	(*pCrtNode).player.name=playerName;
	
	if(pLastToPlay==NULL)
	{
		result =  "There is no one playing.\n";
	}
	else
	{
		//TENTH TRY
		//I give up

		//NINTH TRY
		//Several attempts changing around the code, none worked
		//pNextToPlay = pCrtNode;
		//pNextToPlay = (*pCrtNode).pNext;
		//pLastToPlay = (*pCrtNode).pPrev;
		//(*pNextToPlay).pPrev = pLastToPlay;
		//(*pLastToPlay).pNext = pNextToPlay;
				
		//EIGHT TRY
		//pNextToPlay = (*pCrtNode).pNext;
		//pLastToPlay = (*pCrtNode).pPrev;
		//pCrtNode = pLastToPlay;
		
		//SEVENTH TRY
		//pCrtNode = (*pCrtNode).pPrev;
		//(*pLastToPlay).pNext = (*pCrtNode).pNext;

		//SIXTH TRY
		//pCrtNode = (*pCrtNode).pNext;
		//(*pLastToPlay).pNext = (*pCrtNode).pNext;
		
		//FIFTH TRY
		//REMOVES THE FIRST PLAYER TO PLAY
		pCrtNode = (*pLastToPlay).pNext;
		(*pLastToPlay).pNext = (*pCrtNode).pNext;
		
		//FIRST TRY
		//GameNode * pTemp = (*pCrtNode).pPrev;
		//pCrtNode = (*pCrtNode).pNext;
		//(*pCrtNode).pPrev = pTemp;
			
		//SECOND TRY		
		//(*pLastToPlay).pNext = (*pCrtNode).pNext;
		//pCrtNode = NULL;

		//THIRD TRY
		//pLastToPlay = pCrtNode;
		
		//FOURTH TRY
		//(*pLastToPlay).pNext = (*pCrtNode).pNext;
		//delete pCrtNode;
		
		result = playerName + " has been removed.";
	}
	
	return result;
}

string Game::play()
{
	string result;
	if(pLastToPlay==NULL)
	{
		result= "There are no Players to play.";
	}
	else
	{
		pLastToPlay=(*pLastToPlay).pNext;
		result= (*pLastToPlay).player.name + " takes a turn.";
	}
	return result;
}


string Game::playround()
{
	string result;
	GameNode * pMarker=pLastToPlay;
	do
	{
		result += play()+"\n";
	} while (pLastToPlay!=pMarker);
	return result;
}


main.cpp

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
//********************************************************
//
// (c) COPYRIGHT 2008 Dr. Thomas Fernandez
//         All rights reserved.
//
//********************************************************


// adding these lines lets us use cout and other things
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

// adding this line lets us use Game.
#include "Game.h"



string getCommandFrom(ifstream & fin)
{
	string com;
	if(fin.eof())
	{
		com="ENDBATCH";
	}
	else
	{
		getline(fin,com);
	}
	if( fin.eof() && com=="" )
	{
		com="ENDBATCH";
	}
	return com;
}



void commandLineInterface(Game & g)
{
	ifstream fin;
	ofstream fout;
	bool batchActive=false;
	bool redirectActive = false;
	string com;
	cout<<g.transducer((string)"HELP")<<endl;
	do
	{
		if(!batchActive)
		{
			cout<<">";
			getline(cin,com);
		}
		else
		{
			com=getCommandFrom(fin);
		}


		//META COMMANDS
		if(com.substr(0,5)=="BATCH")
		{
			string filename=com.substr(6,9999);
			fin.open(filename.c_str());
			batchActive=true;
		}
		else if(com=="ENDBATCH")
		{
			fin.close();
			fin.clear();
			batchActive=false;
		}
		else if(com.substr(0,8)=="REDIRECT")
		{
			string filename=com.substr(9,9999);
			fout.open(filename.c_str());
			redirectActive=true;
		}
		else if(com=="DIRECT")
		{
			fout.close();
			fout.clear();
			redirectActive=false;
		}
		else
		{
			if(!redirectActive)
			{
				cout<<g.transducer(com)<<endl;
			}
			else
			{
				fout<<g.transducer(com)<<endl;
			}
		}
	} while (com != "QUIT");
}



void simpleCommandLineInterface(Game & g)
{
	string command;
	cout<<g.transducer((string)"HELP")<<endl;
	do
	{
		cout<<">";
		getline(cin,command);
		cout<<g.transducer(command)<<endl;
	} while (command != "QUIT");
}




int main()
{
	Game game;
	commandLineInterface(game);
	return 0;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string Game::remove(string playerName)
{
	string result;
	GameNode * pCrtNode;
	pCrtNode = new GameNode; //¿why?
	(*pCrtNode).player.name=playerName;
	if(pLastToPlay==NULL)
	{
		result =  "There is no one playing.\n";
	}
	else
	{
		GameNode * pTemp = (*pCrtNode).pPrev; 
		pCrtNode = (*pCrtNode).pNext; //memory leak
		(*pCrtNode).pPrev = pTemp;
						
		result = playerName + " has been removed.";
	}
	
	return result;
}
I suppose that at least you should traverse the container, ¿don't you think?
Also, it will be better that you return a bool, handling the messages outside.

Check the STL
Topic archived. No new replies allowed.