Help me with my project

Hello.
I have two files. One with ID, Name and Surname of employee and secund one is with their ID and number of hours in each day they work.

I need to sum these hours.
But I dont know how.

program is written in my native language.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdlib>


using namespace std;

int pocetRadku(ifstream &soubor)
{
    int pocetRadku = 0;
    string poradi;
    while (getline(soubor, poradi)){
        pocetRadku++;
    }
    soubor.clear(); //REWIND
    soubor.seekg(0, ios::beg);
    return pocetRadku;
}

typedef struct
{
    string ID;
    string Jmeno;
    string Prijmeni;

} Zamestnanci;

void NactiZamestnance(ifstream &souborZamestnancu, Zamestnanci zamestnanci[])
{
    string radek;
    int poradi=0;
    while (getline(souborZamestnancu, radek))
    {

        int pozice = 0;
        for( int i=0;i<radek.length();i++)
        {
           if(radek[i]==';')
           {
               pozice++;
               continue;
           }
           switch(pozice)
           {
                case 0: {zamestnanci[poradi].ID += radek[i]; break;}
                case 1: {zamestnanci[poradi].Jmeno += radek[i]; break;}
                case 2: {zamestnanci[poradi].Prijmeni += radek[i]; break;}

           }
        }
         poradi++;
    }
}


typedef struct
{
    string ID;
    string Pondeli_Hodiny;
    string Uteri_Hodiny;
    string Streda_Hodiny;
    string Ctvrtek_Hodiny;
    string Patek_Hodiny;
    string Sobota_Hodiny;
    string Nedele_Hodiny;
} Hodiny;

void NactiHodiny(ifstream &souborHodin, Hodiny hodiny[])
{
    string radek;
    int poradi=0;
    while (getline(souborHodin, radek))
    {


        int pozice = 0;
        for( int i=0;i<radek.length();i++)
        {
           if(radek[i]==';')
           {
               pozice++;
               continue;
           }
          

           switch(pozice)
           {
                case 0: {hodiny[poradi].ID += radek[i]; break;}
                case 1: {hodiny[poradi].Pondeli_Hodiny += radek[i]; break;}
                case 2: {hodiny[poradi].Uteri_Hodiny += radek[i]; break;}
                case 3: {hodiny[poradi].Streda_Hodiny += radek[i]; break;}
                case 4: {hodiny[poradi].Ctvrtek_Hodiny += radek[i]; break;}
                case 5: {hodiny[poradi].Patek_Hodiny += radek[i]; break;}
                case 6: {hodiny[poradi].Sobota_Hodiny += radek[i]; break;}
                case 7: {hodiny[poradi].Nedele_Hodiny += radek[i]; break;}
           }
        }

        poradi++;
    }
}

int main()
{

    /**
    *Otevreni vstupniho souboru obsahujici Jmena, Prijmeni a ID Zamestnancu
    */

    string name = "..\\vstupnidata\\Zamestnanci.csv";

     ifstream souborZamestnancu(name.c_str());
    if (souborZamestnancu == NULL)
    {
      cout << "Chyba pri otevreni souboru" << endl;
      return -1;
    }

    /**
    *Otevreni vstupniho souboru obsahujici ID Zamestnance a pocet hodin odpracovanych za jednotlive dny
    */

    string name2 = ("..\\vstupnidata\\Leden.csv");

     ifstream souborHodin(name2.c_str());
    if (souborHodin == NULL)
    {
      cout << "Chyba pri otevreni souboru" << endl;
      return -1;
    }


    int pocetZamestnancu = pocetRadku(souborZamestnancu);
    int pocetHodin = pocetRadku(souborHodin);

    Zamestnanci zamestnanci[pocetZamestnancu];
    Hodiny hodiny [pocetHodin];


    NactiZamestnance(souborZamestnancu, zamestnanci );
    NactiHodiny(souborHodin, hodiny);


    for (int i =0; i < 9; i++)\
    {
        cout << hodiny[i].Pondeli_Minuty << " " << hodiny[i].Pondeli_Hodiny << endl;
    }




    return 0;
}
Last edited on
I'm not going to try to read that many lines of code without code tags.

http://www.cplusplus.com/articles/jEywvCM9/

Showing a sample of the 2 files would be helpful.

Also since your asking for help on a English site, maybe adding a comment in English would also be helpful. I can use a translator but that takes up more of my time.

I have two files. One with ID, Name and Surname of employee and secund one is with their ID and number of hours in each day they work.

I need to sum these hours.


