Array with both numbers and alphabet

Pages: 12
Hi everyone I am a student who is totally new with programming. I am unable to create an array which has to content elements(1E, 2E, 3E, 4E). I tried using char however I am unable to display as it only shows the letter E. I need help please.

Thank you
What are those elements? Words?

Please, show your current code (with code tags). We have to see what you do exactly before we can propose alternatives.
For reference the question was
Elimination is a one-player game. The board consists of a set of 12 tiles, number 1 through 12. The player rolls a pair of dice and removes tile based on the numbers shown on the dice. For each roll, the player can remove either the two tiles corresponding to the numbers shown on the dice or a single tile corresponding to the sum of the numbers on the dice. If the same number appears on both dice, the player can remove only the tile corresponding to the sum of the dice. Play continues until the player cannot make a legal move or all the tiles have been removed. The player score is the sum of the remaining tiles. See sample output where the letter ā€˜Eā€™ denotes that it is not being removed. When the tile is being removed, it is denoted as ā€˜Xā€™. A random number generator is used to generate the dice number.

based on the instruction I have to create the 12 tiles in this order (1E, 2E, 3E,4E,5E,....12E)


my current code

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

using namespace std;

void main()
{
int dice1 , dice2,sum;
char tiles[12] = {'1E','2E','3E','4E','5E','6E','7E','8E','9E','10E','11E','12E'};
char choice;
srand(time (0));


{
for(int i=0 ; i<12 ; ++i)
{
cout << tiles[i];
}
dice1 = (rand() % 6) + 1;
dice2 = (rand() % 6) + 1;
cout<<"Roll dice:"<<dice1<<" "<<dice2<<endl;
cout<<"Enter D to remove tile as dice roll,S to remove sumof dice roll:";
cin>>choice;
if (choice == 'D' || choice == 'd')
{
cout<<dice1<<","<<dice2<<endl;
if (dice1 == dice2)
cout<<"Wrong move";
else
cout<<dice1<<" ,"<<dice2;
}
else if (choice == 'S' || choice == 's')
{
sum = dice1 +dice2;
cout<<sum;
}
else
{
cout<<"Invalid option!";
}

}

cin.ignore();
cin.ignore();

}// main
How about this:
1
2
3
4
5
6
bool tiles[12] {};
for(int i=0 ; i<12 ; ++i)
{
  cout << i+1 << ( tiles[i] ? 'X' : 'E' ) << ' ';
}
cout << '\n';

Can you see what I did there?
THe thing is a character is well..a character not 2 characters. You must use a string of some sort I would suggest using std::string this will be able to store the letter and number. You could also use a std::pair<char, char> I suppose. Or make yourself a simple struct for a pair like:
1
2
3
4
5
6
template <class T, class U>
struct pair
{
    T first;
    T second;
};
Hi keskiverto

I am able to understand the first two line of command however the third one is confusing.
cout << i+1 << ( tiles[i] ? 'X' : 'E' ) << ' ';
As it got both X and E but it only display X with the numbers.Can you explain the function ?

Thank you
It's a ternary operator it is the same as
1
2
3
4
5
6
std::cout << i + 1;
if(tiles[i]) //or tiles[i] == 1 or tiles[i] == true
    std::cout << 'X';
else
    std::cout << 'E';
std::cout << ' ';
Oh ok if I wana change only one to include X / E I will have to change 'I' to the number is it ?
I have another question I tried using the function it helps to change all the 12 numbers to have the same alphabet with it but how can I change only one number to have 'X' and all the rest to have 'E' like (1E, 2E,3E,4E,5E,6X,7E,8E,9E,10E,11E,12E) this.
What keskiverto has done is pretty smart, so I'm sure you missed most of it... (at your level).

The first thing to understand is that what the user sees ("7E", "X", etc) is not exactly how you have to keep things in your program.

There are only twelve tiles, and the only information you have to track about each tile is whether it is there or not. Hence the array of 12 bools. The tile number is its index into the array, and its state is a Boolean value ('is tile present?').

When he prints the tile for the user, he uses that information to display the correct thing to him (or her):

- print the tile number
- print its status (E or X)
- print a space

Hope this helps.
For the original question/post, create an object to store the individual numbers and associated characters and then store these objects in a std::vector.

You could do an array as well.

Here is a code example, you should be able to paste the below code in an empty project file and run:

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

class CharacterNumber;

class CharacterNumber {
public:
	CharacterNumber();
	CharacterNumber(char CharacterToSet, int NumberToSet);
	~CharacterNumber();
	char GetCharacter();
	void SetCharacter(char CharToSet);
	void SetCharacter(std::string CharToSet);
	int GetNumber();
	void SetNumber(int NumberToSet);
	std::string GetCharacterNumber();
private:
	int Number;
	char Character;
};

