c++ 我在CPP中进行编码,以递增方式计算一个单词在文本文件中出现的次数,哪里出错了?[关闭]

i34xakig  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(204)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
8天前关闭
Improve this question

**编辑:**代码使用SO回复的提示更新。编译时没有错误,列表打印为空。数量没有更新。
**原帖子:**这是为学校准备的,我似乎无法让它增加项目。它成功地打开了文件,但随后打印的事件为0,即使我在菜单中输入的每个项目至少有1。这不是我的全部代码,但它是我的类的代码,因为我认为这可能是我出错的地方。此外,我的菜单不会识别无效输入,即使if/else的else部分在那里捕获它。它只是进入了一个打印菜单的无限循环。我对所有的反馈和/或链接的资源比Zybooks更好地教我什么,我在这里错过了。

  1. class ItemCounter {
  2. private:
  3. map <string, int> itemFrequency;
  4. string inputFile = "CS210_Project_Three_Input_File.txt";
  5. string outputFile = "itemTracker.dat";
  6. public:
  7. void loadInputFile() {
  8. ifstream file(inputFile);
  9. // OPEN conditions
  10. if (file.is_open()) {
  11. string itemName;
  12. while (getline(file, itemName)) {
  13. int itemQuantity = 0;
  14. // Update item count
  15. itemFrequency[itemName] += itemQuantity;
  16. // Exception check for string conversion
  17. try {
  18. itemQuantity = stoi(itemName);
  19. }
  20. catch (const invalid_argument& e) {
  21. cout << "Error: Invalid Entry Found in Input File." << endl;
  22. continue;
  23. }
  24. }
  25. // Close open file
  26. file.close();
  27. }
  28. // Else statement if file won't open
  29. else {
  30. cout << "Error: Could Not Open Input File" << endl;
  31. cout << endl;
  32. }
  33. }
  34. // Function to save new data to external file
  35. void saveOutputFile() {
  36. ofstream file(outputFile);
  37. if (file.is_open()) {
  38. for (auto& item : itemFrequency) {
  39. file << item.first << " " << item.second << endl;
  40. }
  41. file.close();
  42. }
  43. else {
  44. cout << "Error: Could Not Open Output File - Data Not Saved" << endl;
  45. cout << endl;
  46. }
  47. }
  48. // Function to display item occurrences
  49. void displayItemCount(string itemName) {
  50. int count = itemFrequency[itemName];
  51. cout << "Total Item Occurances for " << itemName << ": " << count << endl;
  52. }
  53. //Function to count and display total item counts of whole list
  54. void displayTotals () {
  55. for (auto& item : itemFrequency) {
  56. cout << item.first << " " << item.second << endl;
  57. }
  58. }
  59. // Function to count items in the list and print as histogram
  60. void displayItemsHistogram() {
  61. for (auto& item : itemFrequency) {
  62. cout << item.first << " ";
  63. for (int i = 0; i < item.second; i++) {
  64. cout << "*";
  65. }
  66. cout << endl;
  67. }
  68. }
  69. };
  70. // Function to save new data to external file
  71. void saveOutputFile() {
  72. ofstream file(outputFile);
  73. if (file.is_open()) {
  74. for (auto& item : itemFrequency) {
  75. file << item.first << " " << item.second << endl;
  76. }
  77. file.close();
  78. }
  79. else {
  80. cout << "Error: Could Not Open Output File - Data Not Saved" << endl;
  81. cout << endl;
  82. }
  83. }
  84. };
  85. int main() {
  86. ItemCounter itemCounter;
  87. itemCounter.loadInputFile();
  88. itemCounter.saveOutputFile();
  89. int menuChoice = 0;
  90. while (menuChoice != 4) {
  91. cout << "Main Menu" << endl;
  92. cout << endl;
  93. cout << "1. Stuff" << endl;
  94. cout << "2. Stuff" << endl;
  95. cout << "3. Stuff" << endl;
  96. cout << "4. Exit" << endl;
  97. cout << "Please make a selection: ";
  98. cin >> menuChoice;
  99. cout << endl;
  100. if (menuChoice == 1) {
  101. string itemName;
  102. cout << "Enter search term: ";
  103. cin >> itemName;
  104. cout << "FIX ME:" << endl;
  105. }
  106. else if (menuChoice == 2) {
  107. cout << "FIX ME" << endl;
  108. }
  109. else if (menuChoice == 3) {
  110. cout << "FIX ME" << endl;
  111. }
  112. else if (menuChoice == 4) {
  113. cout << "Program Terminated" << endl;
  114. }
  115. else {
  116. cout << "Invalid Selection, try again." << endl;
  117. }
  118. }
  119. return 0;
  120. }

w8rqjzmb

w8rqjzmb1#

计数发生次数的问题:
loadInputFile()函数中,在使用itemQuantity递增itemFrequency[itemName]之前,您将初始化itemQuantity,而不对其分配任何值。这意味着您正在使用未初始化的值进行递增,从而导致不正确的结果。
要解决这个问题,您应该将itemCountStr转换为整数,并在递增itemFrequency[itemName]之前将其分配给itemQuantity
替换行:

  1. int itemQuantity;

其中:

  1. int itemQuantity = stoi(itemCountStr);

无效输入处理问题:
main()while循环中,如果用户输入了无效的选择(1、2、3或4以外的值),程序不应无限期地继续打印菜单。
要解决这个问题,可以在else块中添加break;语句以退出循环。
替换:

  1. else {
  2. cout << "Invalid Selection, try again." << endl;
  3. }

其中:

  1. else {
  2. cout << "Invalid Selection, try again." << endl;
  3. break;
  4. }

通过这些修改,代码现在应该正确地增加项的出现次数,并在必要时通过中断菜单循环来处理无效输入。

展开查看全部

相关问题