Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Need a little help with text game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 al0816   Members   -  Reputation: 109

Like
0Likes
Like

Posted 17 October 2012 - 06:17 AM

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"?

Edited by al0816, 17 October 2012 - 06:18 AM.


Sponsor:

#2 slicksk8te   Members   -  Reputation: 190

Like
0Likes
Like

Posted 17 October 2012 - 10:00 AM

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 10char locsToGo[100][FILENAME_LEN];[/source]

Hope this helps and good luck with your text game. :)

#3 Serapth   Members   -  Reputation: 3285

Like
1Likes
Like

Posted 17 October 2012 - 11:07 AM

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.

#4 al0816   Members   -  Reputation: 109

Like
0Likes
Like

Posted 17 October 2012 - 09:21 PM

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);
}
}

Edited by al0816, 17 October 2012 - 09:23 PM.


#5 al0816   Members   -  Reputation: 109

Like
0Likes
Like

Posted 18 October 2012 - 06:31 AM

help pls :c the only idea i have that is something wrong with next file wich must be loaded after chioce




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS