I am getting a few errors on my code. I know my INT and CHAR are not correct. Just not sure where. I am trying to pull data from a file. The data is a day ( and integer 01, 02, 03) a time of day HH:MM, and a level 00. I need to sort the data by the max level and I can not figure that part out. Then I need to have a Pollution class use an Analysis class that has function members maximum() and average().

My error is this on several lines: 1>c:\users\rowley\documents\visual studio 2008\projects\example\example\example.cpp(69) : error C2664: 'strncpy' : cannot convert parameter 1 from 'int [3]' to 'char *'

Code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
 
using namespace std;

struct Pollution
{
public:
        
        Pollution(); 

        Pollution(char * record); 
        Pollution(int * p_day, int * p_time, int * p_level); 
         
        const static int Field_Seperator = ':'; 
        const static int Pollution_day_MaxSize = 3; 
	const static int Pollution_time_MaxSize = 6;
	const static int Pollution_level_MaxSize = 3; 
        
        const int * get_pollution_day(); 
        const int * get_pollution_time(); 
        const int * get_pollution_level();
        const int * get_pollution_indexer(int * day, int * time, int * level);
        
        void print(); 
        void set_pollution_day(int * p_day); 
        void set_pollution_time(int * p_time);
        void set_pollution_level(int *p_level);
        void set_pollution_day(int * p_day, int p_day_sz);
       void set_pollution_time(int * p_time, int p_time_sz);
		
private:

        int pollution_day[Pollution_day_MaxSize]; 
        int pollution_time[Pollution_time_MaxSize]; 
	int pollution_level[Pollution_time_MaxSize];
};

void Pollution::set_pollution_day(int * p_day, int p_day_sz)
{
        if ( p_day_sz >= Pollution_day_MaxSize) 
                p_day_sz = Pollution_day_MaxSize-1;

        int * sp_day = p_day;
        //trim end
        while( isspace(sp_day[p_day_sz-1])) p_day_sz--;
        //trim begining
        while( isspace(* sp_day))
        {
                sp_day ++;
                p_day_sz--;

        }
        strncpy( pollution_day, sp_day, p_day_sz);
        pollution_day[p_day_sz] = NULL;

		return;

};

void Pollution::set_pollution_day(int * p_day)
{
        set_pollution_day(p_day, strlen(p_day));
}

void Pollution::set_pollution_time(int * p_time, int p_time_sz)
{
        if ( p_time_sz >= Pollution_time_MaxSize ) 
                p_time_sz = Pollution_time_MaxSize-1;

       int * sp_time = p_time;
        //trim end
        while( isspace(sp_time[p_time_sz-1])) p_time_sz--;
        //trim begining
        while( isspace(* sp_time))
        {
                sp_time ++;
                p_time_sz--;
        }
        strncpy( pollution_time, sp_time, p_time_sz);
        pollution_time[p_time_sz] = NULL;

        return;
}

void Pollution::set_pollution_time(int * p_time)
{
        set_pollution_time(p_time, strlen(p_time));
}

 void Pollution::set_pollution_level(int * p_level, int p_level_sz)
{
        if ( p_level_sz >= Pollution_level_MaxSize) 
                p_level_sz = Pollution_level_MaxSize-1;

        int * sp_level = p_level;
        //trim end
        while( isspace(sp_level[p_level_sz-1])) p_level_sz--;
        //trim begining
        while( isspace(* sp_level))
        {
                sp_level ++;
                p_level_sz--;

        }
        strncpy( pollution_level, sp_level, p_level_sz);
        pollution_level[p_level_sz] = NULL;

		return;

};

void Pollution::set_pollution_level(int * p_level)
{
        set_pollution_level(p_level, strlen(p_level));
}

Pollution::Pollution()
{
        pollution_day[0] = pollution_time[0] = pollution_level[0] = NULL;
}

Pollution::Pollution(int *p_day, int *p_time, int *p_level)
{
        set_pollution_day(p_day);
        set_pollution_time(p_time);
	set_pollution_level(p_level);
}

