-2
#include <stdio.h>

int main(void) {
    int choice;

    do {
        choice = getUserChoice();
        switch (choice) {
            case 1:
                gameResults();
                break;
            case 2:
                printf("game results:\n ");
                break;
            case 3:
                printf("selected choice is 3\n");
                break;
            case 4:
                printf("selected choice is 4\n");
                break;
            case 5:
                //this case is to keep the default from triggering when selecting 5.
                break;
            default:
                printf("please select a valid number 1-5.\n");
                system("pause");
        }
    } while (choice != 5);
}

int getUserChoice() {
    int x = 0;

    printf("[1]Enter game results.\n");
    printf("[2]Current record (number of wins, losse, and ties)\n");
    printf("[3]Display ALL results from all games won.\n");
    printf("[4]Display ALL results ordered from low to high.\n");
    printf("[5]Quit.\n");

    scanf_s("%d", &x);
    return x;
}//end getUserChoice

int gameResults() {
    int gameInput[1][2];
    //counter variables for loop.

    int i, j;
    for (i = 0; i < 1; i++) {
        for (j = 0; j < 2; j++) {
            printf("please enter a value for gameInput[%d][%d]: ", i, j);
            scanf_s("%d", &gameInput[i][j]);

            return gameInput[i][j];
        }
    }
}

void prinGameResults() {
    gameResults();

    int wins, ties, losses = 0;
    if()

}

So essentially I'm working on this assignment that is asking me to create a 2D array, have the user select stuff from a menu, put in 2 values for 2 team scores (one being "your" team and the other one being the opponents) I've created the array and now I'm looking to display this array when the user calls the 2nd switch case, and display numbers of wins, ties, etc etc.. the only problem is I have no idea how to call this array into one function from another, perhaps I'm doing this the in a wrong way, is there some easier way of going about this array?

Oka
  • 23,367
  • 6
  • 42
  • 53
  • `prinGameResults` (`printGameResults`?) is incomplete in your provided code. – Oka Sep 08 '21 at 06:10
  • @Oka Yeah that was my next function where I am going to call the above function `gameResults` – Hagen Farrell Sep 08 '21 at 06:17
  • 1
    You should post a proper [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), one that compiles (or fails to compile because of an error you don't understand) if you want others to be able to easily help. Note that you'll need function prototypes at the top of your source file, if you want to structure it in the order you've posted the functions here. – Oka Sep 08 '21 at 06:19
  • To let two independent functions operate on the same data structure, either the data structure is globally visible to both functions, or the caller passes (by reference) the same data structure to each function. – jxh Sep 08 '21 at 06:26
  • Have you run your program? As far as I can see, it is unable to input data, so it's too early to care about printing them (apart from the problem with incomplete and syntactically invalid `prinGameResults()` which prevents it from compiling...) – CiaPan Sep 08 '21 at 06:27
  • Aside: avoid using [empty argument lists](https://stackoverflow.com/questions/41803937/func-vs-funcvoid-in-c99) (e.g., `int getUserChoice()` -> `int getUserChoice(void)`). – Oka Sep 08 '21 at 06:28
  • @Oka There's no problem with storing a second pair of data, as the loop `for (i = 0; i < 1; i++)` does not go past the first row. – CiaPan Sep 08 '21 at 06:28
  • @Oka ah I see, I had tried doing that before, passing the `gameResults` to function `prinGameResults` with the array `gameInput[1][2]` inside of it but for some reason it tells me `gameInput` is undefined. – Hagen Farrell Sep 08 '21 at 06:32
  • @CiaPan Oh, I misunderstood the part about 2 values (thought they meant 2 values *per* team). The 2D array is a bit superfluous then. The function also returns instantly on the first iteration. – Oka Sep 08 '21 at 06:35
  • @Oka yeah if you remove the return value `return gameInput[1][2];` it'll stop it from returning instantly like that, I was testing that out for when I pass the function to be printed out, but It was more of a shot in the dark since I'm a new programmer I was just trying something to see if it would work (which it didnt obviously) – Hagen Farrell Sep 08 '21 at 06:40
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 13 '21 at 19:37

1 Answers1

0

You'll want to define your array inside main so that it can be passed as an argument (alongside its dimensions) to each function.

#include <stdio.h>
#include <stdlib.h>

int getUserChoice(void);
void getGameResults(size_t n, size_t m, int res[n][m]);
void printGameResults(size_t n, size_t m, int res[n][m]);

#define ROWS 1
#define COLS 2

int main(void) {
    int results[ROWS][COLS] = { 0 };
    int choice;

    while ((choice = getUserChoice()) != 5)
        switch (choice) {
            case 1:
                getGameResults(ROWS, COLS, results);
                break;
            case 2:
                printGameResults(ROWS, COLS, results);
                break;
            case 3:
                puts("Selected (3)");
                break;
            case 4:
                puts("Selected (4)");
                break;
            default:
                puts("Select a valid option (1-5)");
                break;
        }
}

int getUserChoice(void) {
    int x = 0;

    puts("[1]Input results.");
    puts("[2]Display results.");
    puts("[3]--");
    puts("[4]--");
    puts("[5]Quit.");

    if (scanf("%d", &x) != 1) {
        fprintf(stderr, "Failed to read choice.\n");
        exit(EXIT_FAILURE);
    }

    return x;
}

void getGameResults(size_t n, size_t m, int res[n][m]) {
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < m; j++) {
            printf("Enter a value for [%zu][%zu]: ", i, j);

            if (scanf("%d", &res[i][j]) != 1) {
                fprintf(stderr, "Failed to read result.\n");
                exit(EXIT_FAILURE);
            }
        }
    }
}