CharacterNumber::CharacterNumber() {
	Character = char("a");
	Number = 1;
}

CharacterNumber::CharacterNumber(char CharacterToSet, int NumberToSet) {
	Character = CharacterToSet;
	Number = NumberToSet;
}

CharacterNumber::~CharacterNumber() {

}

char CharacterNumber::GetCharacter() {
	return Character;
}

void CharacterNumber::SetCharacter(char CharToSet) {
	Character = CharToSet;
}

void CharacterNumber::SetCharacter(std::string CharToSet) {
	Character = CharToSet[0];
}

int CharacterNumber::GetNumber() {
	return Number;
}

void CharacterNumber::SetNumber(int NumberToSet) {
	Number = NumberToSet;
}

std::string CharacterNumber::GetCharacterNumber() {
	std::ostringstream NumberConverter;
	NumberConverter << Number;
	std::string ReturnString = "";
	ReturnString = NumberConverter.str();
	ReturnString += Character;
	return ReturnString;
}

int main(int argc, char** argv);

int main(int argc, char** argv) {
	std::vector<CharacterNumber> CharacterNumbers;
	int CharacterNumbersToCreate = 0;
	char CharacterToSet = char("A");
	std::string UserPrompt = "Type in the number of CharacterNumber objects to create: \n";
	std::cout << UserPrompt;
	std::cin >> CharacterNumbersToCreate;
	UserPrompt = "Type in what character you wish to assign to each CharacterNumber object: \n";
	std::cout << UserPrompt;
	std::cin >> CharacterToSet;
	for (int i = 0; i < CharacterNumbersToCreate; i++) {
		CharacterNumber NewCharNum(CharacterToSet, i+1);
		CharacterNumbers.push_back(NewCharNum);
	}
	std::cout << CharacterNumbersToCreate << " CharacterNumber objects created. Press enter to print them out. \n";
	std::cin;
	for (int i = 0; i < CharacterNumbers.size(); i++) {
		std::cout << CharacterNumbers[i].GetCharacterNumber() << std::endl;
	}
	system("pause");
	exit(EXIT_SUCCESS);
}
@tmason
Please, don't. Wall of code is intimidating, too "advanced" at this point, and copy-paste is a weak teacher.

@dev05
how can I change only one number to have 'X'

See "Accessing the values in array" here: http://www.cplusplus.com/doc/tutorial/arrays/

I was afraid that the ternary operator is a bit too much. It is syntactic sugar that keeps code compact and "simple". C++ offers a lot of syntactic sugar and -- as you can guess -- is therefore quite complex.


Lets do two fundamental changes to my previous snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const int Begin = 1;
const int End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
  tiles[i] = true;
}

int dice1 = (rand() % 6) + 1;
tiles[dice1] = false;

