[問題] C++字串複製問題(新手發問)

看板C_and_CPP作者 (JJ)時間14年前 (2011/05/14 22:52), 編輯推噓3(3012)
留言15則, 7人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) C++ 問題(Question): 二維字串複製問題 餵入的資料(Input): 將arr1矩陣中字串複製給arr2並輸出 預期的正確結果(Expected Output): arr2輸出結果與arr1裡相同 錯誤結果(Wrong Output): error C2106: '=' : 左運算元必須是左值 程式碼(Code):(請善用置底文網頁, 記得排版) #include "stdafx.h" #include<stdio.h> #include<stdlib.h> int _tmain(int argc, _TCHAR* argv[]) { char arr1[3][8]={"Dog","Cat","Giraffe",},arr2[3][8]; for(int i=0;i<3;i++) { arr2[i]=arr1[i]; printf("%s ,",arr2[i]); } printf("\n"); system("pause"); return 0; 補充說明(Supplement): 很多地方都是頭腦處於???的狀態 = =" 所以來板上求助於各位高手 感謝>"< -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.249.182.163

05/14 22:56, , 1F
strcpy and you are writing C, not C++
05/14 22:56, 1F

05/14 23:11, , 2F
樓上大大~前面那個我看不懂...我是在寫c++沒錯@@
05/14 23:11, 2F

05/14 23:13, , 3F
複製字串請用「strcpy」這個函式,詳細請找google
05/14 23:13, 3F

05/14 23:14, , 4F
C++的話建議使用string代替char []
05/14 23:14, 4F

05/14 23:15, , 5F
複製字串不能用=
05/14 23:15, 5F

05/14 23:19, , 6F
你的程式用C++改寫的話是這樣 http://pastie.org/1900520
05/14 23:19, 6F

05/14 23:24, , 7F
還需要#include<cstdio>嗎?
05/14 23:24, 7F

05/14 23:26, , 8F
cstdio 應該不需要 沒用到裡面的東西
05/14 23:26, 8F

05/14 23:26, , 9F
要用 system 倒是要寫 cstdlib
05/14 23:26, 9F

05/14 23:32, , 10F
string是指令嗎? 因為教的得很淺= =" 很多都不懂
05/14 23:32, 10F

05/14 23:35, , 11F
string是一個「類別」,http://ppt.cc/dPfw
05/14 23:35, 11F

05/14 23:36, , 12F
應該遲早都會教到,不然你也可以翻翻手邊的書,可能會講
05/14 23:36, 12F

05/14 23:36, , 13F
字串
05/14 23:36, 13F

05/14 23:49, , 14F
感謝大大幫忙~
05/14 23:49, 14F

05/16 17:39, , 15F
寫C++盡量用iostream
05/16 17:39, 15F
文章代碼(AID): #1DpfScej (C_and_CPP)