So i was in a making of my homework where im supposed to write a program to write (output) 2 tables to .txt file 1) given data 2) calculations that were done with that data.

heres the code

/* L3-7. Turime tekstiniame faile studentų apskaitos sąrašą:
pavardė, vardas, įstojimo į Universitetą metai, visų egzaminų pažymiai.
Suformuoti sąrašą: pavardė, vardas, kursas, aštuntukų, devintukų dešimtukų kiekiai.
Išvesti dvi lenteles: duomenų ir skaičiavimų rezultatų.
*/
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>

using namespace std;
//---------------------------------------------------------------
const char CDfv[] = "studentai.txt";
const char CRfv[] = "Rezultatai.txt";
const int CMaksimalus = 200;
//const int Dmetai = 2011;
//---------------------------------------------------------------
class Studentas
{
private:
	string pavardė;
	string vardas;
	int įmetai;
	int kursas;
	int epažymiai[20];
	int rusiuoti[10];
	int kiekis;
public:
	Studentas();
	Studentas(string pav, string var, int įmet, int kur, int kiek);
	~Studentas();
	void DėtiPavardę(string x);
	void DėtiVardą(string x);
	void DėtiĮMetus(int x);
	void DėtiPažymius(int x, int reiksme);
	void DetiKiekis(int x);
	void DetiRusiuoti(int x, int reiksme);
	void DetiKursas(int x);
	string ImtiPavardę();			
	string ImtiVardą();			
	int ImtiĮMetus();
	int ImtiKursą();
	int ImtiPažymius(int x);
	int ImtiRusiuotus(int x);
	int ImtiKieki();
};
//---------------------------------------------------------------
void Duomenys (const char fv[], Studentas S[], int & n);
void Spausdinti (const char fv[], Studentas S[], int n);
//---------------------------------------------------------------
int main()
{
	setlocale(LC_ALL, "Lithuanian");
	Studentas S[CMaksimalus];
	int n;
	Duomenys(CDfv, S, n);
	Spausdinti(CRfv, S, n);
	return 0;
}
//---------------------------------------------------------------
Studentas::Studentas(string pav, string var, int įmet, int kur,  int kiek):pavardė(pav), vardas(var), įmetai(įmet), kursas(kur),  kiekis(kiek){ }
Studentas::Studentas(): pavardė(""), vardas(""), įmetai(0), kursas(0), kiekis(0) { }
Studentas::~Studentas() { }
//---------------------------------------------------------------
string Studentas::ImtiPavardę()
{
	return pavardė;
}
//---------------------------------------------------------------
string Studentas::ImtiVardą()
{
	return vardas;
}
//---------------------------------------------------------------
int Studentas::ImtiĮMetus()
{
	return įmetai;
}
//---------------------------------------------------------------
int Studentas::ImtiPažymius(int x)
{
	return epažymiai[x];
}
//---------------------------------------------------------------
int Studentas::ImtiKursą()
{
	return kursas;
}
int  Studentas::ImtiRusiuotus(int x)
{
	return rusiuoti[x];
}
int  Studentas::ImtiKieki()
{
	return kiekis;
}
//---------------------------------------------------------------
void Studentas::DėtiPavardę(string x)
{
	pavardė=x;
}
//---------------------------------------------------------------
void Studentas::DėtiVardą(string x)
{
	vardas=x;
}
//---------------------------------------------------------------
void Studentas::DėtiĮMetus(int x)
{
	įmetai=x;
}
//---------------------------------------------------------------
void Studentas::DėtiPažymius(int x, int reiksme)
{
	epažymiai[x]=reiksme;
}
void Studentas::DetiKursas(int x)
{
	kursas=x;
}
void  Studentas::DetiKiekis(int x)
{
	kiekis=x;
}
void  Studentas::DetiRusiuoti(int x, int reiksme)
{
	rusiuoti[x]=reiksme;
}
//---------------------------------------------------------------
void Duomenys (const char fv[], Studentas S[], int & n)
{
	ifstream fd(fv);
	string pav, var;
	int įmet, kur, paž[20], kiek, rus[10], pažym;
	fd >> n;
	for (int i = 0; i < n; i++) {
		for(int j=0;j<10;j++)
			rus[j]=0;
		fd >> pav >> var >> įmet;
		kur=2012-įmet;
		kiek=0;
		while(fd.peek() != '\n' && !fd.eof())
		{
			fd >> pažym;
			rus[pažym-1]++;
			paž[kiek]=pažym;
			kiek++;
		}

		S[i].DėtiPavardę(pav);
		S[i].DėtiVardą(var);
		S[i].DetiKursas(kur);
		S[i].DetiKiekis(kiek);
		S[i].DėtiĮMetus(įmet);
		for(int p=0; p<20;p++)
		S[i].DėtiPažymius(p,paž[p]);
		for(int p=0; p<10;p++)
		S[i].DetiRusiuoti(p,rus[p]);


	}
	fd.close();
}

