general question

This is a discussion on general question within the C Programming forums, part of the General Programming Boards category; hi i have a question. in my structure i have "int scored". i will ONLY be using this so that ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    Post general question

    hi i have a question. in my structure i have "int scored". i will ONLY be using this so that the user can input the scores between the two teams after a football match. so the thing is is that i dont want the "scored" to constantly increase after every game. for example. chelsea 2 arsenal 3 and then chelsea 1 liverpool 2. see that? chelsea scored 2 goals in the first game and then 1 against liverpool. but i dont want those 2 goals to be transfered to the next game so that chelsea have 2+1 goals to make 3 which in turn would mean chelsea beat liverpool 3 goals to 2. so is there a way of flushing out the score or setting it back to zero after every time the user enters the scores of each game? here is my code to help you understand more. the part in this code i am referring to is the last "for" loop.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct bones
    {
        char name[20];
        int played;
        int scored;
        int won;
        int drawn;
        int lost;
        int points;
    
    
    };
    
    
    
    
    struct bones team[4];
    
    
    
    
    int main()
    {
        int i;
    
    
        for(i=0;i<4;i++)
        {
            team[i].played = 0;
            team[i].won = 0;
            team[i].scored = 0;
            team[i].lost = 0;
            team[i].drawn = 0;
            team[i].points = 0;
        }
    
    
       
        for(i=0;i<4;i++)
        {
            printf("enter each team name\n");
            fgets(team[i].name, 20, stdin);
        }
       
       
        for(i=1;i<4;i++)
        {
            printf("%s vs %s\n",team[0].name,team[i].name);
            printf("%s's score:\n",team[0].name);
            scanf("%d",team[0].scored);
            printf("%s's score:\n",team[i].name);
            scanf("%d",team[i].scored);
        }
        
    return(0);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It sounds like what you want is an array of scored, with member scored[i] representing the number of goals scored against team i.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    no everything is ok there at the moment. when someone comes along and goes ok chelsea beat liverpool 3 goals to 2, well you need something to read in those scores and then i will use an if statement to test whether the first team scored more, less or the same amount of goals and then in turn they will be assigned 3 points for a win, 1 point for a draw and no points for a loss. also if one team scores more than the other then their win is increased by 1. if they draw their draw increases by 1 and the same goes for a loss. but i hav not got to that yet because i want to overcome this problem first

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well then you need to store the number of goals scored and received in two parallel arrays, although this may not be an optimum solution for your problem, because we don't even know what you actually want to do. If you just want to record losers and winners, then it's a better idea to have a struct that models a game, not a team.

    You have to clearly state what the data that you are modelling in your struct is supposed to be used for, as different structs may be more or less appropriate for different problems.

    This awfully sounds like a typical case of jumping on the keyboard before thinking about what I want to type.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    Quote Originally Posted by claudiu View Post
    Well then you need to store the number of goals scored and received in two parallel arrays, although this may not be an optimum solution for your problem, because we don't even know what you actually want to do. If you just want to record losers and winners, then it's a better idea to have a struct that models a game, not a team.

    You have to clearly state what the data that you are modelling in your struct is supposed to be used for, as different structs may be more or less appropriate for different problems.

    This awfully sounds like a typical case of jumping on the keyboard before thinking about what I want to type.

    its actually not a typical case of jumping on the keyboard.. i got it working perfectly. i guess i didnt need to do what i thought i needed to do .thanks anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General How to go about this question?
    By Chems in forum C Programming
    Replies: 8
    Last Post: 12-06-2010, 11:10 AM
  2. General API Question
    By legion050 in forum C Programming
    Replies: 2
    Last Post: 11-23-2010, 05:00 AM
  3. general question
    By a.mlw.walker in forum C Programming
    Replies: 25
    Last Post: 04-04-2009, 09:32 AM
  4. General Question
    By obaid in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-22-2007, 05:39 AM
  5. General C++ Question
    By IncarnateRW in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21