While I'd urge you to heed the advice provided with solution 1 and check out a C++
tutorial[
^], here's a few things to consider for this problem:
There are various methods for reading input, but ultimately C++ is not a very comfortable language for parsing text from any source. You really have to know which functions and objects to use for what exact purpose.
You chose to read input from
cin
. That is a reasonable choice for a beginner in C++, but you have to understand how to use it correctly:
cin
, like
cout
, is a
stream
. The word
stream
itself already implies an important property of this object: it works on a
continuous flow of data. Being continuous it requires additional information to indicate the end of an object: every input has to be terminated with a final
return
! In your case it means you'd have to indicate the end of the input of
each individual character with a
return
- not a very practical method!
Obviously there's a better way. In fact you have multiple options:
a) instead of using
char
arrays you could use
std::string
[
^], and read your input name by name rather than character by character. Since you have the requirement to store the names in 2D char arrays, that is somewhat impractical however.
b) reading characters individually with cin is better accomplished with the function
get[
^]. However, if you stick with your code as written above, you must input 20 characters for each name, even if a name is much shorter!
c) you can call the method
getline[
^].
d) you could use old C input functions such as
gets
[
^] or
scanf
[
^]
Check out the links above for more information and some example code segments.