在C ++

时间:2018-10-05 08:07:35

标签: c++ arrays structure ifstream

这是一项作业,需要我使用ifstream来传输 CSV 文件。此csv文件包含52个州名称和每个州使用的不同资源的数量。例如:

  

阿拉巴马州,410.20,715.70,169.40,18.00,44.90,309.10,11.90,417.30,64.50,167.40,23.70,0.10,0.40,0.00

然后,我需要提示用户键入状态名称,并且输出是已使用资源的百分比。

我创建了一个包含字符串类型和存储每个状态值的数组的结构,并创建了一个存储多个状态数据的结构数组,但是我不确定我的代码是否正确,我想知道如何当用户输入状态名称时访问其他数据,例如我的double数组中的数据存储。 这是我的代码:

struct statData 
{
    string statename;
    double StatDataNumber[14];

}DataStruc[51];

int main()
{

    ifstream indata;
    string line;
    statData State;
    State.statename;
    statData Consumption;
    Consumption.StatDataNumber;


    indata.open("Data2016.csv");    //opening file
    if (indata.fail())  //fail safe
    {
        cout << "Fail to open file";
        exit(1);
    }
    getline(indata, line); //skipping the first line of the csv file

    int i;
    int N = 0;
    int NLoop;
    int Loop = 0;
    string InvertValueBefore;
    double InvertValueAfter;
    char comma;

    while (indata.eof())    // before file reache the end
    {
        for (NLoop = 0; NLoop < 51; NLoop++) // struct array loop
        {
            {
                getline(indata, DataStruc[Loop].statename, ',');// getting statename
                for (i = 0; i <= 12; i++)       // each group of data, except last
                {
                    indata >> DataStruc[Loop].StatDataNumber[N] >> comma;// storing data in struct
                    N++;
                }
                getline(indata, InvertValueBefore);                     // store last value as string
                InvertValueAfter = stoi(InvertValueBefore);             // convert it into double
                InvertValueAfter = DataStruc[Loop].StatDataNumber[N];   // store it in array of struct

            }
            Loop++;
        }
    }

    ReadData();
    return 0;
}
void ReadData (ifstream& indata , statData DataStruc[] )
{
    int i;
    string input;
    bool stayinloop = true;

    cout << "Enter a statename or 'q' to quit\n";
    getline(cin, input);

    while (stayinloop == true)
    {
        if (input == "Alabama")
            DataStruc[i].statename == "Alabama";
            DataStruc[i].StatDataNumber[]

    }

}

此代码尚未完成。如果发现其他错误,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码很好。但是,某些要点:
1.您只需要摆脱某些不需要的变量。
2.“ eof”功能用于识别是否到达文件末尾。为此,您需要使用while(!indata.eof())。
3.“ ReadData”方法应出现在主函数之前,但是,如果要在主函数之后放置函数,则首先需要在主函数之前定义函数声明(即,在主函数之前,可以将“无效的ReadData(ifstream&indata,statData DataStruc []);“),之后,您可以定义函数。

以下是您要求的有效版本。

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;

struct statData 
{
    string statename;
    double StatDataNumber[3];

}DataStruc[2];

void ReadData (ifstream& indata , statData DataStruc[])
{
    string input;
    bool stayinloop = true;

    while (stayinloop)
    {
        cout << "\nEnter a statename or 'q' to quit\n";
        getline(cin, input);

        for (int i = 0 ; i < 2; i++)
        {
            if (input == DataStruc[i].statename)
            {
                for(int j = 0 ; j < 3; j++)
                {
                    cout << DataStruc[i].StatDataNumber[j] << ',';
                }
            }
            else if(input == "q")
            {
                stayinloop = false;
            }
        }     
    }
}

int main()
{
    ifstream indata;
    string tempData = "";
    string line;
    string InvertValueBefore = "";
    double InvertValueAfter = 0.0;
    char comma = ',';

    indata.open("test.csv");    //opening file
    if (indata.fail())  //fail safe
    {
        cout << "Fail to open file";
    }
    getline(indata, line); //skipping the first line of the csv file

    while (!indata.eof())    // before file reach the end
    {
        for (int NLoop = 0; NLoop < 2; NLoop++) // struct array loop
        {
            {
                getline(indata, DataStruc[NLoop].statename, comma);// getting statename
                for (int i = 0; i < 2; i++)       // each group of data, except last
                {
                    getline(indata, tempData, comma);
                    DataStruc[NLoop].StatDataNumber[i] = atof(tempData.c_str());
                }
                getline(indata, InvertValueBefore);                     // store last value as string
                InvertValueAfter = atof(InvertValueBefore.c_str());             // convert it into double
                DataStruc[NLoop].StatDataNumber[2] = InvertValueAfter; 
            }
        }
    }

    ReadData(indata, DataStruc);
    return 0;
}