Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- using namespace std;
- struct Towar
- {
- char nazwa[64];
- float cena;
- };
- Towar towar;
- Towar *tabTowarow = new struct Towar[10];
- void wypisz(const Towar& towar){
- cout << towar.nazwa << "\t" << towar.cena << endl;
- }
- void wypisz(struct Towar tab[], int ilosc){
- for(int i = 0; i < ilosc; i++){
- cout<<tab[i].nazwa<<" "<< tab[i].cena;
- }
- }
- void sum(struct Towar tab[], int ilosc){
- int sum = 0;
- for(int i = 0; i < ilosc; i++){
- sum+=tab[i].cena;
- }
- cout<<"Suma wynosi: "<<sum;
- }
- void add(struct Towar tab[], int ilosc){
- ilosc+=1;
- cout<<"Podaj nazwe produktu:"<<endl;
- cin>>tab[ilosc].nazwa;
- cout<<"Podaj cene:"<<endl;
- cin>>tab[ilosc].cena;
- wypisz(tabTowarow,ilosc);
- }
- void deleteRecord(struct Towar tab[],int ilosc){
- cout<<"wprowadz numer produktu";
- int num;
- cin>>num;
- for(int i = 0; i < ilosc; i++){
- if(i == num){
- for(int j=i; j<(ilosc-1); j++){
- tab[j]=tab[j+1];
- ilosc--;
- }
- }
- }
- }
- int wczytaj(const char* s){
- ifstream plik;
- plik.open("cos2.txt");
- if(plik.good() == false){
- cout<<endl;
- cout<<"blad pliku";
- (exit(0));
- }
- int lines = std::count(std::istreambuf_iterator<char>(plik),std::istreambuf_iterator<char>(), '\n');
- plik.seekg(plik.beg);
- for(int i=0;i<=lines;i++){
- plik>>tabTowarow[i].nazwa >> tabTowarow[i].cena;
- }
- return lines;
- }
- int main(){
- string s;
- s = "coscoscos.txt";
- int ilosc = wczytaj(s.c_str());
- char c;
- while(true){
- cout<<"1-oblicz sume"<<endl;
- cout<<"2-dodaj produkt do koszyka"<<endl;
- cout<<"3-usun z koszyka"<<endl;
- cout<<"4-EXIT"<<endl;
- cout<<endl<<endl;
- cin>>c;
- switch(c){
- case '1':
- sum(tabTowarow, ilosc);
- cout<<endl;
- break;
- case '2':
- add(tabTowarow, ilosc);
- cout<<endl;
- cout<<"KOSZYK:"<<endl;
- wypisz(tabTowarow,ilosc);
- break;
- case '3':
- deleteRecord(tabTowarow,ilosc);
- cout<<endl;
- cout<<"KOSZYK: "<<endl;
- wypisz(tabTowarow,ilosc);
- break;
- case '4':
- exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement