字符串标记程序,定界符和使用文件输入制作对象

问题描述

我有一个文件,其中包含代码中给出的多行示例数据。每行都是一个对象。我一直在尝试使用字符串令牌生成器,但是我一直遇到错误

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string input = "kevin hill;8;8;jacky knight;5;6;alejandro wilson;jordan walls;6;layla penn;7;mindy kaling;9;jon adams;8;";

    std::istringstream ss(input);
    std::string token;

    std::string mN,fN,mgr,psy,physio,tCo,fCo;
    int mSta,mStr,fSta,fStr,psyS,physioS,tCoS,fCoS;

    struct points
    {
        std::string athName,sportName;
        int totPoints,sc;
        points(int totPoints,int sc,std::string athName,std::string sportName)
        {
            this->totPoints = totPoints;
            this->sc = sc;
            this->athName = athName;
            this->sportName = sportName;
        }
    };

    while (getline(ss,token,';'))
    {
        mN >> mSta >> mStr >> fN >> fSta >> fStr >> mgr >> psy >> psyS >> physio >> physioS >> tCo >> tCoS >> fCo >> fCoS;
    }

    points *one = new points(mSta,mN,fN);

    std::cout << one->athName << std::endl;
}

在while循环开始时出现错误,因为mN给“没有运算符匹配这些操作数”错误后出现>>。当我使用istringstream ss启动while循环时:

ss >> mN >> mSta >> .....

它执行,但是跳过名字,将8; 8; jacky作为一个字符串读取,我想这是因为它试图完成一个字符串,但即使那样,它也会跳过定界符,因为它停止在空白处读取。 我不知道这里发生了什么。如何使用定界符读取不同的数据类型并使用它们创建对象?有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)