Thread: problem defening a map of structs

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I try to put in read a string read from a text-file in pieces.
    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 ;
        }
    }
    I add a breakpoint in this rule : myBuf= str.substr(nextPos, (prevPos-nextPos+1));
    But when i run the programm the programm doesn't break at the breakpoint

    Roelof

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    230
    Nobody who can tell me what's wrong wit the code.

    Roelof

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does it open the file? Ie, no "file not found" error message?
    If so, you could post the contents of the file for others to test?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    230
    It opens the file.

    The contents of the file is one rule"
    Code:
    msk;mac;80;20
    Roelof

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code works fine for me. It must be your IDE/settings/debugger.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Then I have to look into the manual of code::blocks how a breakpoint works.

    Roelof

  8. #23
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Another problem.
    I have this code :
    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 ;
        }
    }
    But why does all variables have one value if woord==1 .
    I thought that only the expression are carried out when woord=1

    Roelof

  9. #24
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  11. #26
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post
    Why do all the other get values and woord is still 1
    Let's look at all the places in that code where woord occurs:
    Code:
        int woord=1  ;
    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:
                                cout << "woord : " << woord ;
    Outputing the value of woord will not change it.
    Code:
                                if (woord==1)
                                if (woord==2)
                                if (woord==3)
    Testing the value of woord will not change it either.

    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

  12. #27
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with structs
    By darkstorm in forum C++ Programming
    Replies: 4
    Last Post: 06-03-2009, 04:18 PM
  2. Menu problem involving structs
    By NyHoK in forum C Programming
    Replies: 5
    Last Post: 03-31-2009, 10:00 AM
  3. Picture download problem
    By wingri in forum C Programming
    Replies: 10
    Last Post: 07-31-2007, 05:32 AM
  4. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  5. problem with a map iterator
    By anykey in forum C++ Programming
    Replies: 17
    Last Post: 04-29-2005, 11:49 PM