Remove Character from String

Hello!

I am writing a program that mimics the game Mastermind. However, I am having trouble getting past the first step. We are using letters as the "colors", a string containing "RGBYO". Each time it goes through trying to get the initial "secret code", it needs to get rid of whatever color it randomly picked so that it can not be used again. In other words, each "color" can only be used once. So if the program is generating a secret code to be guessed for this game, it can only come out with RGBY or GYBO, not something like GGYO or RGBB. I can not figure out how to remove the used color from the string. Below is my code:

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
#include <iostream>
#include <string>
#include <ctime>

void setSecretCode(char[]);
using namespace std;

int main()
{
	char code[4]= {0};
	setSecretCode(code);
	cout << code[0] << code[1] << code[2] << code[3];
	cout << endl;
	system ("pause");
	return 0;
}

void setSecretCode (char code[4])
{
	string colors = "RGBYO";
	int pos;

	srand(time(NULL));
	pos = rand() % 5; 
	code[0] = colors[pos]; 

	srand(time(NULL));
	pos = rand() % 4;
	code[1] = colors[pos];

	srand(time(NULL));
	pos = rand() % 3;
	code[2] = colors[pos];
	
	srand(time(NULL)); 
	pos = rand() % 2; 
	code[3] = colors[pos]; 
	
}


I tried colors = colors - colors[pos] as well as colors = colors - code[whateveritis], but the "code" is char and the "pos" is int, so it's telling me I can't convert within it (obviously). Any help would really be appreciated. Thank you!
@pattilupwned

Here is the mastermind game getting 4 different color values.
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
// MasterMind.cpp : main project file.

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

void setSecretCode(string code[] ,int size);

int main()
{
	string code[4];
	setSecretCode(code,4);
	cout << code[0] << code[1] << code[2] << code[3];
	cout << endl;
	system ("pause");
	return 0;
}

void setSecretCode (string code[],int size)
{
	srand(time(NULL));

	string colors[5] = {"R","G","B","Y","O"};
	int pos;

	for(int i=0;i<size;i++)
	{
		do
		{
			pos = rand() % 5;
		} while(colors[pos] == "-");
		code[i] = colors[pos];
		colors[pos] = "-";
	}
}
@ whitenite1

I have these codes but doesnt work whitenite why?? I dont have too much time,, can you type where is wrong and how to fix it?? Did you remem ber you gave me opinions like;

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
#include <iostream>
using namespace std;

int main(int, char**){

int EXIT_VALUE = 0;
char strn[80];
char arr[80];

int len=strlen(strn);

do{
      cout<<"Enter a positive integer ("<<EXIT_VALUE<<") to exit:";
      cin>>strn;
      
      if (cin.fail()) {
                    cout << "*** error: inputs must be an integer" << endl;
                    continue;  }
      else if(strn<0){
                    cout<<"*** error: inputs must be greater than zero"<<endl;
                    continue;      
      }
      
bool flag=true;
    for(int x=0;x<len;x++) {

arr[x]=(strn[x]);
    if(arr[x] == strn[(len-1)-x]) {
	flag = true;
}
else {
	flag = false;
        
       } 
}

    if(flag){
        cout<<strn<<" is a Palindrome"<<endl;
            
    }
    
    else {
        cout<<strn<<" is not a Palindrome"<<endl;
        }

while(strn!=0);

system("pause");
return 0;
   
@Lebron

For future reference, you shouldn't hi-jack other people threads. You should have just added this to your original request, and it will show up as a 'new' item.
Thank you for your help, @whitenite1. However, we are not allowed to use any "shortcuts" that we haven't learned in the class already - lame I know - but I was able to write a code without using the erase function, although the erase thing would have been MUCH easier and faster. Will keep that in mind for future problems!

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
// Set Secret Code Function
void setSecretCode(char code[])
{
	// Declare Variables
	string colors = "RGBYO";
	int pos;
	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 5;
	code[0] = colors[pos]; 
	// Call Remove Color Function
	removeColor(code, colors, pos);

	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 4; 
	code[1] = colors[pos];
	// Call Remove Color Function
	removeColor(code, colors, pos);

	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 3; 
	code[2] = colors[pos]; 
	// Call Remove Color Function
	removeColor(code, colors, pos);

	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 2; 
	code[3] = colors[pos];
	// Call Remove Color Function
	removeColor(code, colors, pos);
}

// Remove Color Function
void removeColor(char code[], string & colors, int pos)
{
	// Declare Variables
	string temp;
	int j = 0;

	// Create New Color String
	while (j<colors.length())
	{
		if (j!=pos)
		{
			temp = temp + colors[j];
		}
		j++;
	}
	colors = temp;
}
@pattilupwned

Okay. Good luck on your program. Looks a LOT more complicated than what I did. Not even sure how those functions work. Oh yeah, you shouldn't call srand() more than once. Call it at the start of your function, then just use the rand()%# afterward.
I'll be here if you need any further help..
Topic archived. No new replies allowed.