2016-05-23 49 views
0

我必須創建一個簡單的代碼,該代碼應該創建一個.txt文件作爲輸出,其中包含一個具有此格式的符號列表。 (時間;主題;評論) 代碼具有運行使用結構功能的迴路,顯示如下:C++中的對象循環問題

struct annotation_t { 
string topic; 
string comment; 
time_t stamp; 
}; 

,使用戶可以輸入他想要的,直到他決定走出去的符號多次。這是我迄今爲止所做的。

#include <iostream> 
#include <string> 
#include <ctime> 
#include <fstream> 
#include <cstdlib> 
#include <vector> 


using namespace std; 
struct annotation_t { 
string topic; 
string comment; 
time_t stamp; 
}; 

int main() 
{ 

int q = 0; 
std::vector<annotation_t> obj; 

do 
{ 

annotation_t temp = {}; 

cout<< "input your topic: "; 
cin >> temp.topic ; 
cout<< "input yourfeedback: "; 
cin >> temp.comment ; 
cout<< "input your time stamp: "; 
cin >> temp.stamp ; 
cout<< "exit?"; 
cin >> q; 

obj.push_back(temp); 


} while (q != 0); 


ofstream myfile("annotation.txt"); 
char time[1000]; 

for(int i = 0;i<50;i++) 
{ 
struct annotation_t obj[i]; 
myfile<<obj[i].stamp <<" "; // write in file 
myfile<<obj[i].topic <<" ";// write in file 
myfile<<obj[i].comment; // write in file 
myfile<<"\n"; 

} 
cout<<"\nFile Created with Data with name annotation.txt \n"; 

myfile.close(); 


system("Pause"); 

} 

我在退出時遇到問題。如果我輸入任何值(即使是0),我得到一個分段錯誤,所以我不能退出循環,並保存我的文件在TXT,或重新運行,如果我想輸入更多..讓我知道你的想法。謝謝

+0

看@Tas的答案。您可能想要在第二個循環中將變量名從i更改爲j,以避免混淆。 – k1133

+0

請不要污損你的問題 –

+0

Paulit這是我試圖標記爲正的一個錯誤 – FL93

回答

0

你有聲明'obj'對象的問題。 這是錯誤的:

struct annotation_t obj[i]; 

試試這個:

#include <iostream> 
#include <string> 
#include <ctime> 
#include <fstream> 
#include <cstdlib> 


using namespace std; 
struct annotation_t { 
string topic; 
string comment; 
time_t stamp; 
}; 

int main() 
{ 

int q = 0; 
struct annotation_t obj[1000]; //msf 

int i=0; //msf 
do 
{ 

cout<< "input your topic"; 
cin >> obj[i].topic ; 
cout<< "input yourfeedback"; 
cin >> obj[i].comment ; 
cout<< "input your time stamp"; 
cin >> obj[i].stamp ; 
cout<< "exit?"; 
cin >> q ; 
i++; //msf 

} while (q != 0); 


ofstream myfile("annotation.txt"); 
int count=i; //msf 

for(i = 0;i<count;i++) //msf 
{ 
myfile<<obj[i].stamp <<" "; // write in file 
myfile<<obj[i].topic <<" ";// write in file 
myfile<<obj[i].comment; // write in file 
myfile<<"\n"; 

} 
cout<<"\nFile Created with Data with name annotation.txt \n"; 

myfile.close(); 


system("Pause"); 

} 

我在我與 '無國界醫生//' 評論很多持倉變化你的代碼。 我沒有C++編譯器,我希望它會被編譯而沒有錯誤。

+0

當我到達「exit?」時,出現分段錯誤錯誤。 – FL93

+0

對不起,我遲到了。請刪除'system(「Pause」);' –

+0

代碼看起來非常好,還有一件事,我必須做些什麼來確保用戶能夠使用空格輸入單詞?因爲例如,如果我在obj.topic字符串上編寫多個單詞,我無法在obj.comment上輸入它直接輸入obj.stamp。問題是,我必須保持「字符串」,而不是更改爲「char []」 – FL93

3
int i=0; 
struct annotation_t obj[i]; 

你讓0大小的annotation_t對象

cin >> obj[i].topic ; 

然後試圖訪問的第一個元素的數組。

考慮使用std::vector代替,這將允許你動態地改變你的容器的大小,以允許用戶輸入儘可能多的,因爲他們想:

// Empty container 
std::vector<annotation_t> obj; 
do 
{ 
    // Create temporary 
    annotation_t temp = {}; 
    // Accept input: 
    cin >> temp.topic; 
    ... 
    // Add to container: 
    obj.push_back(temp); 
} 

在你for循環之下,你是做同樣的事情如上

for(int i = 0;i<50;i++) 
{ 
struct annotation_t obj[i]; 

另外,你正在創建一個新的容器。你可能打算使用上面的容器,這將改變你的循環:

// Store the contents of the now populated obj from above 
for (auto& a : obj) 
{ 
    myfile << a.stamp << " "; 
    myfile << a.topic << " "; 
    myfile << a.comment << std::endl; 
} 
+0

感謝答案@Tas但是如果想要維護「struct」,最好的方法是什麼? – FL93

+0

你保留'struct'。這裏沒有什麼改變'annotation_t',就像你_store_'annotation_t'對象的方式(使用'std :: vector'而不是數組)' – Tas

+0

當我得到「exit?」時,我有一個分段錯誤,我無法如果我想輸入更多,退出所有重新運行循環。 – FL93