2014-10-29 44 views
0

我試圖將4個字符串放入使用for循環的數組中。之後爲循環完成,我試圖顯示使用for循環的4個字符串。我不知道我的錯誤在哪裏。任何幫助和指導,將不勝感激。C++數組,For循環和字符串數據類型

#include <iostream> 
#include <sstream> 
#include <string> 
using namespace std; 

int main() 
{ 
//declare variables 
string array[4] ; 

cout<<"input 4 strings"<<endl; 

for (int i=1; i<4; i++) 
{ 
    getline(cin,array[i]); 
} 
cout << "here are your 4 strings" << endl; 

for (int j=0; j<4; j++) 
{ 
    cout<<array[j]<<endl; 
} 

system("pause"); 
return 0; 

} // end of main function 

回答

2

你的第一個循環應該在i = 0,不i = 1啓動。

+0

謝謝。我做了修改,但是我仍然遇到錯誤。 – pHorseSpec 2014-10-29 03:28:41

+0

@ user3708219爲了將來的參考,最好指定錯誤。這樣可以幫助我們縮小問題的範圍。 – 0x499602D2 2014-10-29 03:30:05

+0

@ user3708219錯誤是什麼? – 0x499602D2 2014-10-29 03:31:09

0

你正在服用的三根弦,

for (int i=1; i<4; i++) 
{ 
    getline(cin,array[i]); 
} 

和印刷四...

for (int j=0; j<4; j++) 
{ 
    cout<<array[j]<<endl; 
} 
+0

謝謝。我做了修改,但即使修改,我仍然收到錯誤。 – pHorseSpec 2014-10-29 03:29:04

+0

@ user3708219錯誤是什麼? – ravi 2014-10-29 03:33:32

+0

代碼不會構建。對不起,但我不知道具體的錯誤。 – pHorseSpec 2014-10-29 03:37:11