Dynamic allocation and saving

Hello everyone. First post, probably not the last.
In the last few months I didnt have to register because I found solutions to my problems already posted on these forums, but I cant find an answer to this.

Ok, so short version. My assignment for the week is to input a few values into a .dat file (done), be able to write them out (done), then sort them in an allocated array by sifra_robe and then again write them down sorted into another .dat file (this is where i'm stuck).The actual writing down part from an array shouldnt be a problem, but i'm still green with c++ and i'm a bit confused now how to allocate the list and input the data from .dat into it.

Now i'm sorry, this is kinda big, so i'll have to put it in the next post, but i havent noticed brackets in the formating area here. The actual problems dont start before case 3...

And i'm well aware that case 1 is clustered, its part of the assignment, sorry.

So in short, for those that dont have the time to actually go through the code (and since its in a foreign language I cant really blame you) I just need assistance on how to get some data from a .dat file into an array and then back into a .dat file. But most importantly the first part.



Thanks in front for reading this and for any future assistance :)
Last edited on
Turns out I cant post more then 8k characters here so I deleted case2, its just writing out anyway :)

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<iostream>
#include<fstream>
#include "biblioteka_vrijeme.cc"

using namespace std;

struct roba {
       int sifra_robe;
       char naziv_robe[100];
       float cijena_robe;
       float kolicina_robe;
       float vrijeme_unosa;
};

fstream dat;
roba roba;

int ispis (int h) {
    char fiz_ime[100];
    float p1 = 0;
    float p2 = 0;
    float p3 = 0;
     if (h < 3) {
        cout << endl << "Trenutni broj zapisa: " << h << endl;
        return -1;
     }
     else {
          cout << endl << "Trenutni broj zapisa: " << h << endl << endl;
          cout << "Upisi fizicko ime datoteke koju zelis ispisati? ";
          cin >> fiz_ime;
          dat.open(fiz_ime, ios::in | ios::binary);
          while (!dat) {
             dat.close();
             dat.clear();
             cout << endl << "Datoteka koju ste upisali ne postoji!" << endl;
             cout << "Upisi ponovo fizicko ime datoteke koju zelis ispisati? ";
             cin >> fiz_ime;
             dat.open(fiz_ime, ios::in | ios::binary);
          };
          while (1){
                dat.read((char *) &roba,sizeof(roba));
                if (dat.eof()) break;
                cout << endl << "Sifra robe: " << roba.sifra_robe << endl;
                cout << "Naziv robe: " << roba.naziv_robe << endl;
                cout << "Cijena robe: " << roba.cijena_robe << endl;
                cout << "Kolicina robe: " << roba.kolicina_robe << endl;
                cout << "Vrijeme unosa u stotinkama sek: " << roba.vrijeme_unosa << endl;
                cout << "--------------------------------------" << endl;
                cout << "Vrijednost robe (" << roba.naziv_robe << "): " << roba.cijena_robe * roba.kolicina_robe << endl;
                cout << "======================================" << endl;
                p1 = p1 + roba.cijena_robe;
                p2 = p2 + roba.kolicina_robe;
                p3 = p3 + roba.cijena_robe * roba.kolicina_robe;
          };              
          cout << endl << "Prosjecna cijena robe: " << p1/h << endl;
          cout << "Prosjecna kolicina robe: " << p2/h << endl;
          cout << "Prosjecna vrijednost robe: " << p3/h << endl;
          cout << "//////////////////////////////////////" << endl;
          dat.close();
          dat.clear();                 
     };
};

int sort (int h) {
    cout << endl << "Trenutni broj zapisa: " << h << endl;
};

