【发布时间】:2017-03-07 13:20:02
【问题描述】:
您好,我是一名初级程序员,我需要一些帮助来完成我的部分作业。任务是制作一个菜单驱动程序,用户可以通过以下方式操作他们的文件:
- 查找用户输入的单词及其所在的行
- 使用数组从文件中删除用户输入的单词并将新数组写入新文件。每次用户从文件中删除一个单词时,他们都会将旧文件的内容减去删除的单词写入一个新文件。第一个输出文件是 filename1.txt,然后第二个输出文件是 filename2.txt,依此类推。
- 最后将用户输入的单词替换为用户输入的另一个单词。
这里有一些信息可以帮助你:
我正在使用 Microsoft Visual Studio
到目前为止课堂上已经涵盖的内容:
-if, else if, else
-while, do-while, and for loops
-switch statements
-arrays, char arrays, and 2d arrays
-functions
-just started pointers
课堂上尚未涉及的内容:
-haven't done vectors yet
-haven't done classes yet
已完成的任务:
-Read from user File into a string array
-searched for word user enters with linear search
-replace a word deleted by the user with new word entered by the user
待办事项:
In case 2 (delete a word from the file):
-I need to get rid of the empty array element when I delete a word from the array.
-let the user delete a word again, then save it into a new output file (ie: filname1.txt,fileName2.txt,fileName3.txt. etc)
-Formatting code
create functions for everything I need, I can do this part i am just coding it without for ease of understanding
我最需要关于 switch case 2 的帮助。 这是我迄今为止针对案例 2 的代码:
case '2':
{
string d_newDictionaryArray[dictionarySize];
cout << "Enter a word to delete: ";
cin >> word;
ofstream doutFile;
doutFile.open("dictionary_deleted_word.txt");
// copying from one one array to another
for (int i = 0; i < dictionarySize; i++)
{
d_newDictionaryArray[i] = dictionaryArray[i];
}
// searching and deleting the word from the new array
for (int j = 0; j < dictionarySize; j++)
{
if (word == d_newDictionaryArray[j])
{
d_newDictionaryArray[j] = "";
cout << "The word " << word << " has been deleted :)" << endl;
}
}
// Write to a new file
for (int k = 0; k < dictionarySize; k++)
{
doutFile << d_newDictionaryArray[k] << "\r\n";
}
doutFile.close();
break;
}
【问题讨论】:
-
欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。
标签: c++ c++11 visual-c++