Currently working with graphical interface and dynamic object arrays, Got two text files with kinda same data on it, my current task is just to read and output that data. Problem is most likely either with my read function or function to determine how much data there is in files (or both) because after trying many variations to make it work properly it either prints out not all of the rows that are in files, makes empty rows inbetween of each other and also doesnt print them all out or is messed up completely OR it seems everything is fine but last row duplicates (that's the closest I've got into getting it to work properly)

Also here's project and its files if you cant be arsed just to read here - http://stud.if.ktu.lt/~andvla/Laborotorinis_1.zip

Here's both text files:

Birzai.txt

Biržų
Jonaitis,      Audrius,    Agliuonos g. 5,        8450035028
Bakaitis,      Kastas,     Kęstučio g. 15,        8450885307
Čižovas,       Modestas,   J. Janonio g. 7,       8450135182
Mitinas,       Vaidilas,   Upės g. 27,            8450575121
Ragalskis,     Mikalojus,  Apaščios Kranto g. 12, 8450140428
Kumpinis,      Elvinas,    Pakalnės g. 53,        8450554381
Dzevyžis,      Legaudas,   Kilučių g. 17,         8450675695
Adomaitis,     Rolandas,   Lieptų g. 2,           8450957745
Žvirėnas,      Narvydas,   Bitės g. 8,            8450213494

Kaunas.txt

Kauno
Budrionienė,    Algina,     Uosio g. 22,        837224857
Karasiovas,     Tomas,      Žibučių g. 9,       837971421
Šeivala,        Zigmas,     Darbininkų g. 2,    837233724
Kaziulionienė,  Regina,     Savanoriu pr. 367,  837671081
Zagnojus,       Daugvydas,  Saulės g. 31,       837801380
Žurankovas,     Elvinas,    Taikos pr. 78,      837437392
Liumparas,      Gytis,      V. Krėvės pr. 112,  837388918
Majauskas,      Jokūbas,    Partizanų g. 99,    837158071
Abariūnas,      Kasparas,   P. Lukšio g. 12,    837628794

Rezults file Rezultatai.txt

Kauno miesto telefono abonetų sąrašas
----------------------------------------------------------------------------------
| Pavardė       | Vardas          | Adresas                   | Telefono Numeris |
----------------------------------------------------------------------------------
Budrionienė     | Algina          | Uosio g. 22               | 837224857        |
Karasiovas      | Tomas           | Žibučių g. 9              | 837971421        |
Šeivala         | Zigmas          | Darbininkų g. 2           | 837233724        |
Kaziulionienė   | Regina          | Savanoriu pr. 367         | 837671081        |
Zagnojus        | Daugvydas       | Saulės g. 31              | 837801380        |
Žurankovas      | Elvinas         | Taikos pr. 78             | 837437392        |
Liumparas       | Gytis           | V. Krėvės pr. 112         | 837388918        |
Majauskas       | Jokūbas         | Partizanų g. 99           | 837158071        |
Abariūnas       | Kasparas        | P. Lukšio g. 12           | 837628794        |
Abariūnas       | Kasparas        | P. Lukšio g. 12           | 837628794        |
----------------------------------------------------------------------------------

Biržų miesto telefono abonetų sąrašas
----------------------------------------------------------------------------------
| Pavardė       | Vardas          | Adresas                   | Telefono Numeris |
----------------------------------------------------------------------------------
Jonaitis        | Audrius         | Agliuonos g. 5            | 8450035028       |
Bakaitis        | Kastas          | Kęstučio g. 15            | 8450885307       |
Čižovas         | Modestas        | J. Janonio g. 7           | 8450135182       |
Mitinas         | Vaidilas        | Upės g. 27                | 8450575121       |
Ragalskis       | Mikalojus       | Apaščios Kranto g. 12     | 8450140428       |
Kumpinis        | Elvinas         | Pakalnės g. 53            | 8450554381       |
Dzevyžis        | Legaudas        | Kilučių g. 17             | 8450675695       |
Adomaitis       | Rolandas        | Lieptų g. 2               | 8450957745       |
Žvirėnas        | Narvydas        | Bitės g. 8                | 8450213494       |
Žvirėnas        | Narvydas        | Bitės g. 8                | 8450213494       |
----------------------------------------------------------------------------------

