Oke,
Everyone thanks for the help,
I going to the way of using two maps . One struct for making the ranking and a vector for sorting.
Roelof
Oke,
Everyone thanks for the help,
I going to the way of using two maps . One struct for making the ranking and a vector for sorting.
Roelof
Oke,
I try to put in read a string read from a text-file in pieces.
I add a breakpoint in this rule : myBuf= str.substr(nextPos, (prevPos-nextPos+1));Code:#include <iostream> #include <map> #include <string> #include <fstream> using namespace std; struct teamstats { string team_name; int played_games, point_made, points_against; }; int main() { map<string, teamstats> allteams; string str,myBuf, home_team, away_team, home_score, away_score; int teller=1 ; ifstream a_file ("test.txt"); if (!a_file.good()) cerr << "File not found!\n"; while (a_file >> str) { int prevPos=str.size()-1; int nextPos=str.size()-1; while((str.npos != nextPos)&&(nextPos)) { prevPos=nextPos; nextPos=str.rfind(";", prevPos); if(nextPos==str.npos) { nextPos=0; myBuf= str.substr(nextPos, (prevPos+1)); } else { nextPos+=1; myBuf= str.substr(nextPos, (prevPos-nextPos+1)); if(nextPos==1) nextPos+=1; nextPos-=2; } } return 0 ; } }
But when i run the programm the programm doesn't break at the breakpoint
Roelof
Nobody who can tell me what's wrong wit the code.
Roelof
It opens the file.
The contents of the file is one rule"
RoelofCode:msk;mac;80;20
Oke,
Then I have to look into the manual of code::blocks how a breakpoint works.
Roelof
Oke,
Another problem.
I have this code :
But why does all variables have one value if woord==1 .Code:#include <iostream> #include <map> #include <string> #include <fstream> using namespace std; struct teamstats { string team_name; int played_games, point_made, points_against, woord; }; int main() { map<string, teamstats> allteams; string str,myBuf , home_team, away_team, home_score, away_score; int woord=1 ; ifstream a_file ("test.txt"); if (!a_file.good()) cerr << "File not found!\n"; while (a_file >> str) { int prevPos=str.size()-1; int nextPos=str.size()-1; while((str.npos != nextPos)&&(nextPos)) { prevPos=nextPos; nextPos=str.rfind(";", prevPos); if(nextPos==str.npos) { nextPos=0; home_team= str.substr(nextPos, (prevPos+1)); cout<<"Thuisspelende team :" << home_team; } else { cout << "woord : " << woord ; nextPos+=1; if (woord==1) { away_score = str.substr(nextPos, (prevPos-nextPos+1)); cout << "tegengescoord :" << away_score ; }; if (woord==2) { home_score = str.substr(nextPos, (prevPos-nextPos+1)); cout << "zelfgemaakte doelpunten :" << home_score ; }; if (woord==3) { away_team = str.substr(nextPos, (prevPos-nextPos+1)); cout << "Tegenpartij :" << away_team ; }; if(nextPos==1) nextPos+=1; nextPos-=2; } } return 0 ; } }
I thought that only the expression are carried out when woord=1
Roelof
You set woord = 1 and then it never changes, so woord always == 1. Why do you think it's value should have somehow magically changed?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Hello.
I know that but if woord=1 i put in de code that only away_score get a value.
Why do all the other get values and woord is still 1
Roelof
Let's look at all the places in that code where woord occurs:
Declaration and initialization, woord is assigned a value of 1. Nb. this has nothing to do with the fact that struct teamstats has a member named woord (maybe that is what's confused you?).Code:int woord=1 ;
Outputing the value of woord will not change it.Code:cout << "woord : " << woord ;
Testing the value of woord will not change it either.Code:if (woord==1) if (woord==2) if (woord==3)
Woord is not used anywhere else. Hence, woord is always 1. Again, the struct teamstats member "woord" is totally unrelated to "int woord" from main().
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Oke,
That word is a member of teamstats is a error of me.
What i mean is that if a run this programm the ouput looks like this :
Woord : 1
home_score : 20
away_score : 20
away_team : 20
When I expect :
Home_score :
away_score : 20
away_team :
That is my question.
Roelof
Hello,
Problem solved on this way :
Code:else { cout << "woord : " << woord ; nextPos+=1; if (woord==1) { away_score = str.substr(nextPos, (prevPos-nextPos+1)); cout << "tegengescoord :" << away_score ; }; if (woord==2) { home_score = str.substr(nextPos, (prevPos-nextPos+1)); cout << "eigen gescoord :" << home_score ; }; if (woord==3) { away_team = str.substr(nextPos, (prevPos-nextPos+1)); cout << "eigen gescoord :" << away_team ; }; woord +=1; if(nextPos==1) nextPos+=1; nextPos-=2; }
Roelof