0

My program is working as I want it to work (It is on Bosnian language so the cout of the program is not that important). The program is about reading some text from file and then couting what I want to The only problem I have with this program is that for some reason it is showing the two 0 0 at the end of the text file even though I do not have it in my text file

Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;


struct proizvod
{
    char naziv[100];
    char proizvodac[100];
    int cijena = 0;
    int kolicina = 0;

};

bool poredjenje(proizvod a, proizvod b) 
{ 
    if (a.cijena != b.cijena ) 
        return a.cijena > b.cijena; 

} 


void sortiranje(proizvod a[], int n) 
{  
    sort(a, a+n, poredjenje); 

} 
int main()
{
    ifstream datoteka;
    datoteka.open("proizvodi.txt.txt");
    int brojStvari = 0;
    int sumaProizvoda = 0;
    int ukupnaVrijednost = 0;
    char* spisakProizvoda[100];
    int brojFIAT = 0;
    int spisakCijena = 0;

    proizvod automobili[100];

    if (datoteka.fail())
    {
        cout << "Ne postojeca datoteka";
        exit(1);
    }

    while (datoteka.good() && !datoteka.eof())
    {
        datoteka >> automobili[brojStvari].naziv >> automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >> automobili[brojStvari].kolicina;
        ++brojStvari;
        ++sumaProizvoda;
    }

    for (int i = 0; i < brojStvari; i++)
    {
        cout << automobili[i].naziv << " " << automobili[i].proizvodac << " " << automobili[i].cijena << " " << automobili[i].kolicina << endl;
    }

    for (int i = 0; i < brojStvari; i++)
    {
        ukupnaVrijednost += automobili[i].cijena;

        if (automobili[i].kolicina == 0)
        {
            spisakProizvoda[i] = automobili[i].proizvodac;
        }
        else if (automobili[i].proizvodac == "FIAT")
        {
            brojFIAT++;
        }
    }

    char pomocna[100];


    cout << endl;
    cout << "Ukupan broj proizvoda u datoteci: " << sumaProizvoda << endl;
    cout << "Ukupan vrijednost proizvoda u datoteci: " << ukupnaVrijednost << endl;
    cout << "Spisak automobila sa cijenom 0 su: ";
    for (int i = 0; i < brojStvari; i++)
    {
        if (!spisakProizvoda[i])
        {
            cout << "Ne postoje ti proizvodi " << endl;
            break;
        }
        else
        cout << spisakProizvoda[i] << endl;
    }
    cout << "Broj prozivoda koji proizvodi FIAT: " << brojFIAT << endl;
    cout << "Sortirani proizvodi prema cijeni: " << endl;

    sortiranje(automobili, brojStvari);

    for (int i = 0; i < brojStvari; i++)
    {
        cout << automobili[i].proizvodac << endl;
    }
    return 0;
}

And here is the cout

Golf Volskwagen 5000 5
AudiRS5 Audi 50000 3
  0 0

Ukupan broj proizvoda u datoteci: 3
Ukupan vrijednost proizvoda u datoteci: 55000
Spisak automobila sa cijenom 0 su: Ne postoje ti proizvodi
Broj prozivoda koji proizvodi FIAT: 0
Sortirani proizvodi prema cijeni:
Audi
Volskwagen

Can anybody tell me what is the problem ? P.S : Sorry if you do not understand the program itself I apologize sincerely

Samke11
  • 11
  • 2
  • 2
    Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – churill May 06 '20 at 17:30

1 Answers1

0

You are seeing the extra 0 0 at end most likely due to an extra empty line at end of proizvodi.txt.txt file. This happens because the new line is also accepted as input but since there are no entries, the entries in struct proizvod retain default 0 values for cijena and kolicina which gets printed out as 0 0.

Update your main() code to reading file like follow and it will work as expected:

  while (datoteka >> automobili[brojStvari].naziv >>
         automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >>
         automobili[brojStvari].kolicina) {
    ++brojStvari;
    ++sumaProizvoda;
  }
HSB
  • 18
  • 2
  • Thank you so much. That fixed the problem. Also do you now how to link two struct objects together. Beacuse the problem I have is I need to cout the name of the cars (naziv) that have 0 count (kolicina) ? – Samke11 May 06 '20 at 17:46