Class Miestas1.h

#pragma once
#include <string>

using namespace std;

class Miestas1
{
private:
	string pavarde, vardas, adresas, telNumeris;
public:
	Miestas1();
	Miestas1(string pav, string var, string adr, string tnr);
	~Miestas1();

	string ImtiPavarde();
	string ImtiVarda();
	string ImtiAdresa();
	string ImtiTelNumeri();
	
	string Spausdinti();

	void Rikiuoti();
	bool operator > (const Miestas1 & kitas);
};

Miestas1.cpp

//#include "StdAfx.h"
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include "Miestas1.h"

// konstruktorius be parametru
Miestas1::Miestas1(void) : pavarde(""), vardas(""), adresas(""), telNumeris("")
{}
// konstruktorius su parametrais
Miestas1::Miestas1(string pav, string var, string adr, string tnr) :
					pavarde(pav), vardas(var), adresas(adr), telNumeris(tnr)
{}
// destruktorius
Miestas1::~Miestas1(void)
{}

//------------------------------
string Miestas1::ImtiPavarde()
{
	return pavarde;
}
string Miestas1::ImtiVarda()
{
	return vardas;
}
string Miestas1::ImtiAdresa()
{
	return adresas;
}
string Miestas1::ImtiTelNumeri()
{
	return telNumeris;
}
//-------------------------------
string Miestas1::Spausdinti()
{
	stringstream fr;
	fr << left << setw(15) << pavarde << " | "
						<< setw(15) << vardas << " | "
						<< setw(25) << adresas << " | "
						<< setw(16) << telNumeris << " |";
		return fr.str();
}

bool Miestas1::operator >(const Miestas1 & kitas)
	{
		return vardas > kitas.vardas && telNumeris > kitas.telNumeris;
	}

Code fragments from Form1.h

HowMuchDataInFile:

int KiekDuomenuFaile(const char fv[])
{
  ifstream fd(fv);
  string eil;
  int kiek = 0;
  while (!fd.eof()) {
    getline(fd, eil);
    kiek++;
  }
  fd.close();
  return kiek;
}

current reading function:

void IvestiMiesta1(const char fv[], string & abonentai, Miestas1 M[], int k)
{
  string pavarde, vardas, adresas, telNumeris;
  ifstream fd(fv);
//  int i = 0;
  fd >> abonentai;
//  while (!fd.eof())
  for(int i = 0; i < k; i++) 
  {
	fd >> ws;
    getline(fd, pavarde, ',');
	fd >> ws;
    getline(fd, vardas, ',');
	fd >> ws;
    getline(fd, adresas, ',');
	fd >> ws;
	fd >> telNumeris;
    fd.ignore();

   M[i] = Miestas1(pavarde, vardas, adresas, telNumeris);
  // i++;
  }
  fd.close();
}

current output function:

void Spausdinti(const char fv[], Miestas1 M[], int k, string komentaras)
{
  ofstream fr(CRez, ios::app);
  fr << komentaras << endl;
  fr << "----------------------------------------------------------------------------------\n";
  fr << "| Pavardė       | Vardas          | Adresas                   | Telefono Numeris |\n";
  fr << "----------------------------------------------------------------------------------\n";
  for (int i = 0; i < k; i++)
	  fr << M[i].Spausdinti() << endl;
  fr << "----------------------------------------------------------------------------------" << endl << endl;
  fr.close();
}

Main()

setlocale(LC_ALL, "Lithuanian");

  ofstream fr(CRez); fr.close();
  string abonentai, komentaras;

  Miestas1 *M; int k; 

  k = KiekDuomenuFaile(CDuom1);  
  M = new Miestas1[k]; 
  //---------------------------
  IvestiMiesta1(CDuom1, abonentai, M, k);
  Spausdinti(CRez, M, k, abonentai + " miesto telefono abonetų sąrašas");
  //---------------------------