Pollution::Pollution(char *record)
{
        int sept_match = 0;
        char rec_len = strlen(record);

        for ( ;sept_match<rec_len;sept_match++)

			if ( record[sept_match] == Field_Seperator)

                        break;

        if ( sept_match < rec_len )
        {
                char * p_day = record;
                char * p_time = record + sept_match + 1;
		char * p_level = record + sept_match + 1;

                set_pollution_day(p_day, sept_match);
                set_pollution_time(p_time);
		set_pollution_level(p_level);
        } 
	else
                 Pollution();
}

inline const int * Pollution::get_pollution_day()
{
        return pollution_day;
}
inline const int * Pollution::get_pollution_time()
{
        return pollution_time;
}

 inline const int * pollution::get_pollution_indexer(int * key)
{
	return strcmp(key, "day")==0 ? pollution_day : pollution_time : pollution_level;
}


void Pollution::print()

{
	std::cout << get_pollution_day() << " : " << get_pollution_time() <<  " : " << get_pollution_level() << std::endl;
}

int main()
{
        const int Max_File_NM = 128;
        const int Max_Rec_Len = 256;
        const int Max_Pollution = 256;

        char in_rec_buff[Max_Rec_Len]; 
        Pollution * pollution_list[Max_Pollution]; 
	int no_pollution = 0;

		string Pollution;
		ifstream infile;
		infile.open("pollution_level.txt");
	
		while (!infile.eof)
		{
			getline(infile, Pollution);
			cout << Pollution;
		}
		infile.close();
		system ("pause");
}
       
        cout << " Air pollution level of a city :"
                    << "\n\tReading the pollution levels from a file and\n"
                      << "\tsorting it based on maximum levels.\n";

 

        std::cout << "\n-------------------------------------------\n";

        {

                infile.getline(in_rec_buff, Max_Rec_Len);
                pollution_list[no_pollution++] = new Pollution(in_rec_buff);

        }
        for ( int i=0; i<no_pollution; i++)
               pollution_list[i]->print();

        int sort_field;

        while ( true )
        {

                
                std::cout << "\n-------------------------------------------\n";
                //**need code to sort by MAXIMUM here** 
              
                if ( sort_field == 0 )

                        break;

                // save the given sort index in friendly name

		char * s_index = sort_field == 1 ? "day" : "time" : "level";

               // use bubble sort algorithm from :
            
                for(int x=0; x<no_pollution; x++)
                {
                      for(int y=0; y<no_pollution-1; y++)

                        {
                        if(strcmp(pollution_list[y]->get_pollution_indexer(s_index), 
                            pollution_list[y+1]->get_pollution_indexer(s_index)) > 0)
                                {
                                     Pollution * temp = pollution_list[y+1];
                                     pollution_list[y+1] = pollution_list[y];
                                     pollution_list[y] = temp;
                                }
                        }
                }

 
                std::cout << "\n-------------------------------------------\n";
                std::cout << "Sorting by " << s_index << std::endl;
                std::cout << "-------------------------------------------\n";
                // print the sorted records

                for ( int i=0; i<no_pollution; i++)
                        pollution_list[i]->print();
        }

        system("pause");

        return 0;

}

Now I need to have a Pollution class use an Analysis class that has function members maximum() and average().

Pollution record[20];

class Pollution
{
	private:
		int hour, co2, h2s, suspended_particles;
	public:
		void read_data();
		void print_data();
};

void Pollution::read_data()
{
	ifstream infile("pollution_level.txt");	
	infile >> co2 >> h2s >> suspended_particles;
}

Do I just add this to the bottom of my code?

Here is an example of how to use strncpy:

http://www.cplusplus.com/reference/clibrary/cstring/strncpy/

I strongly suggest that you try little < 10 line demonstration programs, ensure you know how everything works, and THEN write a 300 line program. It will work out much better for you, I am sure. It will also help us help you because then we will have a manageable piece of code to help you with.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.