int main() {
    int izbor,g;
    int x;

    cout << "Dobrodosli !" << endl;
    cout << "============" << endl;
    
    do {
        
        cout << endl << "1. Unos podataka o robi u datoteku" << endl;
        cout << "2. Ispis sadrzaja datoteke roba" << endl;
        cout << "3. Sortiranje datoteke uz pomoc polja" << endl;
        cout << "4. Modifikacija." << endl;
        cout << "9. Izlaz iz programa" << endl << endl;
        cin >> izbor;
    
        switch (izbor) {
               case 1:
                    g = 0;
                    dat.open("roba_Sagud_Dario.dat", ios::in | ios::binary);
                    if (!dat) {     
                       dat.open("roba_Sagud_Dario.dat", ios::out | ios::binary);
                       dat.close();
                       dat.clear();
                    }
                    else {
                       cout << endl << "Datoteka je vec kreirana na disku!" << endl;
                       while (1) {
                             dat.read((char *) &roba,sizeof(roba));
                             if (dat.eof()) break;
                             g = g + 1;
                       };
                       cout << "Trenutni broj zapisa: " << g << endl;
                       dat.close();
                       dat.clear();  
                    };
                    dat.close();
                    dat.clear();
                    dat.open("roba_Sagud_Dario.dat", ios::in | ios::out | ios::ate | ios::binary);
                    vrijeme_pocetak();
                    cout << endl << "Upisi sifru robe: ";
                    cin >> roba.sifra_robe;
                    while ((roba.sifra_robe < 100) || (roba.sifra_robe > 999)) {
                          cout << "Sifra ne smije biti izvan 100-999!" << endl;
                          cout << "Upisi ponovo sifru robe: ";
                          cin >> roba.sifra_robe; 
                    };
                    cout << "Upisi naziv robe: ";
                    unos(roba.naziv_robe);
                    x = strlen(roba.naziv_robe);
                    while (x > 50) {
                            cout <<"Naziv robe ne smije biti duzi od 50 znakova!"<<endl;
                            cout << "Upisi ponovo naziv robe: ";
                            unos(roba.naziv_robe);
                            x = strlen(roba.naziv_robe);
                    };
                    cout << "Upisi cijenu robe: ";
                    cin >> roba.cijena_robe;        
                    cout << "Upisi kolicinu robe: ";
                    cin >> roba.kolicina_robe;
                    while (roba.cijena_robe * roba.kolicina_robe < 0 || roba.cijena_robe * roba.kolicina_robe > 39532) {
                          cout << "Umnozak cijene i kolicine robe ne moze biti manji od 0 ili veci od 39532" << endl;
                          cout << "Upisi ponovo cijenu robe: ";
                          cin >> roba.cijena_robe;
                          cout << "Upisi ponovo kolicinu robe: ";
                          cin >> roba.kolicina_robe;
                    };
                    vrijeme_kraj();
                    cout << "Vrijeme unosa u stotinkama sek: " << vrijeme_proteklo()/10 << endl;
                    roba.vrijeme_unosa = vrijeme_proteklo()/10;
                    dat.write((char*) &roba,sizeof(roba));
                    dat.close();
                    dat.clear();
                    
                    dat.open("roba_Sagud_Dario.dat", ios::in | ios::binary);
                    while (1){
                          dat.read((char *) &roba,sizeof(roba));
                          if (dat.eof()) break;
                          cout << endl << "Sifra robe: " << roba.sifra_robe << endl;
                          cout << "Naziv robe: " << roba.naziv_robe << endl;
                          cout << "Cijena robe: " << roba.cijena_robe << endl;
                          cout << "Kolicina robe: " << roba.kolicina_robe << endl;
                          cout << "Vrijeme unosa u stotinkama sek: " << roba.vrijeme_unosa << endl;
                          cout << "--------------------------------------" << endl;
                    };
                    dat.close();
                    dat.clear();                    
                    break;
               
               case 3:
                    g = 0;
                    dat.open("roba_Sagud_Dario.dat", ios::in | ios::binary);
                    if (!dat) {
                       cout << endl << "Trenutni broj zapisa: " << g << endl;
                       break;   
                    };
                    dat.close();
                    dat.clear();
                    dat.open("roba_Sagud_Dario.dat", ios::in | ios::binary);
                    while (1) {
                          dat.read((char *) &roba,sizeof(roba));
                          if (dat.eof()) break;
                          g = g + 1;
                    };
                    dat.close();
                    dat.clear();
                    sort (g);
                    break;
               case 4:
                    break;
               case 9:
                    break;
               default:
                   cout << endl << "Krivi unos! Probaj ponovo." << endl << endl;
        };
    
    } while (izbor!=9);
    cout << endl << "Kraj programa." << endl;
    system ("pause");
};
Last edited on
Topic archived. No new replies allowed.