lundi 30 mars 2020

How to improve the syntax

The code I wrote appears to have a couple of syntax mistakes, yet code blocks doesnt show them. Instead of running the program it opens the ios_base.h and shows a few errors there.

C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\ios_base.h|789|error: 'std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\bits\basic_ios.h|66|error: within this context| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\streambuf|810|error: 'std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]' is private| C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\fstream|72|error: within this context|

#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
struct artikl()
{
    int sif;
    int kolic;
    char naz[20];
    float cena;
};
void unos()
{
    fstream myfile("file.bin", ios::out | ios::binary);
    artikl art_val;
    cout << "Unesite sifru: ";
    cin>>art_val.sif;
    cout << "Unesite kolicinu: ";
    cin>>art_val.kolic;
    cout << "Unesite naziv:  ";
    cin>>art_val.naz;
    cout << "Unesite cijenu:  ";
    cin>>art_val.cena;
    myfile.write((char *) &art_val, sizeof(artikl));

    myfile.close();
}
void ispis()
{
    fstream myfile("file.bin", ios::out | ios::binary);
    artikl art_val;
    myfile.read((char *) &art_val, sizeof(artikl));
    cout << "Naziv:  " << art_val.naz << "\n" << "Sifra: " << art_val.sif << "\n" << "Cijena: " << art_val.cena << "\n" << "Kolicina: "<< art_val.kolic << "\n";
}
int main()
{


    cout << "Izbornik: \n" << endl;

    while (true)
    {
        cout<< "1. Unos podataka: \n";
        cout<< "2. Ispis: \n";
        cout<< "9. Izlaz iz programa \n";
        int a;
        cin>>a;
        if (a == 1)
        {
            cout<<"Unesi podatke koje treba upisati u datoteku.\n";
            unos();
        }
        else if (a == 2)
        {
            cout<<"Podaci iz datoteke su: \n";
            ispis();
        }
        else if (a == 9)
        {
            exit(1);
        }
        else
            cout<<"Niste unijeli 1, 2 ili 9.";
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire