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

C++购物车链表项目:使用getline解析字符串存链表遇问题求助

排查C++购物车链表项目的运行异常

我来帮你搞定这个运行异常的问题!结合你描述的场景——按字母顺序插入链表、用getline()和stringstream读取带格式的文本文件——大概率是文件读取缓冲区残留、字符串分割格式处理不当或者链表插入时的空指针访问这几个环节出了问题,咱们逐个拆解解决:

1. 先解决最常见的「换行符残留」问题

如果你是先用cin读取首行的预算(比如double budget; cin >> budget;),那输入缓冲区里会留下一个换行符,导致第一个getline()直接读到空行,后续的字符串分割逻辑直接炸锅。

解决办法:读完预算后立刻清空输入缓冲区,记得要包含<limits>头文件:

#include <limits>
// ...
double budget;
cin >> budget;
// 清空缓冲区里的换行符,避免干扰后续getline
cin.ignore(numeric_limits<streamsize>::max(), '\n');

2. 修复stringstream的字符串分割逻辑

每行格式是「Itemname, itemcost」,如果直接粗暴分割,很容易因为逗号前后的空格、格式错误导致转换失败。这里给你一个健壮的分割写法:

string line;
while (getline(cin, line)) {
    stringstream ss(line);
    string itemName;
    double itemCost;
    
    // 读取到逗号为止,拿到商品名
    if (!getline(ss, itemName, ',')) {
        cerr << "无效行格式:" << line << endl;
        continue;
    }
    // 跳过逗号后面的空格,读取价格
    if (!(ss >> itemCost)) {
        cerr << "价格转换失败:" << line << endl;
        continue;
    }
    
    // 可选:去掉商品名前后的空格(比如行里写的是"  Milk  , 5.99")
    itemName.erase(itemName.begin(), find_if(itemName.begin(), itemName.end(), [](int ch) {
        return !isspace(ch);
    }));
    itemName.erase(find_if(itemName.rbegin(), itemName.rend(), [](int ch) {
        return !isspace(ch);
    }).base(), itemName.end());
    
    // 现在调用insertNode插入链表
    head = insertNode(head, itemName, itemCost);
}

这里关键是加了格式校验,一旦某行格式不对,直接跳过并打印错误,不会影响后续读取。

3. 避免链表插入时的空指针异常

按字母顺序插入链表时,一定要处理头节点为空和插入到链表尾部的情况,不然很容易访问空指针的next成员。给你一个安全的insertNode实现:

struct Node {
    string itemname;
    double itemcost;
    Node* next;
    // 构造函数简化节点创建
    Node(string name, double cost) : itemname(name), itemcost(cost), next(nullptr) {}
};

Node* insertNode(Node* head, string name, double cost) {
    Node* newNode = new Node(name, cost);
    
    // 情况1:链表为空,或者新节点应该插在头部
    if (head == nullptr || name < head->itemname) {
        newNode->next = head;
        return newNode;
    }
    
    // 情况2:找合适的插入位置,停在要插入节点的前一个位置
    Node* current = head;
    while (current->next != nullptr && current->next->itemname < name) {
        current = current->next;
    }
    
    // 插入新节点
    newNode->next = current->next;
    current->next = newNode;
    return head;
}

注意这里的循环条件是current->next != nullptr,而不是current != nullptr——这样就能避免遍历到链表末尾时,current变成空指针后访问current->next的致命错误。

额外提醒

  • 确保你包含了所有需要的头文件:<iostream>, <fstream>, <sstream>, <string>, <algorithm>(用于trim的find_if), <limits>;
  • 如果是从文件读取而不是标准输入,记得用ifstream打开文件,并且检查文件是否成功打开:
ifstream inputFile("shopping.txt");
if (!inputFile.is_open()) {
    cerr << "无法打开文件!" << endl;
    return 1;
}
// 之后把cin换成inputFile即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:41:45