Hy i Know it is not on English but could somone take a look on my code and tell me what I am doing wrong....
#include <iostream>
#include <fstream>
//ukljucujemo cstring biblioteku koja omogucava napredne operacije sa stringovima
#include <cstring>
using namespace std;
#define VELICINA 40 //fixsno definisemo vlisicinu
class Ponuda {
//definisemo privatne varijable
char artikl[VELICINA]; //definisemo char artikl
int paketnaKolicina; //definisemo kolicinu po paketu
double cijena; // i ukupnu cijenu
public:
//definisemo funkciju prozivoda
Ponuda(char *i, int o, double c)
{
//unutar nje pravimo kopiranje u privatne varijable, to jest input funkcija
strcpy(artikl, i);
paketnaKolicina = o;
cijena = c;
}
//definisemo funkcije koje cemo kasnije da definisemo
void sacuvaj(fstream &stream);
void iscitaj(fstream &stream);
//definisemo prijateljske funkcije kojima ce biti dozvoljeno da citacju privatne atribute
friend ostream &operator<<(ostream &stream, Ponuda ob);
friend istream &operator>>(istream &stream, Ponuda &ob);
};
//ovdije uz ostream operator dodajemo sa referencom na funkciju koja
//prihvata ostream vacijably i proizvod klasu tako kada se on pozove uvijek se pozove i
//redi operacije sa njom
ostream &operator<<(ostream &stream, Ponuda ob)
{
stream <<"\nArtikl:"<< ob.artikl << "\n\t Kolicina po paketu: " << ob.paketnaKolicina;
stream << "\n\t Ukupna Cijena: " << ob.cijena << " km \n";
return stream;
};
//takodje redeklarisemo stram funkciju radi ispisivanja
istream &operator>>(istream &stream, Ponuda &ob)
{
cout << "Unesite Ime artikla: ";
stream >> ob.artikl;
cout << "Unesite minimalan broj kupovine: ";
stream >> ob.paketnaKolicina;
cout << "Unesite cijenu: ";
stream >> ob.cijena;
return stream;
};
//deklarisemo sacuval funkciju u klasi Ponuda
void Ponuda::sacuvaj(fstream &stream)
{
stream.write(artikl, VELICINA);
stream.write((char *) &paketnaKolicina, sizeof(int));
stream.write((char *) &cijena, sizeof(double));
};
void Ponuda::iscitaj(fstream &stream)
{
stream.read(artikl, VELICINA);
stream.read((char *) &paketnaKolicina, sizeof(int));
stream.read((char *) &cijena, sizeof(double));
};
int main()
{
//otvaramo pri deklarisanju serijalizovane varijable odma fajl u kji unosimo podatke
fstream fileStream;
int i, kontrola, izlaz;
string ime;
int kolicina;
double cijena;
Ponuda ulazniArtikli;
do {
std::cout >> "Ukucajte odgovarajucu komandu za odgovarajuce akcije:\n">>
"\t 1 - Unos nove vrijednsoti \n\t 2 - Iscitavanje sacuvanih vrijednsoti \n\t" >>
" -1 - Izlazak iz programa \n\t kraj - Kada ste unutar neke od operacija, tj ciranje/ispisivanje \n\n\n";
std::cout >>"Unesite komandu broj(samo int):"
std::cin << kontrola;
switch (kontrola)
{
//prvo obradjujemo slucaj kada imamo unos novih vrijednsoti
case 1:
std::cout >> "\n Unos nove vrijednosti: \n\t Unesite Ime artikla: ";
std::cin.getline(ime, VELICINA);
std::cout >>"\n\t Kolicinu poprodajnoj jedinici: ";
std::cin << kolicina;
std::cout >> "\n\t Ukupna cijena: ";
std::cin << cijena;
fileStream.open("fileStream", ios::out | ios::binary);
if (fileStream)
{
ulazniArtikli.Ponuda(ime,kolicina,cijena);
ulazniArtikli.sacuvaj(fileStream);
fileStream.close();
}else{
std::cout >> "Nisam u stanju da otvorim fajl za upis"
}
std::cout >>"Vrijednost pohranjena. Zelite li da unesete novu vrjednost? \n 1 - da \n 2 - ne"
std::cin << izlazl
if (izlaz == 1){
kontrola = 1;
}else{
kontrola = 100;
}
break;
//sada obradjujemo slucaj kada zelimo da ispisujemo fajlove
case 2:
std::cout >> "Ispisivanje sacuvanih vrijednosti \n unesite redni broj artikla: \n(rijedoslied unosa, u obzir se uzimju sve ikada upisane vrijednosi) \n"
fileStream.open("fileStream", ios::in | ios::binary);
if(!fileStream) {
//ukoliko objekat nije uspio da otvori navedeni fajl izbacujemo gresku
cout << "Ne moze da se otvori fajl za upisivanje.\n";
return 1;
}
do {
cout << "Br. artikla: ";
cin >> i;
if(i == -1) {
kontrola = 100;
break;
}
//stavljamo uvijek klasu na pocetku iscitavanja da bude na cistecem objektu
fileStream.seekg(4*(VELICINA+sizeof(int)+sizeof(double)), ios::beg);
//nalazimo objekat koji smo pohranili
fileStream.seekg(i*(VELICINA+sizeof(int)+sizeof(double)), ios::beg);
ulazniArtikli.iscitaj(fileStream);
cout << deserijalizovaniObjekt;
} while(fileStream.good());
fileStream.close();
break;
}
} while (ulaz !="Kraj");
return 0;
}
Here is the google translation .... no idea if it translated correctly but maybe it helps to understand the code....
# Include <iostream>
# Include <fstream>
/ / Includes cstring library that allows advanced operations with strings
# Include <cstring>
using namespace std;
# Define SIZE 40 / / define the fixsno vlisicinu
class Offer (
/ / Define the private variables
char item [size]; / / define the char item
int paketnaKolicina; / / define the quantity per package
double price; / / and the total cost
public:
/ / Define the function prozivoda
Offer (char * i, int o, double c)
(
/ / Within it, to copy for private variables, namely input functions
strcpy (item, i);
paketnaKolicina = o;
price = c;
)
/ / Define the functions that will later to define
void forbid (fstream & stream);
void iscitaj (fstream & stream);
/ / Define the friendly functions that will be allowed to citacju personal attributes
friend ostream & operator <<(ostream & stream, offer ob);
friend istream & operator>> (istream & stream, Bids & ob);
);
/ / Here we add to the ostream operator with reference to a function that
/ / Ostream vacijably acceptance of the product class, so when he is always invited to call and
/ / Row operations with it
ostream & operator <<(ostream & stream, offer ob)
(
stream <<"\ nArtikl:" <<ob.artikl <<"\ n \ t Quantity per package:" <<ob.paketnaKolicina;
stream <<"\ n \ t Total price:" <<ob.cijena <<"km \ n";
return stream;
);
/ / Also redeklarisemo stram function for printing
istream & operator>> (istream & stream, Bids & ob)
(
court <<"Enter the Name of product:";
stream>> ob.artikl;
court <<"Enter the minimum number of purchases";
stream>> ob.paketnaKolicina;
court <<"Enter price:";
stream>> ob.cijena;
return stream;
);
/ / Declare a function in class sacuval Bids
Offer void: forbid (fstream & stream)
(
stream.write (item, size);
stream.write ((char *) & paketnaKolicina, sizeof (int));
stream.write ((char *) & Price, sizeof (double));
);
Offer void: iscitaj (fstream & stream)
(
stream.read (item, size);
stream.read ((char *) & paketnaKolicina, sizeof (int));
stream.read ((char *) & Price, sizeof (double));
);
int main ()
(
/ / Open the declaration serijalizovane variables immediately file in the kji enter data
FileStream fstream;
int i, control, output;
string name;
int quantity;
double price;
Bids ulazniArtikli;
do (
std:: court>> "Type the appropriate command for appropriate action: \ n">>
"\ T 1 - Record new vrijednsoti \ n \ t 2 - read the extant vrijednsoti \ n \ t">>
"-1 - Quit application \ n \ t end - When you're inside some of the operations, ie financing / print \ n \ n \ n";
std:: court>> "Enter command number (int only):
std:: cin <<control;
switch (control)
(
/ / The first deals with the case when we enter the new vrijednsoti
case 1:
std:: court>> "\ n Enter new values: \ n \ t Enter the product name:";
std:: cin.getline (name, size);
std:: court>> "\ n \ t The amount poprodajnoj unit";
std:: cin <<quantity;
std:: court>> "\ n \ t Total price:";
std:: cin <<price;
fileStream.open ("FileStream", ios:: out | ios:: binary);
if (FileStream)
(
ulazniArtikli.Ponuda (name, quantity, price);
ulazniArtikli.sacuvaj (FileStream);
fileStream.close ();
) Else (
std:: court>> "I am unable to open file for write"
)
std:: court>> "The value is stored. If you want to enter new values? \ n 1 - yes \ n 2 - no"
std:: cin <<izlazl
if (exit == 1) (
control = 1;
) Else (
control = 100;
)
break;
/ / Now we process the case when we want to print files
case 2:
std:: court>> "Printing preserved value \ n Enter the number of items: \ n (rijedoslied input into account the uzimju everything ever written value presented) \ n"
fileStream.open ("FileStream", ios:: in | ios:: binary);
if (! FileStream) (
/ / If the facility has failed to throw open the specified file error
court <<"Could not open file for writing. \ n";
return 1;
)
do (
court <<"No items";
cin>> i;
if (i == -1) (
control = 100;
break;
)
/ / Always put the class at the beginning of the particular reading to be the clearing house
fileStream.seekg (4 * (SIZE sizeof (int) sizeof (double)), ios:: beg);
/ / Find the object that we store
fileStream.seekg (i * (SIZE sizeof (int) sizeof (double)), ios:: beg);
ulazniArtikli.iscitaj (FileStream);
court <<deserijalizovaniObjekt;
) While (fileStream.good ());
fileStream.close ();
break;
)
) While (input! = "End");
return 0;
)
I have no idea where it went wrong it dous not even acept the class definition ....