2 Replies - 1033 Views - Last Post: 25 April 2012 - 01:13 PM Rate Topic: -----

#1 pattilupwned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-March 12

Remove Character from String

Posted 24 April 2012 - 07:08 PM

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:

#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!

Is This A Good Question/Topic? 0
  • +

Replies To: Remove Character from String

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1306
  • View blog
  • Posts: 4,494
  • Joined: 19-February 09

Re: Remove Character from String

Posted 24 April 2012 - 07:33 PM

Hi, you can use string::erase.

Set n, the number of characters to delete, to 1.

The call srand(time(NULL)) seeds the random numbers using time. This allows a different set of random numbers when the program runs. It is usually only called once per program run.
Was This Post Helpful? 0
  • +
  • -

#3 pattilupwned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-March 12

Re: Remove Character from String

Posted 25 April 2012 - 01:13 PM

Thank you for your suggestion! Unfortunately, we aren't allowed to use any "shortcuts" that we haven't learned in class - IE the erase function - so I had to go about it the long way. Here is my code!

// 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;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1