You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++循环录入学生信息及FormatName函数格式转换报错求助

Fixing Your FormatName Function & Completing the C++ Student Input Program

Hey there! Let's work through this problem together. It sounds like the core issue is getting your FormatName function to correctly convert "First Last" to "Last, First"—I'll break down how to implement that properly, plus show you the full program to make everything work end-to-end.

Step 1: Correct Implementation of FormatName

The FormatName function needs to take a const string (or better, const string& for efficiency) input, split it at the space between first and last name, then reformat it. Here's how to do it safely:

#include <string>
#include <stdexcept> // For handling invalid input

std::string FormatName(const std::string& fullName) {
    // Find the position of the space separating first and last name
    size_t spacePos = fullName.find(' ');
    
    // Handle cases where there's no space (invalid name format)
    if (spacePos == std::string::npos) {
        throw std::invalid_argument("Name must be in 'First Last' format");
    }
    
    // Extract first name (from start to space) and last name (space+1 to end)
    std::string firstName = fullName.substr(0, spacePos);
    std::string lastName = fullName.substr(spacePos + 1);
    
    // Return the formatted string: "Last, First"
    return lastName + ", " + firstName;
}

Key Notes About This Implementation:

  • We use const string& as the parameter to avoid copying the string unnecessarily (it's more efficient, and matches your requirement of a const string parameter).
  • We check if the space exists—this prevents crashes or incorrect output if the user enters a name without a space.
  • We use std::string::find and std::string::substr to split the string, which are standard C++ string operations.

Step 2: Full Program with Input Loop & Output

Now let's build the rest of the program: a loop to collect student names and grades, store them, then format and display all entries. We'll use a std::vector to store pairs of formatted names and grades.

#include <iostream>
#include <vector>
#include <utility> // For std::pair

int main() {
    std::vector<std::pair<std::string, int>> students;
    std::string inputName;
    int grade;
    
    std::cout << "Enter student names (format: First Last) and grades. Type 'quit' to stop.\n";
    
    while (true) {
        std::cout << "Enter name: ";
        std::getline(std::cin, inputName);
        
        // Check if user wants to quit
        if (inputName == "quit") {
            break;
        }
        
        // Get the grade
        std::cout << "Enter grade for " << inputName << ": ";
        std::cin >> grade;
        std::cin.ignore(); // Clear the newline from input buffer
        
        try {
            std::string formattedName = FormatName(inputName);
            students.emplace_back(formattedName, grade);
        } catch (const std::invalid_argument& e) {
            std::cerr << "Error: " << e.what() << " Please try again.\n";
        }
    }
    
    // Display all students
    std::cout << "\n--- Student List ---\n";
    for (const auto& student : students) {
        std::cout << student.first << ": " << student.second << "\n";
    }
    
    return 0;
}

Important Details in the Main Program:

  • We use std::getline to read the full name (since it includes a space—using cin >> inputName would only read the first word).
  • After reading the grade with cin >> grade, we call cin.ignore() to clear the newline character left in the input buffer, which would otherwise mess up the next getline call.
  • We wrap the FormatName call in a try/catch block to handle invalid name inputs gracefully.

Common Pitfalls to Avoid

  • Forgetting to handle the newline after reading the grade: This is a super common issue when mixing cin >> and getline. Always call cin.ignore() after using cin >> to read a value before using getline again.
  • Not checking for the space in FormatName: If a user enters a single-word name, your function would behave unexpectedly—adding the error check prevents that.
  • Using pass-by-value instead of pass-by-const-reference: While const string works, const string& is more efficient because it doesn't make a copy of the string.

内容的提问来源于stack exchange,提问作者Holly Thorsted

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:55:32