c++ - 在不使用向量的情况下在 C++ 中的数组末尾插入元素
问题描述
我是 C++ 新手,所以我当前的代码可能完全错误。我试图将两个输入放入两个数组中,我声明了两个最大大小为 10 的数组,我想使用控制台输入并将其添加到每个数组的末尾。
#include <iostream>
int main() {
char playerName[10];
char playerScore[10];
char name, score;
cout << "Enter the player name:";
cin >> name;
cout << "Enter the player score"
cin >> score;
for (i=0; i<10, i++)
{
// add name at the end of playerName
// add score at the end of playerScore
}
return 0;
}
解决方案
如果要使用字符数组,它们应该是二维数组,例如char playername [10][10]
.
名称最多 10 个字符。
#include <iostream>
using namespace std;
int main() {
char playerName[10][10];
int playerScore[10];
for (int i=0; i<10; i++)
{
cout << "Enter the "<<i+1<<"th "<< "player's name:";
cin >> playerName[i];
cout << "Enter the " <<i+1<<"th "<< "player's score:";
cin >>playerScore[i];
cout << "_________________________________________\n";
}
return 0;
}
推荐阅读
- apache-kafka - 如果通过 stream.store() 或在处理器中通过 context.getStateStore 检索 Kafka Streams 存储中的近似条目数量的差异
- email - Outlook 客户端中不遵守边距
- for-loop - 为什么这里用if会出错,在golang中,remove-duplicates-from-sorted-array
- c# - VS 2022 - 对于在 VS 2019 中创建的项目,无法将目标平台更改为 .Net 6.0
- javascript - datatable.net 重新加载前未清除
- python - 具有精确功能的代码,如 Python 中的 oct()
- node.js - 在 NodeJs 中访问 Json 对象的方法
- swift - UISlider 忽略起始值
- python - sympy.simplify 不适用于“tanh^2(xy) + sech^2(xy)”
- python - 无法访问反向列表中元素的索引