输入和打印2D字符数组 [英] Input and printing of 2D character arrays

查看:88
本文介绍了输入和打印2D字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务,只使用一个2D字符数组和一个2D int数组输入播放器名称及其分数。我想知道如何将我自己的数据(例如播放器名称)输入偶数行和列,然后打印出来。



这是我的想法到目前为止,但它似乎没有起作用:

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];

    }
  }







注意:我是C ++的初学者,所以我不知道很多术语,请在答案中使用初学者的东西,谢谢:)




Note: I am a beginner at C++, so I do not know a lot of terms, please use beginner stuff in your answer, thanks :)

推荐答案

虽然我'' d,请您注意解决方案1提供的建议,并查看C ++ 教程 [ ^ ],这里有几点需要考虑的问题:



有多种读取输入的方法,但最终C ++不是一种非常适合从任何来源解析文本的语言。你真的必须知道哪些功能和对象用于什么目的。



您选择从 cin 读取输入。对于初学者来说,这是一个合理的选择,但是你必须要理解如何正确使用它: cin ,比如 cout ,是一个。单词 stream 本身已经暗示了该对象的一个​​重要属性:它适用于连续数据流。连续它需要额外的信息来指示对象的结束:每个输入都必须以最终 return 终止!在你的情况下,这意味着你必须用返回来指示每个单独字符的输入结束 - 这不是一个非常实用的方法!



显然有更好的方法。实际上你有多种选择:

a)而不是使用 char 数组你可以使用 std :: string [ ^ ],并按名称而不是逐个字符读取输入名称。由于您需要将名称存储在2D char数组中,但这有点不切实际。

b)使用cin单独读取字符可以通过函数 get [ ^ ]。但是,如果你坚持使用上面所写的代码,你必须为每个名字输入20个字符,即使名字要短得多!

c)你可以调用方法getline [ ^ ]。

d)您可以使用旧的C输入功能,例如 获取 [ ^ ]或 scanf [ ^ ]



查看上面的链接以获取更多信息和一些示例代码段。
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.


请参阅我对该问题的评论,回复你的。我很确定你能听到我的声音并不高兴,但最好不要欺骗自己。



只有一件事可能会帮助你:诚实地承认你没有线索,拿一本关于C和基本编程主题的基础教科书,并从头一页开始学习它。在你去的时候做一些非常简单的练习来补充它。



(相信我:你的情况不是最糟糕的,因为你没有多少提前我知道更糟糕的情况,人们假装他们正在开发UI,ADO.NET,图形,但实际上他们只是编译了一些其他的代码并试图调整它而不理解一个单词。这不是你的情况,所以你仍然有很好的机会学习东西。只有认真对待。)
祝你好运,
-SA
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


这篇关于输入和打印2D字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