Reputation: 1
Are there any problems with this? This part of the code is the same type of format as the other options that work but this one isn't writing to the output file.
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;
}
Upvotes: 0
Views: 94
Reputation: 206657
You have:
do {
cin >> number;
nodes[b++] = number;
b++;
} while (number != (-1));
I see couple of problems with the code in this do-while
loop.
nodes
. From what I understood, -1 is a signal to stop. It doesn't make sense to store it.b
twice in the loop. Maybe that's your intention, maybe it is an error of oversight.The above block of code can be changed to:
while (true)
{
cin >> number;
if ( number == -1 )
{
break;
}
nodes[b++] = number;
}
Upvotes: 1
Reputation: 1295
It seems like everything is correct, but you're missing something. In order for the changes to take effect you want to close the file properly.
Use 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;
}
Upvotes: 0