C++程序 尝试连续运行两次写入循环时显示".. stopped working"错误

C++ Program Displays ".. stopped working" error when trying to run write loop twice in a row

本文关键字:循环 显示 working stopped 两次 错误 程序 连续 运行 C++      更新时间:2023-10-16

我目前正在开发一个程序,该程序使用结构将记录写入文件。它应该是一个将不同物品的库存保存在文件中的程序。它显示一个菜单,询问用户想要做什么。当我连续两次选择相同的选项(添加项目)时。有人能告诉我为什么会发生这种事吗?

//Kylie Sanderson
/**********************************************************************************
C++ Early Objects: Chapter 13, Page 896
15. Inventory Program
Write a program that uses a structure to store the following inventory
information in a file:
Item description
Quantity on hand
Wholesale cost
Retail cost
Date added into inventory
The program should have a menu that allows the user to perform the following tasks
Add new records to the file
Display any record in the file
Change any record in the file
Display all records in the file
************************************************************************************/
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
struct InventoryItem{
char description[25];
int quantity;
char wholesale[10];
char retail[10];
char date[10];
};
int displayMenu();
void addItem();
void displayItem();
void changeItem();
void displayAllItems();
fstream infoFile;
InventoryItem record;
int recordCount = 0;
int main(){
    int userI;
    cout << "Welcome to the Inventory Tracker." << endl;
    while(userI != 0){
        userI = displayMenu();
        if(userI == 1){
            recordCount++;
            addItem();
        }
        else if(userI == 2){
            displayItem();
        }
        else if(userI == 3){
            changeItem();
        }
        else if(userI == 4){
            displayAllItems();
        }
        else{
            return 0;
        }
    }
}
int displayMenu(){
    string tab;
    tab.assign(5, ' ');
    int response;
    cout << "What would you like to do?" << endl;
    cout << tab << "(1)Add New Item" << endl;
    cout << tab << "(2)Display Item" << endl;
    cout << tab << "(3)Change Item" << endl;
    cout << tab << "(4)Display All Items" << endl;
    cout << tab << "(0)Quit" << endl;
    cin >> response;
    cin.clear();
    cin.sync();
    return response;
}
void addItem(){
    infoFile.open("inventory.dat", ios::app | ios::binary);
    string des;
    cout << "You chose to add a new item to the inventory." << endl;
    cout << "Enter a description of the item up to 25 characters: ";
    getline(cin, des);
    for(int i = 0; i < 25; i++){
        des[i] = toupper(des[i]);
    }
    strcpy(record.description, des.c_str());
    cin.clear();
    cin.sync();
    cout << "Enter the quantity of the item: ";
    cin >> record.quantity;
    cin.clear();
    cin.sync();
    cout << "Enter the wholesale cost of the item: ";
    cin >> record.wholesale;
    cin.clear();
    cin.sync();
    cout << "Enter the retail cost of the item: ";
    cin >> record.retail;
    cin.clear();
    cin.sync();
    cout << "Enter the date of entry (dd/mm/yyyy): ";
    cin >> record.date;
    cin.clear();
    cin.sync();
    infoFile.write(reinterpret_cast<char*>(&record), sizeof(record));
    infoFile.close();
}
void displayItem(){
    cout << "You chose to display an item from the inventory." << endl;
}
void changeItem(){
    cout << "You chose to change an item in the inventory." << endl;
}
void displayAllItems(){
    infoFile.open("inventory.dat", ios::in | ios::binary);
    infoFile.seekg(0, ios::beg);
    char response;
    cout << "You chose to display all items in the inventory." << endl;
    while(!infoFile.eof()){
        infoFile.read(reinterpret_cast<char*>(&record), sizeof(record));
        cout << setw(16) << left << "Description: " << record.description << endl;
        cout << setw(16) << left << "Quantity: " << record.quantity << endl;
        cout << setw(16) << left << showpoint << setprecision(2) << "Wholesale Cost: " << record.wholesale << endl;
        cout << setw(16) << left << showpoint << setprecision(2) << "Retail Cost: " << record.retail << endl;
        cout << setw(16) << left << "Date of Entry: " << record.date << endl;
        cout << "Press any key to see the next item.";
        cin >> response;
    }
}

它对我来说运行良好,编译并运行。我遇到的问题:添加项目时,任何长度小于25个字符的描述都会引发错误。通过更改for循环参数修复。for(int i = 0; i < des.length(); i++)然后只复制前24个字符,并在末尾添加一个\0,或者扩展描述的大小,或者将其从char[]更改为std::string

不幸的是,我无法重新创建您询问的关于的错误

如果不检查输入长度并告诉程序运行以下代码块,就会损坏内存。

输入的每个字符都需要for循环,而不是支持的最大长度。

string des;
cout << "You chose to add a new item to the inventory." << endl;
cout << "Enter a description of the item up to 25 characters: ";
getline(cin, des);
for(int i = 0; i < 25; i++){
    des[i] = toupper(des[i]);
}