//---------------------------------------------------------------
void Spausdinti(const char fv[], Studentas S[], int n)
{
	ofstream fr(fv);
	fr << "Pradiniai Duomenys\n" << endl;
	fr << "------------------------------------------------------------------------------\n" << endl;
	fr  << setw(20) << left << "Pavardė" << setw(10) << "Vardas" << left << setw(16) << "Įstojimo metai" << "Egzaminu pažymiai" << endl;
	for(int i = 0; i < S[i].ImtiKieki() ; i++)
	{
	fr << left << setw(20) << S[i].ImtiPavardę() << setw(14) << S[i].ImtiVardą() << fixed << right << setw(4) << right << S[i].ImtiĮMetus() << "       |";
// cycle to print given numbers (students grades), maybe something's wrong here?
	for (int x = 0; x < S[i].ImtiKieki(); x++)
	fr << fixed << right << S[i].ImtiPažymius(x) << " ";
	fr << endl;
	}
	fr << "------------------------------------------------------------------------------\n" << endl;
	fr << " Skaičiavimų rezultatai" << endl;
	fr << "------------------------------------------------------------------------------\n";
	fr << " Pavardė            Vardas         Kursas	 8 kiekis  9 kiekis  10 kiekis|\n";
	fr << "------------------------------------------------------------------------------\n";
	for (int i = 0; i < n; i++)
	{
		fr 
		<< left << setw(20) << S[i].ImtiPavardę()
		<< setw(14) << S[i].ImtiVardą()
		<< fixed << right << setw(4) << S[i].ImtiKursą() << "       |";
		fr 
		<< fixed << right << setw(8) << S[i].ImtiRusiuotus(7) << " "
		<< fixed << right << setw(9) << S[i].ImtiRusiuotus(8) << " "
		<< fixed << right << setw(8) << S[i].ImtiRusiuotus(9) << "     |";

		 fr <<endl;
	}
	fr << "------------------------------------------------------------------------------\n";
	fr.close();
}

its my data file with a list of students and related data:

9
Petraitis  Jonas     2010 10 8 9 7
Magnis     Rimantas  2011 8 9 8
Keistuolis Mantas    2008 10 10 9 10 8 9 9 10
Rimtuolis  Petras    2011 9 10 8
Kaimynas   Jurgis    2009 7 8 8 7 9 8
Mažylis    Tomas     2010 9 10 8 9
Kilimėlis  Haroldas  2008 8 6 4 10 8 9 9 7
Šviesuolis Lukas     2009 8 9 10 9 8 9
Ramutis    Dovydas   2010 9 10 10 8

here's what I get

Pradiniai Duomenys

------------------------------------------------------------------------------

Pavardė             Vardas    Įstojimo metai  Egzaminu pažymiai
Petraitis           Jonas         2010       |10 8 9 7 
Magnis              Rimantas      2011       |8 9 8 
Keistuolis          Mantas        2008       |10 10 9 10 8 9 9 10 
------------------------------------------------------------------------------

 Skaičiavimų rezultatai
------------------------------------------------------------------------------
 Pavardė            Vardas         Kursas	 8 kiekis  9 kiekis  10 kiekis|
------------------------------------------------------------------------------
Petraitis           Jonas            2       |       1         1        1     |
Magnis              Rimantas         1       |       2         1        0     |
Keistuolis          Mantas           4       |       1         3        4     |
Rimtuolis           Petras           1       |       1         1        1     |
Kaimynas            Jurgis           3       |       3         1        0     |
Mažylis             Tomas            2       |       1         2        1     |
Kilimėlis           Haroldas         4       |       2         2        1     |
Šviesuolis          Lukas            3       |       2         3        1     |
Ramutis             Dovydas          2       |       1         1        2     |
------------------------------------------------------------------------------

the problem is that for some uknown reason I'm missing 6 students in upper given data list, any ideas why?

Recommended Answers

All 4 Replies

Probably the line in your code:

for(int i = 0; i < S[i].ImtiKieki() ; i++)

I think you meant for this line to specify "i < n". Happy coding!

No the line is fine, it takes the amount of grades or the grades itself, either way if i change it to what you wrote, I get same 3 lanes but grades are messed up plus some of them are random long numbers

I think you changed the wrong line. It's hard to say, since the source code didn't get line numbers when you posted it. The "for i" line, not the "for x" line. Here, I'll copy it:

fr << setw(20) << left << "Pavardė" << setw(10) << "Vardas" << left
   << setw(16) << "Įstojimo metai" << "Egzaminu pažymiai" << endl;
for(int i = 0; i < S[i].ImtiKieki() ; i++)
{
    fr << left << setw(20) << S[i].ImtiPavardę() << setw(14) << S[i].ImtiVardą()
       << fixed << right << setw(4) << right << S[i].ImtiĮMetus() << "       |";
    // cycle to print given numbers (students grades), maybe something's wrong here?
    for (int x = 0; x < S[i].ImtiKieki(); x++)
        fr << fixed << right << S[i].ImtiPažymius(x) << " ";
    fr << endl;
}

Line 3 above (not line 8).

Yup, that worked just fine, thanks

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.