So after you open the second file, use a while loop to read the file.
add a if statement to see if the ID is the same as the last one you read, and if yes then add the hours to the total hoursforweek. if no print your data and clear hoursforweek before you add more to it. It's going to be helpful to print out each line and calculation you do until your sure it all works.
I have my data of hours in array like hours[4].Thusday = 7:15.

Is there any way to seperate them to two int ? like int = 7 and int = 15.

This is what I need.

So I can sum total hours.
You can use getline(...) with a seperator different from the new line character ('\n'):

http://www.cplusplus.com/reference/string/string/getline/?kw=getline

1
2
ifstream ifs;
getline(ifs, str, ':');



To convert a string to int you might use stoi or stringstream:

http://www.cplusplus.com/reference/string/stoi/

or

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/
or is there any way tu sum like 7:15 and 8:30 to 15:45 ? in array ?
You can declare a custom data-type with an overloaded + operator like:
1
2
3
4
5
6
7
struct Time
{
	int hours;
	int minutes;
	Time operator + (const Time& rhs) const;
	...
}

Google 'C++ Primer Plus (5th edition) by Stephen Prata', in Chapter 11 he has a Time class just like above with the overloaded + operator
or is there any way tu sum like 7:15 and 8:30 to 15:45 ?


"7:15" and "8:30" are strings.
First convert to type double
double a = 7 + 15/60.0;
double b = 8 + 30/60.0;

hence a = 7.25, b = 8.5
sum a+b = 15.75

To convert back to a string, you need to separate the integer and fractional parts, 15 and 0.75
minutes = 0.75 * 60.0 = 45
Then join it all back together as a string.
Sorry, but im realy stupid in programing.

I think abouth this.

this is part of my code

radek = line

hodiny = hours

minuty = minits

and pondeli to nedele is Mondat to Saturday


line[0] = I 1 2 3 4 5 6 7 8
line[1] = D ; ; ; ; ; ; ;



in line I have evrything from my file whre i have hours.
in this part of code i put part of line in each string.

I want to go in loop until it hit ; then hit :


when it hit : to put in Monday_Minits

is that some how possible ?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

typedef struct
{
    string ID;
    string Pondeli_Hodiny;
    string Pondeli_Minuty;
    string Uteri_Hodiny;
    string Uteri_Minuty;
    string Streda_Hodiny;
    string Streda_Minuty;
    string Ctvrtek_Hodiny;
    string Ctvrtek_Minuty;
    string Patek_Hodiny;
    string Patek_Minuty;
    string Sobota_Hodiny;
    string Sobota_Minuty;
    string Nedele_Hodiny;
    string Nedele_Minuty;

} Hodiny;

void NactiHodiny(ifstream &souborHodin, Hodiny hodiny[])
{
    string radek;
    int poradi=0;
    while (getline(souborHodin, radek))
    {


        int pozice = 0;
        for( int i=0;i<radek.length();i++)
        {

           if(radek[i]==';')
                {
                pozice++;
               continue;
                }


           switch (pozice)
                {
                case 0: {hodiny[poradi].ID += radek[i]; break;}
                case 1: {hodiny[poradi].Pondeli_Hodiny += radek[i]; break;}
                case 2: {hodiny[poradi].Uteri_Hodiny += radek[i]; break;}
                case 3: {hodiny[poradi].Streda_Hodiny += radek[i]; break;}
                case 4: {hodiny[poradi].Ctvrtek_Hodiny += radek[i]; break;}
                case 5: {hodiny[poradi].Patek_Hodiny += radek[i]; break;}
                case 6: {hodiny[poradi].Streda_Hodiny += radek[i]; break;}
                case 7: {hodiny[poradi].Nedele_Hodiny += radek[i]; break;}
                }
                if (radek[i] ==':')
            {
                
                switch (pozice++)
                {
                case 0: {hodiny[poradi].Pondeli_Minuty += radek[i]; break;}
                case 1: {hodiny[poradi].Uteri_Minuty += radek[i]; break;}
                case 2: {hodiny[poradi].Streda_Minuty += radek[i]; break;}
                case 3: {hodiny[poradi].Ctvrtek_Minuty += radek[i]; break;}
                case 4: {hodiny[poradi].Patek_Minuty += radek[i]; break;}
                case 5: {hodiny[poradi].Sobota_Minuty += radek[i]; break;}
                case 6: {hodiny[poradi].Nedele_Minuty += radek[i]; break;}
                }

            }
        }

        poradi++;
    }
}
Topic archived. No new replies allowed.