Retrieving user input and putting them in arrays

closed account (4EUoLyTq)
Hi, I'm having trouble retrieving a user inputted 4 letter word and storing each of those letters in an array index for each.
So far, I have this but it's not working as expected.
1
2
3
4
string codemessage[4];
cout << "What is the code word? (lowercase word, 4 characters long)\n";
		cin >> codemessage[0] >> codemessage[1] >> codemessage[2] >> codemessage[3] >> codemessage[4];
  

What I would like the array to do would be that the four letter word(e.g. matt) would have m stored in codemessage[0], a stored in codemessage[1] and so on.
> string codemessage[4];
¿have you never seen an array before or you don't know what string means?


> cin >> codemessage[0] >> codemessage[1] >> codemessage[2] >> codemessage[3] >> codemessage[4];
count. you are trying to input 5 things.
@OP

string codemessage[4]; is an array of 4 different strings. This can store more characters than you realize. What you really need based on your input is char codemessage[4];, but then you would have to tell the user to put a space between the code word letters for the cin>> to work properly. Otherwise you would be trying to fit all 4 letters into codemessage[0].

you might consider
1
2
string codemessage;
getline(cin,codemessage);

and then a function to break up the string. You can put the string through a for loop using the counter for the subscript to access each character of the string.

Hope that helps,

Andy
Topic archived. No new replies allowed.