//  delete [] M;
  //-------------------------------------------------------------------
 // k = KiekDuomenuFaile(CDuom2);  
 // M = new Miestas1[k]; 
  //---------------------------
  IvestiMiesta1(CDuom2, abonentai, M, k);
  Spausdinti(CRez, M, k, abonentai + " miesto telefono abonetų sąrašas");
  //---------------------------
  delete [] M;

Recommended Answers

All 6 Replies

I am not entirely certain, but maybe the problem has to do with the unusual characters? You may need to store them in an int and not a char to allow for long unicodes. It may be easier if you translate the code into english.

I'm almost certain as well that unusual characters in text files has nothing to do with this, and i did not use any lithuanian letters in code, just words, will translate functions in few minutes, I suppose you dont really care about class.

Well your code would be easier to understand in english. I can sort of assume what each function and/or variable will do, but it is hard in lithuanian.

Here's everything in english:

class Miestas1.h

pragma once
#include <string>
 
using namespace std;
 
class City1
{
private:
string surename, name, adress, phoneNumber;
public:
City1();
City1(string sur, string nam, string adr, string pnr);
~City1();
 
string TakeSurename();
string TakeName();
string TakeAdress();
string TakePhoneNumber();
 
string Output();
 
void Allign();
bool operator > (const City1 & other);
};

Miestas1.cpp

//#include "StdAfx.h"
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <iomanip>
    #include "Miestas1.h"
     
    City1::City1(void) : surename(""), name(""), adress(""), phoneNumber("")
    {}
    City1::City1(string sur, string nam, string adr, string pnr) :
    surename(sur), name(nam), adress(adr), phoneNumber(pnr)
    {}
    City1::~City1(void)
    {}
     
    //------------------------------
    string City1::TakeSurename()
    {
    return surename;
    }
    string City1::TakeName()
    {
    return name;
    }
    string City1::TakeAdress()
    {
    return adress;
    }
    string City1::TakePhoneNumber()
    {
    return phoneNumber;
    }
    //-------------------------------
    string City1::Output()
    {
    stringstream fr;
    fr << left << setw(15) << surename << " | "
    << setw(15) << name << " | "
    << setw(25) << adress << " | "
    << setw(16) << phoneNumber << " |";
    return fr.str();
    }
     
    bool City1::operator >(const City1 & other)
    {
    return name > other.name && phoneNumber > other.phoneNumber;
    }

function Howmuchdata

int HowMuchDataInFile(const char fv[])
    {
    ifstream fd(fv);
    string row;
    int howmuch = 0;
    while (!fd.eof()) {
    getline(fd, row);
    howmuch++;
    }
    fd.close();
    return howmuch;
    }

Input function:

void InputCity1(const char fv[], string & people, City1 M[], int k)
    {
    string surename, name, adress, phoneNumber;
    ifstream fd(fv);
    // int i = 0;
    fd >> people;
    // while (!fd.eof())
    for(int i = 0; i < k; i++)
    {
    fd >> ws;
    getline(fd, surename, ',');
    fd >> ws;
    getline(fd, name, ',');
    fd >> ws;
    getline(fd, adress, ',');
    fd >> ws;
    fd >> phoneNumber;
    fd.ignore();
     
    M[i] = City1(surename, name, adress, phoneNumber);
    // i++;
    }
    fd.close();
    }

output function:

void Output(const char fv[], City1 M[], int k, string comment)
    {
    ofstream fr(CRez, ios::app);
    fr << comment << endl;
    fr << "----------------------------------------------------------------------------------\n";
    fr << "| surename | name | Adress | Phone Number |\n";
    fr << "----------------------------------------------------------------------------------\n";
    for (int i = 0; i < k; i++)
    fr << M[i].Output() << endl;
    fr << "----------------------------------------------------------------------------------" << endl << endl;
    fr.close();
    }

My guess is that your HowMuchDataInFile function needs to return the value - 1 because of the name of the city occupying the first line of the file. IE: return howmuch-1;

GENIUS! Works so far, unless my lecturer will find some kind of other bug that will screw me up, which I hope he does not ha, thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.