0

How can I use the "cin" code to ask for a letter as an input?

I'm playing with arrays and data types and am trying to create a simple code that will ask to enter a letter between a to e. Each letter chosen will print a certain word in return. I'm not fully getting how to use data types and arrays. Here's my currently dysfunctional code: #include <iostream> using namespace std; int main () { string letter[5]={"minor", "toddler", "child", "teenager", "adult"}; string L[5]; cout << "enter a letter between a to e\n\n"; cin >>L; switch (L) { case a: cout <<letter[0]; break; case b: cout <<letter[1]; break; case c: cout <<letter[2]; break; case d: cout <<letter[3]; break; case e: cout <<letter[4]; break; } return 0; }

27th Sep 2018, 8:52 PM
Tom Eckert
Tom Eckert - avatar
2 Respostas
+ 2
I modified your code a bit, explanations are in the comments: https://code.sololearn.com/cf0u96je2YjF/?ref=app
27th Sep 2018, 9:16 PM
Shadow
Shadow - avatar
+ 1
Thanks Shadow for a really fantastic answer. It was more than helpful!
28th Sep 2018, 4:44 AM
Tom Eckert
Tom Eckert - avatar