Results 1 to 6 of 6

Thread: C programming Help Needed

  1. #1

    C programming Help Needed

    Hi

    I have the following code below but it doesn't seem to work . Anyone here who knows C can look at it and help me locate the errors ?

    thanks



    #include "stdio.h"
    #include "conio.h"

    #define MAX_TEAM 12

    // Number of teams
    int nteams = 0;
    // Team names (up to MAX_TEAM teams). Name is up to 24 chars (+1 for zero).
    char teams[MAX_TEAM][25];

    // Team record in league table
    typedef struct TEAM_RECORD
    {
    // Number of matches
    int played;
    int won;
    int drawn;
    int lost;
    // Goals
    int goals_for;
    int goals_against;
    // Total points
    int total;
    };

    // Array for league table
    struct TEAM_RECORD table[MAX_TEAM];

    void inputTeams()
    {
    char c;
    if(nteams==MAX_TEAM)
    {
    printf("All %d teams are entered already.\n\r", MAX_TEAM);
    return;
    }
    do
    {
    printf("Enter team name: ");
    gets(&teams[nteams]);
    nteams++;
    if(nteams==MAX_TEAM)
    break;
    printf("Would you like to enter another team? (y/n) ");
    c = getche();
    printf("\n\r");
    }while(c=='y' || c=='Y');
    }

    void displayTable()
    {
    int i;
    printf(" Team P W D L F A T\n\r");
    for(i=0; i < nteams; i++)
    printf("%24s %2d %2d %2d %2d %2d %2d %2d\n\r", teams[i],
    table[i].played, table[i].won, table[i].drawn, table[i].lost,
    table[i].goals_for, table[i].goals_against, table[i].total);
    printf("\n\rPress any key to return to Main Menu ");
    getch();
    printf("\n\r");
    }

    void inputMatches()
    {
    int i;
    int team1, team2, score1, score2;
    int c;
    if(nteams==0)
    {
    printf("Please enter teams first\n\r");
    return;
    }
    do
    {
    // print all teams
    printf("\n\rTeams:\n\r");
    for(i=0; i < nteams; i++)
    printf("%2d %s\n\r", i+1, teams[i]);
    printf("Enter index of first team in match: ");
    scanf("%d", &team1);
    team1--; // to count from zero
    printf("Enter goals for %s: ", teams[team1]);
    scanf("%d", &score1);
    printf("Enter index of second team in match: ");
    scanf("%d", &team2);
    team2--; // to count from zero
    printf("Enter goals for %s: ", teams[team2]);
    scanf("%d", &score2);
    table[team1].played++;
    table[team2].played++;
    if(score1 > score2)
    {
    table[team1].won++;
    table[team2].lost++;
    }
    else if(score1 < score2)
    {
    table[team1].lost++;
    table[team2].won++;
    }
    else // draw
    {
    table[team1].drawn++;
    table[team2].drawn++;
    }
    table[team1].goals_for += score1;
    table[team1].goals_against += score2;
    table[team2].goals_for += score2;
    table[team2].goals_against += score1;
    table[team1].total = table[team1].won*3 + table[team1].drawn;
    table[team2].total = table[team2].won*3 + table[team2].drawn;

    printf("Would you like to enter another match results? (y/n) ");
    c = getche();
    printf("\n\r");
    }while(c=='y' || c=='Y');
    }

    int main(int argc, char* argv[])
    {
    int exit = 0; // exit from main loop flag
    char c;
    // main loop
    do
    {
    printf("\n\r==========");
    printf("\n\rMain Menu:\n\r");
    printf("a. Entry of teams\n\r");
    printf("b. Display current table\n\r");
    printf("c. Enter match details\n\r");
    printf("d. Exit system\n\r");
    printf("Your choice: ");
    c = getche();
    printf("\n\r\n\r");
    switch(c)
    {
    case 'a':
    case 'A':
    inputTeams();
    break;
    case 'b':
    case 'B':
    displayTable();
    break;
    case 'c':
    case 'C':
    inputMatches();
    break;
    case 'd':
    case 'D':
    // set exit flag
    exit = 1;
    break;
    default:
    printf("Please select a-d.\n\r");
    }
    } while(exit!=1);
    return 0;
    }

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    149
    What are you using to compile your program with? And have you tried using #include <stdio.h> and #include <conio.h>, instead of using the "".

  3. #3
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Ok, jeebus where to start...

    Its been a while since I messed with C so may be a little off, but for starters:

    1) Next time you post code, use so you don't lose your indentation (or [quote]), and if that is really how your program looks, shame on you.
    2) Line: typedef struct TEAM_RECORD . Not sure what you are trying to do here, but if memory serves, you don't typedef structs, at least not with typedef.
    3) char teams[MAX_TEAM][25]; Unless you are strictly checking input, this is a bad idea, you could get an overflow situation here easily, especially since you are doing this: gets( teams[nteams]); without checking length.
    4) gets( &teams[nteams]); Since this is a multi-dimensional array, supplying only one index will result in the returned value being a pointer to the row, therefore, no need to use the reference symbol.

    Anyway, after dropping the typedef, and the other stuff (and I didn't have conio.h for whatever reason), and changing "stdio.h" to <stdio.h> (for pathing reasons to system includes), I also got an error that basically refers to a { } ( ) or whatever not matching up and given how hard the code is to follow without indentation I moved on.

    It would also be helpful next time if you posted what your compile errors were.
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  4. #4
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    We used typedef for structs in my class last semester for C. I think his syntax may be wrong though, we used it like this:
    Code:
    typedef struct nodeType
    {
       //struct info in here
    }node;
    Then you could declare a struct nodeType just by saying node. Peace.

    The name you are typedeffing to should be the last thing before the semicolon. Everything else is what is being typedeffed, besides the keyword typedef of course.

  5. #5
    Senior Member
    Join Date
    Dec 2003
    Location
    Pacific Northwest
    Posts
    1,675
    Yep,

    should be: "typedef type name" as above.

    typedef struct nodeType
    {
    //struct info in here
    }node;

    Can use this to assign one struct to another if needed.

    {
    struct {
    int a;
    int b;
    } x, y;
    x.a = 10;
    y = x; /* assign one struct to another */
    printf(ā€œ%dā€, y.a);
    return 0;
    }


    Edit: arrrh I'm too sleepy for this....
    Connection refused, try again later.

  6. #6
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    It's a bit like doing "astruct->avar;" rather than "*astruct.avar;" when using pointers to structs. It saves on the typing by not having to type "struct node anode;" instead of "node anode;"

    ac

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •