Sep 26, 2013 at 1:26pm UTC
Hello! I have one question with my C++ program.
I would like to ask why my program prints just last text file numbers and lessons?
Here's my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
void SkPamoka(const char CD[], Pamoka p[], int & sum){
string klase, diena, pamoka;
int nr;
ifstream fd(CD);
sum = 0;
while (!fd.eof()){
fd >> klase >> diena;
while (fd.peek() != '\n' && !fd.eof() ){
fd >> nr >> pamoka;
p[sum].DetiKlase(klase);
p[sum].DetiDiena(diena);
p[sum].DetiNr(nr);
p[sum].DetiPamoka(pamoka);
}
sum++;
}
fd.close();
}
void Spausdinti(const char FR[], Pamoka p[], Mokytojai m[], int sum, int k, string A, int B){
ofstream fr(FR);
fr.setf(ios::fixed);
fr.setf(ios::left);
fr << "-----------Students Timetable----------" << endl;
fr << "| Class | Day | Nr | Lesson |" << endl;
for (int i=0; i<sum; i++){
fr << "| " << setw(12) << p[i].ImtiKlase() << "|" << setw(10) << p[i].ImtiDiena() << "|" << setw(9) << p[i].ImtiNr() << "|" << p[i].ImtiPamoka() << endl;
}
}
p[i]Imtipamoka() and p[i]imtiNr() they just print me last number and last lesson on txt file why? :/
Last edited on Sep 26, 2013 at 1:27pm UTC
Sep 26, 2013 at 1:45pm UTC
Thank You! But now I have another problem i need that all my data would be written to for examople
2a monday 1 history 2 math 3biology 4 sport
| Class | Day | Nr |Lesson |
------------------------------------------------
| 2a |monday |1 |history|
| 2a |monday |2 |math|
| 2a |monday |3 |biology|
| 2a |monday |4 |Sport|
| 2a |tuesday |1 |math|
| 2a |tuesday |2 |history|
| 2a |tuesday |3 |art|
| 2a |tuesday |4 |math|
| 2a |wednesday |1 |x|
| 2a |wednesday |2 |history|
| 2a |thursday |1 |math|
| 2a |thursday |2 |programming|
| 2a |thursday |3 |art|
| 2a |friday |1 |math|
| 2a |friday |2 |Sport|
| 2b |monday |1 |math|
| 2b |monday |2 |biology|
| 2b |monday |3 |art|
| 2b |monday |4 |Sport|
| 2b |tuesday |1 |art|
| 2b |tuesday |2 |x|
| 2b |tuesday |3 |biology|
| 2b |tuesday |4 |music|
| 2b |wednesday |1 |biology|
| 2b |wednesday |2 |x|
| 2b |wednesday |3 |music|
| 2b |thursday |1 |histrory|
| 2b |thursday |2 |math|
| 2b |thursday |3 |art|
| 2b |friday |1 |music|
| 2b |friday |2 |Sport|
------------------------------------------------
so how I need to fix it?
Last edited on Sep 26, 2013 at 1:46pm UTC
Sep 26, 2013 at 2:42pm UTC
You do have two options:
1. Different data structure. One that has multiple number-topic entries listed for each class-day entry, just like in the file.
2. A smarter loop. Now you print class-day-number-topic-endl and that is not what you want. You want to print only the number-topic, except when the class-day differs from previous. Then you want the endl-class-day too.
Sep 26, 2013 at 3:10pm UTC
Yes without them my program just *break. But how I need to sort everythin?
Last edited on Sep 26, 2013 at 3:37pm UTC
Sep 26, 2013 at 3:37pm UTC
Sorry :) I'm from Lithuania :)
By the way maybe you know how to make sorting?