Thread: Storing game scores for 2 teams in a 2D array

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    8

    Storing game scores for 2 teams in a 2D array

    Let me start by saying I am new to this forum and new to C programming. I am struggling with the concept of storing 2 teams scores in a 2 d array from a menu based system. Yes, this is a homework assignment, and I am not asking anyone to write the assignment for me. I just would like to understand why my code is not placing values as I would expect them to. I am here to learn, I am taking the project one piece at a time. The menu functions and does work, the next step was placing elements in the array. This does not work sadly. Thank you in advance.

    Code:
    /
    //  main.c
    //  game_scores
    //
    //  Created by on 1/24/19.
    //  Copyright © 2019 game_scores. All rights reserved.
    //
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define score 100
    #define teams 2
    
    
    // declare functions
    
    
    void display_menu(void);
    void game_result(void);
    void list_scores(void);
    void games_won(void);
    void scores_sort(void);
    
    
    // declare variables
    
    
    int game[score] [teams] , j = 0, i = 0;
    char choice;
    
    
    int main()
    
    
    // call menu
    {
        display_menu();
        
        return 0;
    }
    
    
    // display menu options
    
    
    void display_menu(void)
    {
        
        
        do
        {
            printf("\n\n************************* \n");
            printf("**      MAIN MENU     *** \n");
            printf("************************* \n\n");
            printf("[A]Enter game result \n");
            printf("[B]Current Record(# of wins and # of losses and # of ties \n");
            printf("[C]Display ALL results from all games WON \n");
            printf("[D]Display ALL results ordered by oponent score from low to high. \n");
            printf("[E]Quit the program \n");
            printf("Enter an option:");
            scanf(" %c",&choice);
            
            // set menu options
            switch(choice)
            {
                case 'A': game_result();
                    break;
                case 'B': list_scores();
                    break;
                case 'C': games_won();
                    break;
                case 'D': scores_sort();
                    break;
                case 'E': printf("Quitting program!\n");
                    exit(0);
                    break;
                default: printf("Invalid choice!\n");
                    break;
            }
            
        } while (choice != 'E');
        
    }
    
    
    // begin function items
    //*********************
    
    
    // loop through game score values
    
    
    void game_result(void)
    {
        
        for(i=0; i<score; i++)
            for(j=0;j<teams;j++)
                game [i][j] = 0;
                printf("Enter game scores:");
                scanf("%d", &game[i][j]);
        
        
        return;
    }
    
    
    
    
    // list game scores in array
    
    
    void list_scores(void)
    {
        
        return;
    }
    
    
    // sort the by games won
    
    
    void games_won(void)
    {
        
        return;
    }
    
    
    // sort score values
    
    
    void scores_sort(void)
    {
       
        return;
    }
    Last edited by jlucey; 01-26-2019 at 07:21 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
        for(i=0; i<score; i++)
            for(j=0;j<teams;j++)
                game [i][j] = 0;
                printf("Enter game scores:");
                scanf("%d", &game[i][j]);
    Well C isn't Python, where indentation alone specifies block scope.
    In C, you have to use braces to make your intention explicit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    8
    Funny you should reference Python, that is the other class I am taking.

    So then it should look something like this? After testing this and using my debugger I can see it is populating the array now, but it continues to ask for input rather than just populating one row of values. Instead, it populates a row and then moves to the next row and populates and so on. Would I move my return up to the inner nest?

    Code:
    for(i=0; i<score; i++){
        for(j=0;j<teams;j++){
            game [i][j] = 0;
            printf("Enter game scores:");
            scanf("%d", &game[i][j]);
           }
        }
    

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    8
    Please accept my thanks, your reply got me thinking and I believe I have found a better way to obtain the desired result. I decided to ask the user for the number of games and teams. and pass those variables to define my loops and my array. This way the expectation is set, the user will know how many values they will need to enter.

    Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to calculate my game from multiple scores
    By mm1990 in forum C Programming
    Replies: 12
    Last Post: 07-03-2015, 01:56 PM
  2. array for teams
    By Crossfire in forum C Programming
    Replies: 7
    Last Post: 04-14-2013, 01:30 PM
  3. Storing 5 Game Scores- Which STL Container?
    By bengreenwood in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2009, 09:43 PM
  4. Adding Scores from a string array
    By MB1 in forum C++ Programming
    Replies: 6
    Last Post: 11-11-2005, 07:27 AM
  5. How do i put scores in my game?
    By Anonymous in forum Game Programming
    Replies: 2
    Last Post: 09-02-2002, 04:32 PM

Tags for this Thread