C++ Dynamically Allocated Array; Size set by quantity of user input;
Writing to a file;
// lets user input ingredients; but when "n" is inputted it
terminates the loop
string test;
static int counter = 0;
string* gredients = new string[counter];
string newingredients;
while (test != "no")
{
getline(cin,newingredients);
gredients[counter] = newingredients;
if (newingredients == "n"){test = "no";}
counter++;
}
// write ingredients to file
int counter3=1;
ofstream ob;
ob.open(recipeName+".txt");
// counter - 1 is so, because i do not want it to output n into
the file
ob << recipeName << " has "<< counter-1 << " ingredients." << endl;
for(int a = 0; a <= counter-1 ; a++)
{
ob << gredients[a] << endl;
}
ob.close();
When I try to write the array to a file, not everything i've inputted into
the array is outputted into the file. In this case i've inputted two
things into the array cats then rats. The problem is, my program only
outputs cats but not rats. The only possible problem i can think of is
that the for loop is not set properly. But I dont think that's the case
because the 'counter' in the for loop is clearly set correctly - the file
even displays the number of things within the array. So to reiterate, why
is it that not everything i've inputted into the array is showing up in
the text file.
Txt File Output: catsandrats has 2 ingredients. cats
No comments:
Post a Comment