【问题标题】:Loop Issues with objects in C++C++ 中对象的循环问题
【发布时间】:2016-05-23 02:04:19
【问题描述】:

我必须创建一个简单的代码,它应该创建一个 .txt 文件作为输出,其中包含具有这种格式的符号列表。 (时间;主题;评论) 代码必须使用如下所示的结构函数运行循环:

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 中,或者如果我想输入更多内容,请重新运行它。让我知道你的想法。谢谢

【问题讨论】:

  • 看@Tas 的回答。您可能希望在第二个循环中将变量名称从 i 更改为 j 以避免混淆。
  • 请不要污蔑你的问题
  • Paulit 这是我试图标记为正面的错误

标签: c++ loops object while-loop ofstream


【解决方案1】:
int i=0;
struct annotation_t obj[i];

您正在创建一个annotation_t 对象数组,其大小为0

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;
}

【讨论】:

  • 感谢@Tas 的回答,但如果想维护“struct”,最好的方法是什么?
  • 您保留struct。 annotation_t 这里没有任何变化,就像您存储 annotation_t 对象的方式(使用std::vector 而不是数组)
  • 当我进入“退出?”时出现分段错误,如果我想输入更多内容,我无法退出所有重新运行循环。
  • 您是否更新了循环的两个实例?在do while 下方,您还有另一个for 循环,它也创建了一个空数组。您可能想继续使用上面的矢量
  • 我没有在我的 while 循环之后更新 for 循环,看起来我已经以一种我无法继续的方式设置了我的“q”值。 @Tas
【解决方案2】:

您在声明“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");

}

我在许多用“//msf”注释的位置更改了您的代码。 我手头没有 C++ 编译器,希望编译不会出错。

【讨论】:

  • 当我“退出?”时出现分段错误错误
  • 对不起,我迟到了。请删除行'system("Pause");'
  • 代码看起来很不错,还有一件事,我必须做些什么来确保用户能够使用空格输入单词?因为例如,如果我在 obj.topic 字符串上写了多个单词,我无法在 obj.comment 中输入,它会直接进入 obj.stamp。问题是我必须保留“字符串”而不是更改为“字符 []”
  • 使用这个:'std::getline(cin, obj[i].topic);'而不是:'cin >> obj[i].topic;'
  • 但在这种情况下,在下一次迭代中,它将直接转到 obj.comment,在“exit”之后跳过 obj.topic
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
相关资源
最近更新 更多