How to make data from a file work as a menu in "C" ?

Hi again.. I know its sounds crazy, but I explain what I meant in a few words.
So let's say the way I want this menu to be is more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <conio.h>
#include <windows.h>

#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_ENTER 13

#define ARRAYSIZE(sel) (sizeof(sel) / sizeof(sel[0]))
#define SELECT_ENDU 5

void changeUser()
{
    int select = 0;
    int x;

    selectorUser(select);

    while((x = _getch()))
    {
        if(x == KEY_UP)
        {
            select -= 2;
            if(select < 0)
                select = 0;
            selectorUser(select);
        }
        else if(x == KEY_DOWN)
        {
            select += 2;
            if(select > SELECT_ENDU)
                select = SELECT_ENDU;
            selectorUser(select);
        }
        else if(x == KEY_ENTER)
        {
            if(select <= 1)
            {
                newUser();
                selectorUser(select);
            }
            else if(select <= 2)
            {
                selectUser();
                selectorUser(select);
            }
            else if(select <= 4)
            {
                printf("\n\n\n\n\n\n\t\t\t        Quit Game");
                Sleep(1500);
                system("cls");
                exit(0);
            }
        }
    }
}

void selectorUser(unsigned int select)
{
    const char *selection[] =
    {
        "\n\n\n\n\n\t\t\t      >  NEW USER",
        "\n\n\n\n\n\t\t\t         new user",
        "\n\t\t\t      >  SELECT USER",
        "\n\t\t\t         select user",
        "\n\t\t\t      >  QUIT GAME",
        "\n\t\t\t         quit game",
    };

    unsigned int i;

    system("cls");
    printf("\n\n\n\n\t\t\t ****** My GaMe ******\n");

    for(i = 0; i < ARRAYSIZE(selection); i += 2)
    {
        if(i == select)
            printf("%s\n", selection[i]);
        else
            printf("%s\n", selection[i + 1]);
    }
}


Okay now from the code I showed you if I choose SELECT USER, I want this to be another menu and to act like the one I showed you before, only that this time the elements of the SELECT USER to be from a file.txt, where this file contains only names like this:

e.g.
Alex
George
Michael
Burton
Last edited on
I know how to print the content of a file.txt and make user to choose from a line by typing the line number, but this isn't the way I want this menu to act.
you need to load the file contents into some sort of container, and display that for the user to select from, and once they make their selection, you proceed with the chosen one.

eg a vector of string, they pick George, and you process that via vector_variable[1] passed forward into the processing function.

its similar, but selection variable needs to be populated from the file, and the container can't be const-char strings it has to be variable so you can set it from the file contents. But the same basic code, with those changes.

Edit: missed the "C" note.
to do it in C, you can use a 2-d array of char where I said vector of string.
char example[100][20]; //100 strings, max 20 length each
(you can also use dynamic arrays with pointers if you want, but its a lot of code to save a few bytes of memory -- I would use arrays if you KNOW the approximate max size of your text file, if not, you need to go to pointers). Track the # of strings you got from the file so you know how much of your container you used (if array approach) (you also still need the size with pointers, or some other way to know when to stop, eg a linked list of 'strings').
Last edited on
Okay .. I understand. Let's say I know the approximate max size of this text file, and it won't be that large as there won't be 100 players to write their name on the file, I'd like to do it this in both way, I mean I would like to learn it either ways.

Okay for now I ty jonnin, I'll go and figure out how to do this as you mentioned..
Last edited on
to learn it both ways may mean a side-project.
Don't try to work a dynamic thing into an existing complicated program right off, instead, make a new little project and play with just that one feature. Say you wanted to do it with a linked list of char pointers / c-strings. That is a bit of an undertaking in and of itself, and you would learn it easier and better doing that and nothing else in a new program. Then see if you can re-use what you did in the bigger program once you get it working.

C lacks the data structures that C++ provides, so a study in data structures is critical to coding in C...
Last edited on
Ohh okay, I understand what are you saying, ofc. I'll create another project and play with that, but what if you said about C, it's a bit discouraging, even that, I wont stop doing it, cause I like what I'm doing, and after a bit of experience, I'll make the difference how to do this is C++
Nothing I said should discourage you. C is a great language, widely used and powerful.
It just does not have as much built in algorithms and data structures and toys in its built in language header libraries. All these things exist out in the wild in third party libraries, but knowing how to do it yourself will make you a better coder.
Topic archived. No new replies allowed.