CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Apr 2017
    Posts
    1

    Help with Coding (Newbie)

    Hi Guys,

    Need help with this code. Using codeblocks and getting error that the template is not being recognised.

    Code is as follows:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <winddata.h>
    
    void menu(vector<WindData>& windlog);
    void readFile(ifstream& inputFile, vector<WindData>& windlog);
    void averageWind(int monthNum, vector<WindData>& windlog);
    
    int main()
    {
        vector<WindData> windlog;
        ifstream inputFile("MetData-31-3.csv");
    
        if(!inputFile) return -1;
        readFile(inputFile, windlog);
        menu(windlog);
    
        return 0;
    }
    
    void readFile(std::ifstream& inputFile, std::vector<WindData>& windlog)
    {
        float tempSpeed;
        int timeIndex, windSpeedIndex, radiationIndex;
        int columns = 18, field = 0;
        std::string tempDate, tempTime;
        std::string headers[20];
        std::string nStream;
    
        for(int i = 0; i < columns -1; i++)
        {
            getline(inputFile, headers[i], ',');
            if(headers[i] == "WAST")
                timeIndex = i;
    
            if(headers[i] == "S")
                windSpeedIndex = i;
    
            if(headers[i] == "SR")
                radiationIndex = i;
        }
    
        getline(inputFile, headers[17]);
    
        if(inputFile)
        {
            std::string token;
            std::stringstream iss, dateStream;
            while(getline(inputFile, nStream))
            {
                iss << nStream;
                while(getline(iss, token, ','))
                {
                    if(field == timeIndex)
                    {
                        dateStream << token;
                        getline(dateStream, tempDate, ' ');
                        getline(dateStream, tempTime);
                    }
    
                    if(field == windSpeedIndex)
                    {
                        tempSpeed = std::atof(token.c_str());
                    }
    
                    field++;
                }
                field = 0;
                iss.clear();
                dateStream.clear();
    
                WindData windSpeed(tempDate, tempTime, tempSpeed);
                windlog.push_back(windSpeed);
                windSpeed.print();
            }
            std::cout << "Vector Size: " << windlog.size() << std::endl;
        }
    }
    
    void averageWind(int yearNum, std::vector<WindData>& windlog)
    {
        std::string month[12] = {"January", "February", "March", "April", "May", "June",
                                "July", "August" "September", "October", "November", "December"};
    
        int monthCount = 0, monthNum[12] = {0}, monthAverage[12] = {0};
        int dayCount[12] = {0}, totalWindSpeed[12] = {0}, totalRadiation[12] = {0}, mWindAverage[12] = {0};
    
        for (WindData& windData : windlog) {
            if (windData.getYear() == yearNum) {
                int i = windData.getMonth() - 1;
                totalWindSpeed[i] += windData.getSpeed();
                dayCount[i]++;
            }
        }
    
        std::cout << "Wind Speed: " << totalWindSpeed[i] << std::endl;
        std::cout << "Day Count: " << dayCount[i] << std::endl;
    
        for (int i = 0; i < 12; i++) {
            mWindAverate[i] = totalWindSpeed[i] / dayCount[i];
        }
    
        std::cout << mWindAverage[i];
        std::cout << month[i];
    }
    
    void menu(std::vector<WindData>& windlog)
    {
        int option = 0;
    
        while(option != 5)
        {
            std::cout << "Menu:" << std::endl;
            std::cout << "1: \tPrint the maximum wind speed for a specified month & year" << std::endl;
            std::cout << "2: \tPrint the average wind speed for each month of a specified year" << std::endl;
            std::cout << "3: \tPrint the total solar radiation for each month of a specified year" << std::endl;
            std::cout << "4: \tAverage wind speed & solar radiation for each month of a specified year\n\tSaved to file." << std::endl;
            std::cout << "5: \tExit the program.\n\n" << std::endl;
            std::cin >> option;
            switch(option)
            {
                case 1:
                    break;
    
                case 2:
                    int year;
                    std::cout << "Please enter a year" << std::endl;
                    std::cin >> year;
                    averageWind(year,windlog);
                    break;
    
                case 3:
                    break;
    
                case 4:
                    break;
    
                case 5:
                    std::cout << "Exiting..." << std::endl;
                    break;
            }
        }
    }
    winddata.h:

    Code:
    #ifndef WINDDATA_H
    #define WINDDATA_H
    
    using namespace std;
    
    template <class WindData>
    class vector{
    public:
    
        /**default constructor*/
        vector(int size = 100000);
        vector(Vector<WindData>& otherList);
        ~vector();
    
        /**data members*/
    private:
        WindData *list ;
        int length;
        int maxSize;
    };
    
    /** implementation of template class*/
    
    template<class WindData>
    vector<WindData>::vector(int size)
    {
        maxSize = listSize;
        length = 0;
        list = new WindData[maxSize];
    }
    
    template<class WindData>
    vector<WindData>::~vector()
    {
        delete[] list;
    }
    
    /**end of class*/
    #endif
    Last edited by 2kaud; April 23rd, 2017 at 09:45 AM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured