这有什么问题吗?这部分代码与其他可用的选项格式相同,但这一部分不能写入输出文件。
else if (choice1 == 4) {
int nodes[100];
int b = 0;
int number;
cout << "Please give the nodes that you want to include in the sub-graph.\n"
<< "(press -1 to stop input): ";
do {
cin >> number;
nodes[b++] = number;
b++;
} while (number != (-1));
outfile.open("output.txt", ::ofstream::in | ::ofstream::out | ::ofstream::app);
if (outfile.is_open()) {
outfile << "nodes[0]" << nodes[1] << nodes[2];
}
cout << endl << "Report stored in file: output.txt." << endl;
}
答案 0 :(得分:1)
你有:
do {
cin >> number;
nodes[b++] = number;
b++;
} while (number != (-1));
我在这个do-while
循环中看到了代码的几个问题。
nodes
中。根据我的理解,-1是一个停止的信号。存储它没有意义。b
两次。也许这是你的意图,也许这是一个疏忽的错误。上面的代码块可以更改为:
while (true)
{
cin >> number;
if ( number == -1 )
{
break;
}
nodes[b++] = number;
}
答案 1 :(得分:0)
似乎一切都是正确的,但你错过了一些东西。为了使更改生效,您需要正确关闭文件。
使用outfile.close();
else if(choice1==4)
{
int nodes[100];
int b=0;
int number;
cout<< "Please give the nodes that you want to include in the sub-graph.\n (press -1 to stop input): ";
do
{
cin>>number;
nodes[b++]=number;
b++;
}
while(number!= (-1));
outfile.open ("output.txt", ::ofstream::in | ::ofstream::out | ::ofstream::app);
if (outfile.is_open())
{
outfile<<"nodes[0]"<<nodes[1]<<nodes[2];
}
outfile.close();
cout<<endl<<"Report stored in file: output.txt."<<endl;
}