2014-02-15 71 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

相關問題