for( int i=Begin ; i < End ; ++i )
{
  cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';

What were the changes?

1. We reserve more memory (13) than we need (12) and then never use the element tiles[0]. Now we can use the tile numbers directly as indices, rather than always calculating the +1. We name the limits Begin and End so that we don't have to write raw 1 or 13 into the various loops. Unused space for one bool is a small price for convenience and efficiency.

2. I did change the convention. The true now means 'E' (present) and false is 'X' (removed). Loop on lines 4-7 sets each tile to be present. This change will simplify certain piece of code that your homework will need.


Back to the array access. Lines 6 and 14 do access individual element, but being in loop that happens for all 12 elements. Line 10 now explicitly changes one array element.
@keskiverto
If I want to have more than 1 number with a 'X' and the rest with 'E' how do I have to change the code or rather which part of the code need to be changed?

Thank you
You set the value of desired array elements to false. The lines 9-10 did change one element (but we don't know which, apart from it being one of the first 6).

Your program needs a loop, where in each iteration you do remove (i.e. set false) one or two tiles. It seems that removing an already removed tile is an invalid move that ends the loop.

For example, if first throw is 1 and 1, you have to remove tile 2. If you do throw 1 and 1 again, the game is over.

When the dice are not equal, the player has a choice to remove either those two values or their sum. At that point the player has to know what tiles are left after the previous round, because he should not make invalid move uninformed.
In other words, every iteration should
1. show tiles
2. throw dice
3. let player choose
4. break or remove tiles

When the loop has ended, you need one more loop that computes the score.
Help!
I have manage to complete my codes however I have a major issue as wherever it repeats it doesn't show the tiles it have cancelled earlier it only cancel the tile that choose lastly
my current code:

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

void main()
{
int dice1 , dice2,sum;
char choice;
srand(time (0));

//Display the 12 tiles
const int Begin = 1;
const int End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
tiles[i] = true;
}
int diceroll = (rand() % 6) + 1;

for( int i=Begin ; i < End ; ++i )
{
cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';
while(true)
{
//Rolling the pair of dice
dice1 = (rand() % 6) + 1;
dice2 = (rand() % 6) + 1;
//Results
cout<<"Roll dice:"<<dice1<<" "<<dice2<<endl;
//Player choose the option 'S' or 'D'
cout<<"Enter D to remove tiles as dice roll,S to remove sum of dice roll:";
cin>>choice;
//choice D
if (choice == 'D' || choice == 'd')
{
if (dice1 == dice2)
cout<<"Wrong move";
else
const int Begin = 1;
const int End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
tiles[i] = true;
}
int diceroll = (rand() % 6) + 1;
tiles[dice1]=false;
tiles[dice2]=false;

for( int i=Begin ; i < End ; ++i )
{
cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';

}
else if (choice == 'S' || choice == 's')
{
sum = dice1 +dice2;
const int Begin = 1;
const int End = 13;
bool tiles [End];
for ( int i=Begin ; i < End ; ++i )
{
tiles[i] = true;
}
int diceroll = (rand() % 6) + 1;
tiles[sum]=false;

for( int i=Begin ; i < End ; ++i )
{
cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
}
cout << '\n';
}
else
{
cout<<"Invalid option!";
}

}

cin.ignore();
cin.ignore();

}// main
Please edit your post to add code tags. See http://www.cplusplus.com/articles/jEywvCM9/
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
#include<iostream>
 #include<cstdlib>
 #include<ctime>

 using namespace std;

 void main()
 {
 int dice1 , dice2,sum;
 char choice;
 srand(time (0));

 //Display the 12 tiles
 const int Begin = 1;
 const int End = 13;
 bool tiles [End];
 for ( int i=Begin ; i < End ; ++i )
 {
 tiles[i] = true;
 }
 int diceroll = (rand() % 6) + 1;

 for( int i=Begin ; i < End ; ++i )
 {
 cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
 }
 cout << '\n';
 while(true)
 {
 //Rolling the pair of dice 
 dice1 = (rand() % 6) + 1;
 dice2 = (rand() % 6) + 1;
 //Results
 cout<<"Roll dice:"<<dice1<<" "<<dice2<<endl;
 //Player choose the option 'S' or 'D'
 cout<<"Enter D to remove tiles as dice roll,S to remove sum of dice roll:";
 cin>>choice;
 //choice D 
 if (choice == 'D' || choice == 'd')
 {
 if (dice1 == dice2)
 cout<<"Wrong move";
 else
 const int Begin = 1;
 const int End = 13;
 bool tiles [End];
 for ( int i=Begin ; i < End ; ++i )
 {
 tiles[i] = true;
 }
 int diceroll = (rand() % 6) + 1;
 tiles[dice1]=false;
 tiles[dice2]=false;

 for( int i=Begin ; i < End ; ++i )
 {
 cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
 }
 cout << '\n';

 }
 else if (choice == 'S' || choice == 's')
 {
 sum = dice1 +dice2;
 const int Begin = 1;
 const int End = 13;
 bool tiles [End];
 for ( int i=Begin ; i < End ; ++i )
 {
 tiles[i] = true;
 }
 int diceroll = (rand() % 6) + 1;
 tiles[sum]=false;

 for( int i=Begin ; i < End ; ++i )
 {
 cout << i << ( tiles[i] ? 'E' : 'X' ) << ' ';
 }
 cout << '\n';
 }
 else
 {
 cout<<"Invalid option!";
 }

 }

 cin.ignore();
 cin.ignore();

 }// main 
Next, consider how indentation makes reading easier.

Move lines 23-27 to line 30.
Remove lines 21, 44-51, 55-59, 65-72, and 75-79.
You need to enclose in braces the statements that should execute with the else of line 43.
You need something that will end/break the while loop.
You need the "count and show the score" at the end.
Thanks a lot that it is much better after the editing the codes . Now I am trying to improvise the codes according to the game rule.
If I need to create a function such that if the number is already with an X and if I try to re-create it have to end the game . I tried but my codes look wrong and I don't know how to improve.

1
2
3
4
5
6
7
8
9
 else if (choice == 'S' || choice == 's')
 {
 sum = dice1 +dice2;
 	
	tiles[sum]=false; 
	while (tiles[sum]=false == tiles[sum]=false)

	
 }


Pages: 12