Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 Smile | :)
Posted 15-Apr-13 14:50pm
Comments
ThePhantomUpvoter at 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 at 15-Apr-13 21:06pm
   
Its a character array, so I thought I used char....?
Sergey Alexandrovich Kryukov at 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
ThePhantomUpvoter at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Coder Block at 17-Apr-13 5:46am
   
Agree,
At least one honest attemp should be there.
upvoted.:)
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 OriginalGriff 410
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 235
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100