-4

I am trying this code

int n,k;
enum Throws {R, P, S};
int userInput;
for(int i=0;i<3;i++)
{
cin>>userInput;
cout<<(Throws)userInput;
}

why does this code not take R as input and provide 0 as output and wait for the next input rather gives 000 as output for any of the values expected output: input-R output-0 input-S output-2 input-P output-1 but instead i am getting this input-R(or P or S) output- 000 (exit of loop takes place) not exactly sure how the code is percieving the input as i understand that the enum should act like R=0,P=1,S=2 where in R,P,S should become like integers. what am i doing wrong or if my understanding is wrong?please can someone explain how to do it ? thanks

input output window:
input-R
output-R
input-P
output-P
input-S
output-S

this is what i am trying to get as the output i tried taking enum itself as input but it gives error like

enum Throws q;
cin>>q;
it gives error: no match for ‘operator>>’```
  • 3
    do you mean the character `'R'` is entered by the user? `R` is just a name like `userInput` is. There is no implicit relation between the letter `'R'` and the enum value named `R` – 463035818_is_not_an_ai Jul 01 '21 at 18:10
  • 1
    i am not 100% sure if I understand what you want to do. Please try to be more clear about what is user input and what you expect to get as output – 463035818_is_not_an_ai Jul 01 '21 at 18:11
  • 4
    C++ doesn't have any kind of introspection, it doesn't know that `'R'` should correspond to the enumeration constant `R`. You have to do that mapping yourself in the code. – Some programmer dude Jul 01 '21 at 18:12
  • Your `cin >> userInput;` statement inputs an *integer*. Why would it interpret, or otherwise adjust, the input to an `enum`? – Adrian Mole Jul 01 '21 at 18:13
  • Does this answer your question? [C++ string to enum](https://stackoverflow.com/questions/7163069/c-string-to-enum) – Ruzihm Jul 01 '21 at 18:13
  • Also, enumeration constants are basically a simple way to label integer constant values. `R` corresponds to the integer `0`, `P` to `1` and so on. So if you output `P` it will be output as `1`. – Some programmer dude Jul 01 '21 at 18:13
  • @Ruzihm actually this solves the problem but understanding wise of why we cant take enum as input is still not clear still trying to learn all of this so thanks for being patient – Nagendra Prasad Jul 02 '21 at 05:29

1 Answers1

0

I am not completely sure if that is what you want, but the following lets the user enter a character, then assigns corresponding enum value to a variable and then prints again the corresponding character. None of this is for free, if you want to have such mapping you need to implement it yourself:

#include <unordered_map>
#include <iostream>
#include <algorithm>

enum Throws {R, P, S};

int main(){
    std::unordered_map<char,Throws> mapping{{'R',R},{'P',P},{'S',S}};
    std::unordered_map<Throws,char> reverse_mapping{{R,'R'},{P,'P'},{S,'S'}};
    char input;

    std::cin >> input;
    Throws t = mapping[input];
    std::cout << "user entered " << input << ", corresponding enum value is " << t << "\n";

    std::cout << "the character for enum value " << t << " is " << reverse_mapping[t] << "\n";
}

For example when user enters 1 then output is:

user entered P, corresponding enum value is 1
the character for enum value 1 is P

In your code userInput is an int and when the user enters a character, eg R, then userInput has the integer value representing that character, in ascii thats 82. That 82 has not relation to the enum value R. On the other hand, if user enters 0 then you can cast that 0 to the value of R via static_cast<Throws>(userInput), thats effectively the same as the cast in your code, just that it doesn't work as you expect for user input R.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • yes this is what i wanted to do but also what if i want to input an enum value itslef like `enum Throws q; cin>>q; cout< – Nagendra Prasad Jul 02 '21 at 05:10
  • @NagendraPrasad you can want that, but thats not how C++ works. I can only repeat, there is no relation between the letter `R` and the enum value `R` unless you provide that mapping – 463035818_is_not_an_ai Jul 02 '21 at 07:42
  • 1
    That is not entirely true. One can overload `operator>>` for `std::istream` and `Throws` and implement the logic there. The mapping is still required there, but at least it is hidden away, and `std::cin >> my_throws;` can be used. – Mestkon Jul 02 '21 at 12:39
  • @Mestkon absolutely. I was too much fixating on OPs original code where `userInput` is an `int` – 463035818_is_not_an_ai Jul 02 '21 at 12:55