// 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();
当我尝试将数组写入文件时,并非我输入到数组中的所有内容都会输出到文件中。在这种情况下,我已经将两个东西输入到数组猫然后老鼠。问题是,我的程序只输出猫而不是老鼠。我能想到的唯一可能的问题是for循环设置不正确。但我认为情况并非如此,因为 for 循环中的“计数器”显然设置正确 - 该文件甚至显示数组中的事物数量。所以重申一下,为什么不是我输入到数组中的所有内容都显示在文本文件中。
Txt 文件输出:catsandrats 有 2 种成分。猫