trying to convert location name from numbers to words

Im creating a text-based game and in the game, i have several locations where the user can go. The problem is that in the beginning, I was using numbers as the locations, but after coming up with the names, I don't know how I'm supposed to use the names of the location instead of numbers.

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
  #include <stdio.h>

int main() {

    // Define the starting location
    int location = 1;
    int next_location;

    // Loop until the user reaches the winning location
    while (location != 5) {

        // Display the current location and possible paths
        printf("You are at location %d\n", location);

        if (location == 1) {
            printf("You are in a forest. There are paths to the north (2) and east (3).\n");
        }
        else if (location == 2) {
            printf("You are at a river. There is a bridge to the east (3) and a path to the south (1).\n");
        }
        else if (location == 3) {
            printf("You are at the entrance of a dark cave. There is a path to the west (2) and a path leading further into the cave (4).\n");
        }
        else if (location == 4) {
            printf("You are inside the dark cave. There is a path leading out of the cave (3) and a path leading deeper into the cave (5).\n");
        }

        // Get user input for the next location
        printf("Where would you like to go? Enter the number of your chosen path: ");
        scanf_s("%d", &next_location);

        // Verify user input
        if (location == 1) {
            if (next_location == 2 || next_location == 3) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
        else if (location == 2) {
            if (next_location == 1 || next_location == 3) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
        else if (location == 3) {
            if (next_location == 2 || next_location == 4) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
        else if (location == 4) {
            if (next_location == 3 || next_location == 5) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
    }

    // Congratulate the user 
    printf("Congratulations! You have reached the winning location.\n");

    return 0;
}
roughly, just make a lookup table. If the numbers are not simple (like 0-20) then some sort of map (eg <unordered map> ) will do the dirty work for you. If its just 0-N, a vector or even C array gets it done.
string names[] {"loc one", "loc 2", ...};
...
cout << names[location number];
how would i do that? im fairly new
seriously, just like I said above:
string names[] { all your names}
where names[number] converts the number back to a string. Very simple.
Try it, and if you can't get it working, we can do it for you but I almost did... just fill in a few blanks, research 'arrays' if you need to in the tutorial...

if this is C, you need char array strings instead of c++ string type. Its hard to tell from your code if its C like C++ or C.
Last edited on
The code is in C++. i just added a string of the names as you mentioned.Is this correct?

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
#include <stdio.h>

int main() {
    // Define the starting location
    int location = 1;
    int next_location;

    // String array to hold location names
    const char* names[] = { "Forest","River","Cave Entrance","Inside Cave","Winning Location"};

    // Loop until the user reaches the winning location
    while (location != 5) {
        // Display the current location and possible paths
        printf("You are at location %s\n", names[location - 1]);
        if (location == 1) {
            printf("You are in a forest. There are paths to the north (2) and east (3).\n");
        }
        else if (location == 2) {
            printf("You are at a river. There is a bridge to the east (3) and a path to the south (1).\n");
        }
        else if (location == 3) {
            printf("You are at the entrance of a dark cave. There is a path to the west (2) and a path leading further into the cave (4).\n");
        }
        else if (location == 4) {
            printf("You are inside the dark cave. There is a path leading out of the cave (3) and a path leading deeper into the cave (5).\n");
        }

        // Get user input for the next location
        printf("Where would you like to go? Enter the number of your chosen path: ");
        scanf_s("%d", &next_location);

        // Verify user input
        if (location == 1) {
            if (next_location == 2 || next_location == 3) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
        else if (location == 2) {
            if (next_location == 1 || next_location == 3) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
        else if (location == 3) {
            if (next_location == 2 || next_location == 4) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
        else if (location == 4) {
            if (next_location == 3 || next_location == 5) {
                location = next_location;
            }
            else {
                printf("Invalid input. Please enter the number of a valid path.\n");
            }
        }
    }

    // Congratulate the user 
    printf("Congratulations! You have reached the winning location.\n");

    return 0;
}
Last edited on
The code is in C++.


No, you are most likely writing C code.

The telltale signs are:

#include <stdio.h>
The use of printf and scanf
The pointer to char array

While a C++ program can include C code, you haven't written any specific C++ code.

If one is going to use scanf, then make sure to test the value that is returned; to see if it worked :+)

I realise it can take awhile to transition from C to C++; having done it myself a long time ago. Do try to make use of std::vector and std::string as a starting point.
line 14 looks correct and like I said to do, yes. You can use this idea from here as needed, I think, you seem to have it working.

ideally as a long term idea/goal .. your could would look like (roughly, this is pseudo code) this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

struct location
{
   char name[50];
   int from_here[10]; //eg set 0 means invalid, anything else is ok destination
      
};
location all[10]; 
... set up each name and destinations, preferably from a text file so you can easily change the game without changing the game ENGINE. 
while(not dead or not winning)
{
   print you are at location  all[position].name
   print you can go to
   for(10 destinations, if not zero, print it with words, eg 2 and 3)
    print all[position.from_here[0].name] so if you are at 0 and can go to 1 it would say "you can go to 1) one's name" 
}

anyway, the idea is to get rid of the special check each position logic and do the SAME THING on every location, varied only by the data FOR that location.
this moves the game logic out of the game engine; any valid set of locations and destinations will work whether its a fantasy setting or walking through your mom's house or going to each planet in our solar system!

does this make sense? It may be throwing a lot at you, but its not super difficult to get from where you are to this if you want to try it. You are about 75% of the way, but it would mean some re-do.
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main() {
	constexpr const char* names[5][2] { { "Forest", "You are in a forest.There are paths to the north(2) and east(3)"},
		{"River", "You are at a river. There is a bridge to the east (3) and a path to the south (1)"},
		{"Cave Entrance", "You are at the entrance of a dark cave. There is a path to the west (2) and a path leading further into the cave (4)" },
		{"Inside Cave", "You are inside the dark cave.There is a path leading out of the cave(3) and a path leading deeper into the cave(5)"},
		{ "Winning Location", "Congratulations! You have reached the winning location." } };

	constexpr unsigned valid[4][2] { {1, 2}, {0, 2}, {1, 3}, {2, 4} };

	for (unsigned location {}, next_location {}; (std::cout << "You are at location: " << names[location][0] << '\n' << names[location][1] << ".\n") && location != 4; location = next_location)
		do {
			std::cout << "Where would you like to go: ";
			std::cin >> next_location;
			--next_location;
		} while ((next_location > 4) || ((valid[location][0] != next_location) && (valid[location][1] != next_location)) && (std::cout << "Invalid location\n"));
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
if (location == 1) {
            printf("You are in a forest. There are paths to the north (2) and east (3).\n");
        }
        else if (location == 2) {
            printf("You are at a river. There is a bridge to the east (3) and a path to the south (1).\n");
        }
        else if (location == 3) {
            printf("You are at the entrance of a dark cave. There is a path to the west (2) and a path leading further into the cave (4).\n");
        }
        else if (location == 4) {
            printf("You are inside the dark cave. There is a path leading out of the cave (3) and a path leading deeper into the cave (5).\n");
        }


just wanna give a lil advice, it's better to use switch and case

e.g.:
1
2
3
4
5
6
7
8
9
10
11
switch (location) {
case 1: {
	//bla bla bla...
}
case 2: {
	//bla bla...
}
default: {
	//bla bla...
}
}
Topic archived. No new replies allowed.