2014-02-15 144 views
0

我写了这个代码:存储从一个字符数组中的值到另一个字符数组

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。我知道这些是二维字符数组,所以我需要将数据输入到两个参数中,但我不确定如何做到这一点。请让我知道,如果我在任何时候都含糊不清,我非常感谢所有的帮助。

+2

你缺少你的队名数组声明的'char'类型?为什么你不使用'std:string' – Barmar

+0

我们不允许使用字符串或向量 – user3255966

+0

http://en.cppreference.com/w/cpp/string/byte/strcpy –

回答

1

使用strncpy()

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

好吧,使用strncpy给我一个错误footballA2.cpp:290:70:错误:太少参数函数'char * strncpy(char *,const char *,size_t)' strncpy(winningteamnames [WINTEAMcounter],shuffledteamnames [index1 ]); ^ 在文件中包括来自footballA2.cpp:4:0: /usr/include/string.h:131:14:注:这里声明 的extern的char *函数strncpy(字符* __限制__dest, ^ 当我使用strcpy它只存储当WINTEAMcounter是1,2或3,但不是0时的值。是否有任何其他信息可以帮助调试? – user3255966

+0

它应该适用于从0到7的任何“WINTEAMcounter”值。没有显示足够的程序来解释为什么有任何区别。 – Barmar

+0

让它工作!谢谢你的所有帮助 – user3255966

相关问题