首页 > 解决方案 > 在不使用向量的情况下在 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;
}

标签: c++

解决方案


如果要使用字符数组,它们应该是二维数组,例如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;
}

推荐阅读