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

C++读取嵌套结构体时嵌套字段重复输出的问题求助

C++读取嵌套结构体时嵌套字段重复输出的问题求助

看起来你遇到了嵌套结构体读取后输出重复的问题,我帮你分析一下可能的原因和解决办法:

问题分析

  1. 函数嵌套定义的语法错误
    你把readFile函数直接定义在了main函数内部,这在C++里是不允许的,会导致编译失败。必须把函数定义放在全局作用域,或者在main前声明函数原型。

  2. 输出重复的核心原因
    从你的输出结果来看,所有条目的Title和Year Published都是第一组的BAD 1990,很大概率是你显示数据的时候用错了变量。看你的main函数里定义了一个全局的Discography records;,如果你的显示代码(没贴出来)输出的是这个全局变量的内容,而不是数组music[i]里的music[i].records成员,那自然会重复同一个值。

  3. 文件读取循环的潜在问题
    使用while (!inputMusic.eof())作为循环条件是个常见的坑——EOF标志只有在读取操作失败后才会被设置,所以当读到文件末尾时,循环会多执行一次,导致count值超出实际条目数,可能出现垃圾数据或者重复读取的情况。

修正后的代码示例

我帮你调整了代码,修复了上述问题,并且添加了正确的显示函数:

#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<cstdlib> // 用于exit()

using namespace std;

struct Discography {
    string title;
    int yearPub;
};

struct Collection {
    string name;
    string genres;
    int startYear;
    int endYear;
    Discography records;
};

// 函数原型放在main之前
void readFile(ifstream&, string, Collection [], int&);
void displayAll(Collection [], int);

const int DATA_FILE = 10;

int main() {
    ifstream inputMusic;
    Collection music[DATA_FILE];
    int count = 0;

    const int DISPLAY_ALL = 1,
              SEARCH_ARTIST_NAME = 2,
              SEARCH_GENRE = 3,
              SEARCH_TITLE = 4,
              SEARCH_YEAR = 5,
              QUIT_CHOICE = 6;
    int choice;
    string nameArtist, results;

    readFile(inputMusic, "My_Artists.txt", music, count);
    displayAll(music, count); // 调用显示函数

    // 这里可以添加你的菜单逻辑
    return 0;
}

// readFile函数定义放在全局作用域
void readFile(ifstream& inputMusic, string data, Collection music[], int &count) {
    inputMusic.open(data);
    if (!inputMusic) {
        cout << "Error in opening file\n";
        exit(1);
    } else {
        // 把读取操作放在循环条件里,避免多读
        while (count < DATA_FILE && 
               inputMusic >> music[count].name
                         >> music[count].genres
                         >> music[count].startYear
                         >> music[count].endYear
                         >> music[count].records.title
                         >> music[count].records.yearPub) {
            count++;
        }
        inputMusic.close();
    }
}

// 正确的显示函数,遍历数组输出每个元素的嵌套结构体
void displayAll(Collection music[], int count) {
    for (int i = 0; i < count; i++) {
        cout << "Name : " << music[i].name << endl;
        cout << "Genre: " << music[i].genres << endl;
        cout << "Start Year: " << music[i].startYear << endl;
        cout << "End Year: " << music[i].endYear << endl;
        cout << "Title: " << music[i].records.title << "  Year Published: " << music[i].records.yearPub << endl;
        cout << "----------------------------------------------------\n";
    }
}

额外注意事项

  • 你的输入文件格式是没问题的,因为>>运算符会自动跳过空格、换行等空白字符,所以每行一个字段或者标题和年份在同一行都能正确读取。
  • 记得添加数组越界检查(比如循环里的count < DATA_FILE),避免超出数组容量导致的内存错误。

备注:内容来源于stack exchange,提问作者Ronin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 07:03:09