Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an assignment to enter player names and their scores only using one 2D char array, and one 2D int array. I was wondering how you can input my own data (player names for example) into even rows and columns, then print it out.

This is my idea so far, but it does not seem to be working:
char i,j,names[5][20];
  for (i=0;i<5;i++)
  {
    for(j=0;j<20;j++)
    {
      cin >> names[i][j];

    }
  }



Note: I am a beginner at C++, so I do not know a lot of terms, please use beginner stuff in your answer, thanks :)
Posted
Comments
[no name] 15-Apr-13 20:33pm    
What exactly does "does not seem to be working" mean? First thing that jumps out at me is the you have i and j declared as char when they should be int.
Member 9990263 15-Apr-13 21:06pm    
Its a character array, so I thought I used char....?
Sergey Alexandrovich Kryukov 15-Apr-13 21:47pm    
You last comment leaves me no hope that you can get any help at all. You don't even see the difference between the type of array element and the type of its index. This is possible only if you have no clue on what programming is.
—SA
[no name] 15-Apr-13 22:29pm    
No. Try and take Sergey's advice, get yourself a basic C programming book and read it, or take a class at your local college if you are serious.

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.
 
Share this answer
 
Please see my comment to the question, in reply to yours. I'm sure your could not be happy to hear that from me, but it's the best not to fool yourself.

There is only one thing that could possibly help you: honestly admit that you have no a clue, take an elementary textbook on C and basic programming topics, and start learning it from the very first page. Complement it with doing very simple exercises as you go.

(Believe me: your situation is not the worst, because you did not advance much. I knew much worse cases, where people pretended that they are developing UI, ADO.NET, graphics, but in fact they simply compiled some other's code and tried to tweak it not understanding a word. This is not your case, so you still have good chances to learn things. Only take is seriously.)
Wish you the best of luck,
—SA
 
Share this answer
 
Comments
Coder Block 17-Apr-13 5:46am    
Agree,
At least one honest attemp should be there.
upvoted.:)
Sergey Alexandrovich Kryukov 17-Apr-13 11:06am    
Thank you very much.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900