void printGameResults(size_t n, size_t m, int res[n][m]) {
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < m; j++) {
            printf("Value for [%zu][%zu]: %d\n", i, j, res[i][j]);
        }
    }
}

And for compilers that don't support variable-length arrays, you must be more rigid in your approach. There are a few ways to do this, here is one example:

#include <stdio.h>
#include <stdlib.h>

#define ROWS 1
#define COLS 2

int getUserChoice(void);
void getGameResults(int res[ROWS][COLS]);
void printGameResults(int res[ROWS][COLS]);

int main(void) {
    int results[ROWS][COLS] = { 0 };
    int choice;

    while ((choice = getUserChoice()) != 5)
        switch (choice) {
            case 1:
                getGameResults(results);
                break;
            case 2:
                printGameResults(results);
                break;
            case 3:
                puts("Selected (3)");
                break;
            case 4:
                puts("Selected (4)");
                break;
            default:
                puts("Select a valid option (1-5)");
                break;
        }
}

int getUserChoice(void) {
    int x = 0;

    puts("[1]Input results.");
    puts("[2]Display results.");
    puts("[3]--");
    puts("[4]--");
    puts("[5]Quit.");

    if (scanf("%d", &x) != 1) {
        fprintf(stderr, "Failed to read choice.\n");
        exit(EXIT_FAILURE);
    }

    return x;
}

void getGameResults(int res[ROWS][COLS]) {
    for (size_t i = 0; i < ROWS; i++) {
        for (size_t j = 0; j < COLS; j++) {
            printf("Enter a value for [%zu][%zu]: ", i, j);

            if (scanf("%d", &res[i][j]) != 1) {
                fprintf(stderr, "Failed to read result.\n");
                exit(EXIT_FAILURE);
            }
        }
    }
}

void printGameResults(int res[ROWS][COLS]) {
    for (size_t i = 0; i < ROWS; i++) {
        for (size_t j = 0; j < COLS; j++) {
            printf("Value for [%zu][%zu]: %d\n", i, j, res[i][j]);
        }
    }
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • Only problem I had when I ran that code was I got an error saying "a parameter is not allowed" in `void getGameResults(size_t n, size_t m, int[n][m]); void printGameResults(size_t n, size_t m, int[m][m]);` also this is a school assignment so my professor forbids us to use global variables. – Hagen Farrell Sep 08 '21 at 16:17
  • @HagenFarrell Those are function prototypes, so that main knows about functions that haven't been defined yet (because they appear later in the source file). Variable length arrays are a feature of C99. I suppose you are on Windows using the ancient C compiler in MSVC, which doesn't support C99. Additionally, there are no global variables in the first example; `ROWS` and `COLS` are *macros* that expand to `1` and `2`, respectively. I'll post a different example that should work with your compiler. – Oka Sep 08 '21 at 19:20
  • Additional reading: [here](https://stackoverflow.com/questions/11428526/passing-a-matrix-in-a-function-c) and [here](https://stackoverflow.com/questions/546860/passing-arrays-and-matrices-to-functions-as-pointers-and-pointers-to-pointers-in). – Oka Sep 08 '21 at 19:20
  • thanks for the additional reading I'll study up on that now, also yes my OS is windows 10 64 bit (idk if that matters the bits) while using Microsoft visual studio code 2019 – Hagen Farrell Sep 08 '21 at 22:06