Thread: what's the best way to achieve this

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    what's the best way to achieve this

    Hello,

    I have read the getting started guide.
    My idea is now to make a programm which do this.

    A user can input a game. Home team - Away team and the end -score of the game.
    This will be stored in a database.

    When the user wants the programm makes a ranking of the teams.

    I think the game is stored in a text-file.

    Now my question :

    It is possible to read the database one for one and puts the data in a struct.
    And can I check if a team is already present in the struct.

    Roelof

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming you know how the database was written to file in the first place, then of course you can read it from the file. And you're going to need more than one struct, but a container of structs (since by definition there must be at least two teams). You can search through a container looking for a given team by walking through the container (although naturally some containers are easier to search through than others).

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Dunno. Try it on your own and when you have a specific problem with it post it here and we can help. Until then you won't get much help.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    No problem.

    Here's the code I have till now :
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    char home_team, away_team, game ;
    int score_home , score_away;
    map struct database {
    String Team ;
    int played_games, game_points, points_made, points_against
    }
    
    int Menu(){
    int ret;
    cout<<"1) Input game.: "<<endl;
    <<"2) Making the rank.: "<<endl;
    <<"3) Quit.: ";
    cin>>ret;
    return ret;
    }
    
    Input_games() {
    cout << "Home team :"<<end1 ;
    cin.getline (home_team, 30, '/n');
    cout << "Away team :"<<end1;
    cin.getline (away_team, 30, '/n');
    cout << "Score home team :"<<end1;
    cin.get (score_home) ;
    cout << "Score Away team :"<<end1;
    cin.get (score_away)
    ofstream a_file ( "example.txt" );
    game= home_team + away_team + score_home + score_away
    a_file<< game;
    a_file.close();
    }
    
    Ranking() {
    char str[80];
    
    ifstream b_file ( "example.txt" );
    b_file>> str;
    
    
    }
    
    
    void main(){ // or int main() {
    int rply;
    rply=Menu();
    switch (rply){
    case 1:
      Input_games();
    break;
    case 2:
       Ranking();
    break;
    case 3:
    break;
    }
    // return 0; if you have int main()
    }

    The question is how do i get the right values of str into the struct var ?

    Roelof

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Have you actually tried compiling that? I shuddered after looking at the first dozen or so lines.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    No,

    Why try to compile a not complete code ?

    Roelof

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by roelof View Post
    No,

    Why try to compile a not complete code ?

    Roelof
    It is fairly common (and best) practice to write a single component of the code, compile and test it, and then move on to the rest of the code.

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

    Then i start over and build first the menu and then try to debug it.

    Roelof

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

    I have tested this part.
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int Menu(){
    int ret;
    cout<<"1) Input game.: "<<endl;
    cout<<"2) Making the rank.: "<<endl;
    cout<<"3) Quit.: "<<endl;
    cin>>ret;
    return ret;
    }
    
    int main(){ // or int main() {
    int rply;
    rply=Menu();
    switch (rply){
    case 1:
      cout<<"U hebt gekozen voor invoeren uitslagen";
    break;
    case 2:
       cout<<"U hebt gekozen voor het maken van standen";
    break;
    cout<<"U hebt gekozen voor stoppen";
    break;
    }
    // return 0; if you have int main()
    }
    Can someone confirm that this is a good source.

    Roelof

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

    Can someone confirm that this is a good source.

    Roelof
    What happens if I enter something other than 1, 2, or 3?
    goto( comeFrom() );

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    230
    Youre right.
    The programm ended.
    So I changed it to :
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int Menu(){
    int ret;
    cout<<"1) Input game.: "<<endl;
    cout<<"2) Making the rank.: "<<endl;
    cout<<"3) Quit.: "<<endl;
    cin>>ret;
    return ret;
    }
    
    int main(){ // or int main() {
    int rply;
    rply=Menu();
    switch (rply){
    case 1:
      cout<<"U hebt gekozen voor invoeren uitslagen";
    break;
    case 2:
       cout<<"U hebt gekozen voor het maken van standen";
    break;
    case 3:
       cout<<"U hebt gekozen voor stoppen";
    break;
    default:
       cout<<"U hebt een verkeerde keuze gemaakt";
    }
    // return 0; if you have int main()
    }
    Now I have to find out how I allways come back to the menu till someone chooses 3.

    Roelof

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

    I could use a while wend but then if the user chooses three i have to go out the loop but I have to use a case 3 also. If I don't use case 3 I can't have a good control if the user chooses somewhat else then 1,2,3.

    I can also use the menu function again when a user chooses something else then 1,2,3 but I think that I use 2 menu's. The old menu and the new menu.

    Is this right or am i thinking wrong.

    Roelof

  13. #13
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    An int is not a good choice for user input at the prompt. If I enter...

    "Hello world! I'm trying to crash your program!"

    ...what data-type do you think you would need?
    goto( comeFrom() );

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello StainedBlue.

    Then Im thinking about a string or char.

    Roelof

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

    Then Im thinking about a string or char.

    Roelof
    right. then you can use:

    Code:
    using namespace std;
    string input;
    getline(cin, input);

    which is way better than an int, or a char for handling user input.
    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