-1

I get inputs from user and I put them into a vector. After that I want to get any value of vector at some index. It gives a number. How can I get the exact input value?

enum Enums 
{
    I, O,T, J , L, S, Z
};

int main() 
{


  unsigned int index;
  Piece piece;

 
 
for (index=0; index < 5; index ++)  
{
    cout << "What are the " << index+1 << ". piece?\n";
    cin >> piece.inputPiece;
    piece.pieceList.push_back(static_cast<Enums>(piece.inputPiece));
}
 Enums firstPiece= piece.pieceList[0];
 cout <<firstPiece;

}

Out Value is 79;

mordeby
  • 67
  • 6
  • 3
    What is `tetrominos`? How is the `Piece` type related to the problem? What is `piece.inputPiece`? Please create a [mre] to show us. – Some programmer dude Oct 26 '22 at 06:43
  • I fix it, this is the part of my code so I change the names to make more understandable. – mordeby Oct 26 '22 at 06:44
  • @Someprogrammerdude honestly this is not very hard to understand, alltough yes the input reading code has no relevance, not sure if one needs to be so pedantic here. – DownloadPizza Oct 26 '22 at 06:45
  • 1
    Make an example on https://godbolt.org/ and make sure it compiles and output what you've encountered. Then post your share link by pressing upper right "share" -> "short link" – Louis Go Oct 26 '22 at 06:45
  • 1
    @DownloadPizza the level of your crytalball is pretty high. I cannot get why there are `piece.pieceList` and `pieceList`. It should be reproducible to get a better answer. – Louis Go Oct 26 '22 at 06:47
  • https://godbolt.org/z/xernz8aYa @LouisGo – mordeby Oct 26 '22 at 06:48
  • @LouisGo pieceList is just a list of the enum values in order, alltough you are right, it is hard to read and not part of the problem – DownloadPizza Oct 26 '22 at 06:49
  • Now I get the point of the question, so `enum` stores as an value within C++. If you'd like to see the original enum name. Either build a string table to it or find a reflection library to it. Eg: [enum to string](https://stackoverflow.com/q/28828957/4123703) – Louis Go Oct 26 '22 at 06:53
  • 1
    Does this answer your question? [enum to string in modern C++11 / C++14 / C++17 and future C++20](https://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c11-c14-c17-and-future-c20) – Louis Go Oct 26 '22 at 06:54
  • That code in the compiler explorer is the [mre] we've beeen expecting in the question itself. – Some programmer dude Oct 26 '22 at 06:55
  • @mordeby Write the **expected outputs** for some different inputs in your question. – Jason Oct 26 '22 at 06:56
  • As for your problem, as you read a character, it will stored with the actual character encoding. So if you read the input `T`, with ASCII encoding that will be stored as the decimal value `84`. – Some programmer dude Oct 26 '22 at 06:56
  • https://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-string this works for me thanks – mordeby Oct 26 '22 at 07:00
  • @LouisGo yes you are right, I have taken too much as implied, the code does not really make a lot of sense. You are correct – DownloadPizza Oct 26 '22 at 12:36

1 Answers1

0

One way to solve this is to fix the underlying type of the enum to unsigned char and also change the type of inputPiece to unsigned char as shown below:

//----------vvvvvvvvvvvvv---->fixed this to unsigned char
enum Enums: unsigned char 
{
    I, O,T, J , L, S, Z
};

class Piece  
{
public:
//--vvvvvvvvvvvvv------------->changed this to unsigned char
    unsigned char inputPiece;
    vector<Enums> pieceList;    
    
private:
 
};

int main() 
{
  unsigned int index;
  Piece piece;

 
 
for (index=0; index < 5; index ++)  
{
    cout << "What are the " << index+1 << ". piece?\n";
    cin >> piece.inputPiece;
    piece.pieceList.push_back(static_cast<Enums>(piece.inputPiece));
}
 Enums firstPiece= piece.pieceList[0]; 
 cout <<firstPiece;//prints I for input I J T L S

}

Working demo.

The output of the program for the input: I J T L S is:

I
Alex
  • 318
  • 1
  • 14