Thread: what's the best way to achieve this

  1. #76
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Next attempt :
    Code:
    #include <iostream>
    #include <cstring>
    #include <sstream>
    #include <fstream>
    
    using namespace std;
    
    int input_games()
    {
    
        string home_team , away_team, game ;
        int lengte ;
        do
        {
            cout<< "Home team :"; // inlezen van de team die thuis speelt
            getline(cin,home_team);
            lengte = home_team.length() ; // controle of de gebruiker iets ingevoerd heeft
            if (lengte==0)
            {
                cout<< "input is empty. There must be a input";
            }
        }
        while (lengte==0);
        do
        {
            cout<< "Away team :"; // inlezen van het uitspelende team
            getline(cin,away_team);
            lengte = away_team.length() ; //controle of de gebruiker iets ingevoerd heeft
            if (lengte==0)
            {
                cout<< "input is empty. There must be a input";
            }
        }
        while (lengte==0);
        int home_score;
        for (;;)
        {
            cout << "Enter the home score: "; // Inlezen van de soore van de thuisspelende team
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> home_score && ss.eof()) // controle of er alleen cijfers zijn ingevoerd
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
        int away_score;
        for (;;)
        {
            cout << "Enter the away score: "; // invoeren van de score van het uitspelende team
            string line;
            getline(cin, line);
            stringstream ss(line);
            if (ss >> away_score && ss.eof()) // controle of er alleen maar cijfers zijn ingevoerd
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
        }
        ofstream a_file ( "test.txt"); // open de tekstfile and zet de gegevens erin
        a_file << home_team << ";" << away_team << ";" << home_score << ";" << away_score ;
        a_file.close() ;
        return 0;
    }
    
    string get_user_input() // invoeren van de keuze van het hoofdmenu
    {
        string input;
    
        do
        {
            cout<<"1) Input game.: "<<endl;
            cout<<"2) Making the rank.: "<<endl;
            cout<<"3) Quit.: "<<endl;
            getline(cin, input);
            if ( input!="1" && input!="2" && input!="3" )
            {
                cout<< "Input must be 1 or 2 or 3" <<endl;
            }
        }
        while ( input!="1" && input!="2" && input!="3"); // while not 1, 2, or 3
        return input;
    }
    
    int main(){
    
        string input ;
    
        do
        {
            input = get_user_input();
            if( input == "1" )
            {
                input_games(); // als de gebruiker 1 kiest ga dan naar invoeren van de wedstrijdgevens
            }
            else
            if( input == "2" )
            {
            /* do whatever */ 
            }
        } // als de gebruiker 2 kiest , ga dan naar het maken van de stand
        while (input!="3"); // laat het menu zien zolang de gebruiker geen 3 kiest
        return 0;
    }
    Roelof

  2. #77
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
            if( input == "2" )
            {
            /* do whatever */ 
            }
    /* do whatever */ should have an extra indentation level. Everything else looks perfect.
    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.

  3. #78
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's a quick point:

    Code:
            if (ss >> home_score && ss.eof()) // controle of er alleen cijfers zijn ingevoerd
            {
                break;
            }
            else
            {
                cout << "Invalid input: enter a number only.\n";
            }
    You have a lot of these.* Single statements don't need blocks:
    Code:
            if (ss >> home_score && ss.eof()) break; 
            else cout << "Invalid input: enter a number only.\n";
    * I think it's called "allmanitis"
    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

  4. #79
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    You have a lot of these.* Single statements don't need blocks:
    On the other hand, having the braces is also consistent. It is a matter of style, so if you do not want to change, don't change. After all, if you cure "Allmanitis", you may end up with "MK27itis", which is worse

    What I recommend is writing a function to remove the duplication concerning home_score and away_score.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #80
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    A question for the next problem.

    For making the ranks I will read the text-file one by one.
    Then I would store them in a map of structs.

    Do I need to make a new struct for every new team or can I just insert the values in the struct ?

    A function can be a solution as you said.
    But I don't know if I as noob can write it.

    But why not make a function for home_team and away_team to look if there are empty?

    Roelof

  6. #81
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post
    For making the ranks I will read the text-file one by one.
    Then I would store them in a map of structs.
    I think you mean vector of structs?

    Do I need to make a new struct for every new team or can I just insert the values in the struct ?
    If there is only one struct what is the vector for? If you don't need to keep the data for each team and use it concurrently, you can overwrite the same struct repeatedly, yes.

    A function can be a solution as you said.
    But I don't know if I as noob can write it.
    Ah but you should try! It is not so hard. All you want to do is set a parameter for the question and a reference to the int you want to set:
    Code:
    void setscore(string team, int &score);
    So you could make a call to this:
    Code:
    setscore("home", home_score);
    Make sense?
    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

  7. #82
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello MK27,

    That can be.

    What I was thinking about is this :

    Struct :

    Team-name string
    played games int
    game-points (3 for every won game)
    made-points
    points_against

    So I read a line from the text-file.
    Insert the values in home-team, away-team, home_score and away_score.

    Then compare if the home-team is present in the struct.
    If not, insert the home-team in the struct.
    Then played_games increase by 1
    Look if the team has won. If so, game-points increase by 3
    Increase points_made and points_against with home_score and away_score.

    Same story for the away_team.

    Roelof

  8. #83
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I honestly don't follow what you are trying to do. I think you start coding it and work from there.
    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

  9. #84
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I give a example.

    Lets say that in the text -file there is this rule:
    msk;mac;80;20

    What I want to do is this.

    Read this line.
    Then put it into variables.
    So
    home_team = msk
    away_team = mac
    home_score = 80
    away_score = 20

    Now I have to look if MSK is already in the struct.
    If not :
    Then msk must be in the struct
    Played_games must be 1
    MSK has won this game so games_point must be 3
    Made_points must be 80
    Against_points must be 20

    Then the next rule will be read and the same has to happen.

    Is it now more clear what I want to achieve.

    Roelof

  10. #85
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post
    Is it now more clear what I want to achieve.
    Possibly. By "in the struct" you mean an array or vector of such structs?

    Anyway, this is a good purpose for a map. If your struct is called "struct teamstats" you could use a
    Code:
    map<string,struct teamstats> allteams;
    Then when you read a line, you get the team name and check and if there is a already such an item, eg:
    Code:
    map<string,struct teamstats>::iterator it = allteams.find("MSK");
    if (it == allteams.end()) // MSK does not exist yet
    Nb, in this case the struct does not contain the team name -- that is the string (key) part of the map, which you can search and find as above. That key corresponds to a struct instance which contains the details for the team named in the string.
    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

  11. #86
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    map<string,struct teamstats>::iterator it = allteams.find("MSK");
    Remove "struct":
    Code:
    map<string, teamstats>::iterator it = allteams.find("MSK");
    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.

  12. #87
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    And when I want to increase lets say played_games of a team.
    Can I just do played_games = played_games + 1

    And another question : Don't I have to tell how what the struct teamstats contains?
    Iterator contains now the team-name or am I mistaken ?

    Roelof

    Edit : Oke, I have to use teamstats.played_games.
    But how can I be sure the right one is being used when there are more then one team.
    Last edited by roelof; 06-04-2010 at 02:19 PM.

  13. #88
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    sure, or possibly

    Code:
    played_games += 1;
    
    // or
    
    played_games++;
    as to your second question...


    it is your job to keep track of which teamstat object is being modified. I believe a map was suggested.

    As long as you know what teamstat the iterator is pointing to, then you have no worries.
    goto( comeFrom() );

  14. #89
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    But don't I have to do something like this
    Code:
    struct database {
    string team_name
    int played_games, point_made, points_against 
    }
    I know which teamstat the iterator is pointing to by using this
    Code:
    map<string,struct teamstats>::iterator it = allteams.find("MSK");
    Roelof

  15. #90
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by roelof View Post
    Oke,

    But don't I have to do something like this
    Code:
    struct database {
    string team_name
    int played_games, point_made, points_against 
    }
    I know which teamstat the iterator is pointing to by using this
    Code:
    map<string,struct teamstats>::iterator it = allteams.find("MSK");
    Roelof
    yes, and add some semi-colons, and it might compile.
    goto( comeFrom() );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-31-2008, 01:26 PM
  2. Best way to achieve this
    By cloudy in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-06-2006, 07:23 PM
  3. Replies: 3
    Last Post: 09-11-2005, 10:13 AM
  4. The more intelligent risks you take in life, the more you'll achieve
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-20-2003, 05:23 PM
  5. Regarding Careers - need advice
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-05-2002, 04:59 AM