[問題] 如何讓後面的亂碼不出現?

看板C_and_CPP (C/C++)作者 (ken)時間13年前 (2012/09/07 12:27), 編輯推噓1(104)
留言5則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement):s #include<iostream> #include<cstdlib> using namespace std; class CWin { public: char id; int width; int height; char title[50]; char *title1; int area() { return width*height; } int set_title() { display(); } int display() { cout<<"title :"; for(int i=0;i<=49;i++) { cout<<*(title1+i); } cout<<endl; } }; int main() { CWin win1; win1.id='A'; win1.width=50; win1.height=40; cin.getline(win1.title,5); win1.title1=win1.title; cout<<win1.set_title(); cout<<"Window"<<win1.id<<":"<<endl; cout<<"Area="<<win1.area()<<endl; cout<<"sizeof(win1)="<<sizeof(win1)<<"bytes"<<endl; system("pause"); return 0; } 輸入:abcd 輸出結果: title :abcd XXXX後面一堆亂碼 4469696WindowA: (4469696這一段也不知道從哪跑出來的) Area=2000 sizeof(win1)=68bytes 請幫幫小弟一下,問題出在哪? -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.251.46.218

09/07 12:29, , 1F
怎麼不要兩篇合一篇
09/07 12:29, 1F

09/07 12:31, , 2F
?
09/07 12:31, 2F

09/07 12:44, , 3F
不噓你 你先回去翻書上講字串的部份
09/07 12:44, 3F

09/07 13:06, , 4F
感謝,問題已解決
09/07 13:06, 4F

09/07 13:07, , 5F
亂碼1/2
09/07 13:07, 5F
文章代碼(AID): #1GINULXi (C_and_CPP)
文章代碼(AID): #1GINULXi (C_and_CPP)