C++ CRUD程序字符串输入异常及记录操作问题求助
Hey there! Let's break down the issues in your C++ CRUD program one by one and fix them up. I can already spot a few critical problems that are causing your crashes and unexpected behavior.
The biggest issue in your code is how you're storing records:
You declared arrays like
int id[record];whererecordstarts at 0. First off, C++ doesn't support variable-length arrays (VLAs)—this is a C-only feature, and using it in C++ leads to undefined behavior. Even worse, an array of size 0 can't hold any data at all, so when you try to add records, you're writing to invalid memory, which crashes the program or makes records disappear entirely.
The fix here is to use std::vector instead of fixed-size arrays. Vectors dynamically adjust their size as you add/remove data, so you never have to worry about memory bounds issues.
If you're mixing cin >> (for reading chars/ints/doubles) with getline for strings, the newline character left in the input buffer by cin >> will cause getline to read an empty string immediately. This makes it look like the program stopped responding when you try to enter a name.
To fix this, add cin.ignore(numeric_limits<streamsize>::max(), '\n'); after every cin >> call to clear the buffer before using getline. You'll need to include the <limits> header for this to work.
Overwriting a record with "none" is a workaround, not a proper deletion. With vectors, you can remove the record entirely from your data set using vector::erase, which cleans up your data properly instead of leaving placeholder values.
Here's the full, fixed version of your program with all these changes applied:
#include <iostream> #include <vector> #include <string> #include <limits> // Required for input buffer clearing using namespace std; int main() { char choice; // Use vectors to store records dynamically vector<int> id; vector<string> name; vector<double> price; do { cout << "\n=== CRUD Menu ===" << endl; cout << "1. Add Record" << endl; cout << "2. View Single Record" << endl; cout << "3. Edit Record" << endl; cout << "4. Delete Record" << endl; cout << "5. View All Records" << endl; cout << "6. Exit" << endl; cout << "Enter your choice: "; cin >> choice; cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear buffer after input switch(choice) { case '1': { int newId; string newName; double newPrice; cout << "\nEnter ID: "; cin >> newId; cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Enter Name: "; getline(cin, newName); cout << "Enter Price: "; cin >> newPrice; cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Add new record to vectors id.push_back(newId); name.push_back(newName); price.push_back(newPrice); cout << "Record added successfully!" << endl; break; } case '2': { int searchId; cout << "\nEnter ID to view: "; cin >> searchId; bool found = false; for (int i = 0; i < id.size(); ++i) { if (id[i] == searchId) { cout << "\nRecord Found:" << endl; cout << "ID: " << id[i] << endl; cout << "Name: " << name[i] << endl; cout << "Price: " << price[i] << endl; found = true; break; } } if (!found) { cout << "Record with ID " << searchId << " not found!" << endl; } break; } case '3': { int editId; cout << "\nEnter ID to edit: "; cin >> editId; bool found = false; for (int i = 0; i < id.size(); ++i) { if (id[i] == editId) { cout << "\nCurrent Name: " << name[i] << endl; cout << "Enter new Name: "; cin.ignore(numeric_limits<streamsize>::max(), '\n'); getline(cin, name[i]); cout << "Current Price: " << price[i] << endl; cout << "Enter new Price: "; cin >> price[i]; cout << "Record updated successfully!" << endl; found = true; break; } } if (!found) { cout << "Record with ID " << editId << " not found!" << endl; } break; } case '4': { int deleteId; cout << "\nEnter ID to delete: "; cin >> deleteId; bool found = false; // Iterate through vectors to find and delete the record for (auto it = id.begin(); it != id.end(); ++it) { int index = it - id.begin(); if (*it == deleteId) { id.erase(it); name.erase(name.begin() + index); price.erase(price.begin() + index); cout << "Record deleted successfully!" << endl; found = true; break; } } if (!found) { cout << "Record with ID " << deleteId << " not found!" << endl; } break; } case '5': { if (id.empty()) { cout << "\nNo records to display!" << endl; break; } cout << "\n=== All Records ===" << endl; for (int i = 0; i < id.size(); ++i) { cout << "\nRecord " << i+1 << ":" << endl; cout << "ID: " << id[i] << endl; cout << "Name: " << name[i] << endl; cout << "Price: " << price[i] << endl; } break; } case '6': cout << "Exiting program..." << endl; break; default: cout << "Invalid choice! Please enter a number between 1-6." << endl; } } while (choice != '6'); return 0; }
Key Changes Explained:
- Replaced fixed-size arrays with
std::vectorto safely handle dynamic record counts - Added
cin.ignore()calls to clear input buffer conflicts betweencin >>andgetline - Implemented proper record deletion using
vector::eraseinstead of placeholder values - Added checks for empty records when viewing all entries to avoid confusing output
内容的提问来源于stack exchange,提问作者Mark

