C++姓名数组功能完善求助:实现查询修改及最终列表展示
Hey there! Let's fix up your code step by step so it meets all your requirements. Right now we've got two main gaps: your check function only prints the modified name instead of updating the array, and you're never calling the display function when the user hits "q". Plus, we need to tweak the matching logic to make sure we're only targeting the actual first name (not random substrings) and avoid re-modifying names that already have the "(c)" tag.
Modified Complete Code
#include <iostream> #include <string> using namespace std; void check(string[], int, string); void display(string[], int); int main() { string lookup; const int arraySize = 5; string names[arraySize] = { "Sergio Ramos", "Federico Valverde", "Edin Hazard", "Carlos Henrico", "Ferland Mendy" }; do { cout << "Type first name to lookup, q to quit: "; cin >> lookup; // Only run check if user didn't enter q if (lookup != "q") { check(names, arraySize, lookup); } } while (lookup != "q"); // After user quits, display the full array cout << "\nFinal list of names:\n"; display(names, arraySize); return 0; } void check(string str[], int size, string firstNameToFind) { for (int i = 0; i < size; i++) { string& fullName = str[i]; // Use reference to modify the original array element // Skip if the name is already modified (has (c)) if (fullName.find("(c)") != string::npos) { continue; } // Find the space separating first and last name size_t spacePos = fullName.find(' '); if (spacePos == string::npos) { continue; // Skip if no space (though our initial array doesn't have this) } // Extract the first name from the full name string actualFirstName = fullName.substr(0, spacePos); // Only match if the input exactly matches the first name (not a substring) if (actualFirstName == firstNameToFind) { string lastName = fullName.substr(spacePos + 1); // Update the array element with flipped name + (c) fullName = lastName + " " + actualFirstName + "(c)"; // Optional: Confirm the modification to the user cout << "Updated: " << fullName << endl; } } } void display(string str[], int size) { for (int i = 0; i < size; i++) { cout << "- " << str[i] << endl; } }
Key Fixes & Improvements
- Main Function Adjustment: We now only call
checkwhen the input isn't "q", and after the loop exits (user pressed "q"), we explicitly calldisplayto show the final array. - Check Function Overhaul:
- We use a reference (
string& fullName = str[i]) to modify the original array element directly, instead of just printing the modified name. - Added a check to skip names that already have "(c)" so we don't re-modify them if the user enters the same first name multiple times.
- Fixed the matching logic: we extract the actual first name from the full name and do an exact match, instead of using
findwhich could match substrings (e.g., input "erg" would incorrectly match "Sergio" before).
- We use a reference (
- Display Function Implementation: Now it loops through the array and prints each name in a clean list format, as requested.
How It Works
- When you enter a first name like "Sergio", the function finds the matching entry, flips first/last name, adds "(c)", and updates the array.
- If you enter the same first name again, it skips modifying it since "(c)" is already present.
- When you type "q", the loop ends, and the full updated array is printed as a list.
内容的提问来源于stack exchange,提问作者thebigZ25
相关产品推荐
相关产品推荐

