Need a little help with text game

Started by
3 comments, last by al0816 11 years, 6 months ago
Hi!
I am making a text quest game in C++ and it have choices like where to go or what to do. My teacher showed me how to write the game without cases and switches . Just in 2 whiles. It loads 1st text files then it displays text before it founds special line [goto] ( just spicial line) . and then the other "while" starts working . It shows us text and the number of variant of it. It is easy to make a txt quest game this way but there is 1 minus it loads txt with only digits. I made that 1st file is not digited and it has name, but i cant get it to work with choices that leads to another not digit file.
sorry 4 bad english . If u dont get how it works ill try to explain. We got the .cpp file(i work in codeblocks) and some txt near the cpp. the cpp code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cstdio>
#include <iostream>
int main()
{
setlocale(LC_ALL,"russian_russia");
char location[100]="a" ; // !!!
char filename[10]="a";
while(1 == 1 )
{
// !!!
strcat(filename, ".txt");
FILE *file = fopen(filename, "rt");
while(1==1)
{
char line[1001];
fgets(line, 1000, file);
fscanf(file, line); // !!!
if (strcmp(line,"[goto]\n") == 0)
{
break;
}
printf(line);
// !!!!
}
int t = 1;
char locsToGo[100];
while (1==1)
{
char line[1001];
fgets(line, 1000, file);
fgets(file, "%d\n", &locsToGo[t]);
if (strcmp(line,"[end]\n") == 0)
{
break;
}
printf("%d: %s", t, line);
t++;
}
char a='o'; //////Noob DEFENCE
while (!(a>='1' && a<='4'))//////Nooob DEFENCE
{
scanf("%c", &a);////////NOOOOB DEFENCE
}
int input = a-'0';
char location = locsToGo[input];
fclose(file);
}
}



and text file looks like:
text
[goto]
1st variant
12
2nd variant
13
[end]

so can you help me with my problem So i can do variants that leads to not digit filebut file with name like "b.txt"?
Advertisement
If I'm understanding you correctly, you would like to load a file from a string rather than a digit, that is you would like to load "b.txt" instead of "1" or some variation of that. To do this you can change:
[source lang="cpp"]fgets(file, "%d\n", &locsToGo[t]);[/source]
to load the a string into locsToGo instead of an int.
The declaration of locsToGo would be:

[source lang="cpp"]#define FILENAME_LEN 10

char locsToGo[100][FILENAME_LEN];[/source]

Hope this helps and good luck with your text game. :)
This is code your teacher provided you? There are a number of problems or potential problems in that code, I am not sure you really want to go into them all. Are you supposed to be using C by the way ( as opposed to C++ ), because that is essentially what you are doing. Move all your variable definitions to the top of the file and thats basically straight C code.

Also, this line
fscanf(file, line);
doesn't actually do anything.


As to your question:
1 minus it loads txt with only digit

I take this to mean, when you read the file in, you are only getting numbers and not strings? That is because this line:
fgets(file, "%d\n", &locsToGo[t]);

Well... it's just wrong. Im actually rather shocked it compiles. ( Does it compile? )

fgets is supposed to be (buffer, maxCharactersForBuffer, file)
You are doing:
(file, const char*, char**)

From the looks of that parameters list, you actually want to do a fscanf there.

This is code your teacher provided you? There are a number of problems or potential problems in that code, I am not sure you really want to go into them all. Are you supposed to be using C by the way ( as opposed to C++ ), because that is essentially what you are doing. Move all your variable definitions to the top of the file and thats basically straight C code.

Also, this line
fscanf(file, line);
doesn't actually do anything.


As to your question:
1 minus it loads txt with only digit

I take this to mean, when you read the file in, you are only getting numbers and not strings? That is because this line:
fgets(file, "%d\n", &locsToGo[t]);

Well... it's just wrong. Im actually rather shocked it compiles. ( Does it compile? )

fgets is supposed to be (buffer, maxCharactersForBuffer, file)
You are doing:
(file, const char*, char**)

From the looks of that parameters list, you actually want to do a fscanf there.

Yes it compile and works. As written there I work in codeblocks. Just it is not writen in iostream.It is on stdio.And now i made somechanges that gives you variants that leads to a non digit file(like file b.txt) but if you decide and then press enter it will just print [end] all time :c
P.s 1st file we load is a.txt but it can be changed
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cstdio>
#include <iostream>
int main()
{
setlocale(LC_ALL,"russian_russia");
char location[100] = "a"; // !!!
char filename[10] = "a";
while(1 == 1 )
{
// !!!
strcat(filename, ".txt");
FILE *file = fopen(filename, "rt");
while(1==1)
{
char line[1001];
fgets(line, 1000, file);
fscanf(file, line); // !!!
if (strcmp(line,"[goto]\n") == 0)
{
break;
}
printf(line);
// !!!!
}
int t = 1;
char locsToGo[10][100];
while (1==1)
{
char line[1001];
fgets(line, 1000, file);
fscanf(file, "%s\n", &locsToGo[t]);
if (strcmp(line,"[end]\n") == 0)
{
break;
}
printf("%d: %s", t, line);
t++;
}
char a='o'; //////Noob DEFENCE
while (!(a>='1' && a<='4'))//////Nooob DEFENCE
{
scanf("%c", &a);////////NOOOOB DEFENCE
}
int input = a-'0';
strcpy(location,locsToGo[input]);
fclose(file);
}
}
help pls :c the only idea i have that is something wrong with next file wich must be loaded after chioce

This topic is closed to new replies.

Advertisement