**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答复。
此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
8天前关闭
Improve this question
**编辑:**代码使用SO回复的提示更新。编译时没有错误,列表打印为空。数量没有更新。
**原帖子:**这是为学校准备的,我似乎无法让它增加项目。它成功地打开了文件,但随后打印的事件为0,即使我在菜单中输入的每个项目至少有1。这不是我的全部代码,但它是我的类的代码,因为我认为这可能是我出错的地方。此外,我的菜单不会识别无效输入,即使if/else的else部分在那里捕获它。它只是进入了一个打印菜单的无限循环。我对所有的反馈和/或链接的资源比Zybooks更好地教我什么,我在这里错过了。
class ItemCounter {
private:
map <string, int> itemFrequency;
string inputFile = "CS210_Project_Three_Input_File.txt";
string outputFile = "itemTracker.dat";
public:
void loadInputFile() {
ifstream file(inputFile);
// OPEN conditions
if (file.is_open()) {
string itemName;
while (getline(file, itemName)) {
int itemQuantity = 0;
// Update item count
itemFrequency[itemName] += itemQuantity;
// Exception check for string conversion
try {
itemQuantity = stoi(itemName);
}
catch (const invalid_argument& e) {
cout << "Error: Invalid Entry Found in Input File." << endl;
continue;
}
}
// Close open file
file.close();
}
// Else statement if file won't open
else {
cout << "Error: Could Not Open Input File" << endl;
cout << endl;
}
}
// Function to save new data to external file
void saveOutputFile() {
ofstream file(outputFile);
if (file.is_open()) {
for (auto& item : itemFrequency) {
file << item.first << " " << item.second << endl;
}
file.close();
}
else {
cout << "Error: Could Not Open Output File - Data Not Saved" << endl;
cout << endl;
}
}
// Function to display item occurrences
void displayItemCount(string itemName) {
int count = itemFrequency[itemName];
cout << "Total Item Occurances for " << itemName << ": " << count << endl;
}
//Function to count and display total item counts of whole list
void displayTotals () {
for (auto& item : itemFrequency) {
cout << item.first << " " << item.second << endl;
}
}
// Function to count items in the list and print as histogram
void displayItemsHistogram() {
for (auto& item : itemFrequency) {
cout << item.first << " ";
for (int i = 0; i < item.second; i++) {
cout << "*";
}
cout << endl;
}
}
};
// Function to save new data to external file
void saveOutputFile() {
ofstream file(outputFile);
if (file.is_open()) {
for (auto& item : itemFrequency) {
file << item.first << " " << item.second << endl;
}
file.close();
}
else {
cout << "Error: Could Not Open Output File - Data Not Saved" << endl;
cout << endl;
}
}
};
int main() {
ItemCounter itemCounter;
itemCounter.loadInputFile();
itemCounter.saveOutputFile();
int menuChoice = 0;
while (menuChoice != 4) {
cout << "Main Menu" << endl;
cout << endl;
cout << "1. Stuff" << endl;
cout << "2. Stuff" << endl;
cout << "3. Stuff" << endl;
cout << "4. Exit" << endl;
cout << "Please make a selection: ";
cin >> menuChoice;
cout << endl;
if (menuChoice == 1) {
string itemName;
cout << "Enter search term: ";
cin >> itemName;
cout << "FIX ME:" << endl;
}
else if (menuChoice == 2) {
cout << "FIX ME" << endl;
}
else if (menuChoice == 3) {
cout << "FIX ME" << endl;
}
else if (menuChoice == 4) {
cout << "Program Terminated" << endl;
}
else {
cout << "Invalid Selection, try again." << endl;
}
}
return 0;
}
1条答案
按热度按时间w8rqjzmb1#
计数发生次数的问题:
在
loadInputFile()
函数中,在使用itemQuantity
递增itemFrequency[itemName]
之前,您将初始化itemQuantity
,而不对其分配任何值。这意味着您正在使用未初始化的值进行递增,从而导致不正确的结果。要解决这个问题,您应该将
itemCountStr
转换为整数,并在递增itemFrequency[itemName]
之前将其分配给itemQuantity
。替换行:
其中:
无效输入处理问题:
在
main()
的while
循环中,如果用户输入了无效的选择(1、2、3或4以外的值),程序不应无限期地继续打印菜单。要解决这个问题,可以在
else
块中添加break;
语句以退出循环。替换:
其中:
通过这些修改,代码现在应该正确地增加项的出现次数,并在必要时通过中断菜单循环来处理无效输入。