将值从一个char数组存储到另一个char数组

时间:2014-02-15 01:38:34

标签: c++ arrays char

我写了这段代码:

shuffledteamnames[8][80]; // global
winningteamnames[8][80]; // global
int main()
{
    if (team1 > team2)
    {
        cout << shuffledteamnames[index1] << " beat the " << shuffledteamnames[index2] << " " << team1 << "-" << team2 << " in a game 1." << endl;
        winningteamnames[WINTEAMcounter] = shuffledteamnames[index1];
    }
    else if (team1 < team2) // index1 = 0, index2 = 1, WINTEAMcounter = 0
    {
        cout << shuffledteamnames[index2] << " beat the " << shuffledteamnames[index1] << " " << team1 << "-" << team2 << " in a game 1." << endl;
        winningteamnames[WINTEAMcounter] = shuffledteamnames[index2];
    }
}

shuffledteamnames的输出是这样的:

Trojans
Bruins
Bears
Trees
Ducks
Beavers
Huskies
Cougars

我正在尝试创建一个竞赛支架,我将每轮的获胜者放入char数组winningteamnames。我知道这些是2D char数组,所以我需要将数据输入到两个参数中,但我不知道该怎么做。如果我在任何时候都含糊不清,请告诉我,我非常感谢所有的帮助。

1 个答案:

答案 0 :(得分:1)

使用strncpy()

strncpy( winningteamnames[WINTEAMcounter]
       , shuffledteamnames[index1]
       , sizeof winningteamnames[WINTEAMcounter]);