I'm having problems :(
I''m trying to do a very short and simple rpg text game. But I''m having a lot of problems. I''d put the code of what I''ve done, but it''s large to post. So If it''s really needed I will post it. Here is a list of some of my problems:
- Converting int to chars or strings.
- Reading a cin input word by word.
- Using switch() for string cases.
- Reading different info from a same file. I''ve been making a lot
of files to retrieve the different information of a single
room in the game. For example, for the first room, a file
for the description, other for the objects, for the name,
etc. I need to learn how to, in a single file, read all the
information possible.
- Putting color to text if it''s possible.
- And others I don''t remember right now.
I''m programing with BC++ under DOS. Please, help me if you could with any of the problems I listed above.
int to char:
int to string: look up sprintf (if in C) or use this (untested) in C++:
Using switch for strings: don't think you can do that, so you'll have to use lots of if/elses (unless someone has an elegant trick to prove me wrong)
Reading different info from a file: you define what you read and write, and in what order. Therefore, when saving info simply dump each object in turn (saving everything you need). Then read info back in the same order that you wrote it. You'll have to give more details on what your objects contain, etc., for people to help you with that question, I'm afraid.
Putting colour to text: if you are using DOS then have a look around conio.h, which contains many DOS (non-standard) functions for messing around with text, clearing the screen, etc. --assuming your compiler has that sitting around, that is!
Also, note that while you're saying that you are compiling for DOS, you'll have to make sure. There's an option to compile a win32 console application in most compilers. This gives a DOS-like box, but is running under Windows. If you're using a console application and not DOS then have a look around MSDN for console functions (including, but not limited to, SetConsoleTextAttribute.
EDIT: duh, got int to char wrong (used minus instead of plus)
[edited by - Alimonster on May 17, 2002 9:12:24 PM]
char myChar = myInt + '0';
(assumes myInt is a value from 0 to 9, not something outside it)int to string: look up sprintf (if in C) or use this (untested) in C++:
#include <sstream>std::string intToString(int myInt){ std::stringstream str; str << myInt; return str.str();}
Using switch for strings: don't think you can do that, so you'll have to use lots of if/elses (unless someone has an elegant trick to prove me wrong)
Reading different info from a file: you define what you read and write, and in what order. Therefore, when saving info simply dump each object in turn (saving everything you need). Then read info back in the same order that you wrote it. You'll have to give more details on what your objects contain, etc., for people to help you with that question, I'm afraid.
Putting colour to text: if you are using DOS then have a look around conio.h, which contains many DOS (non-standard) functions for messing around with text, clearing the screen, etc. --assuming your compiler has that sitting around, that is!
Also, note that while you're saying that you are compiling for DOS, you'll have to make sure. There's an option to compile a win32 console application in most compilers. This gives a DOS-like box, but is running under Windows. If you're using a console application and not DOS then have a look around MSDN for console functions (including, but not limited to, SetConsoleTextAttribute.
EDIT: duh, got int to char wrong (used minus instead of plus)
[edited by - Alimonster on May 17, 2002 9:12:24 PM]
quote: Original post by Alimonster
int to char:char myChar = myInt - ''0'';
Actually, that''s
char myChar = myInt + ''0'';
quote:
int to string: look up sprintf (if in C) or use this...
Or use itoa.
quote:
Using switch for strings: don''t think you can do that, so you''ll have to use lots of if/elses (unless someone has an elegant trick to prove me wrong)
No tricks here, switch cases must be constant expressions. You can use
case "blah":
but you''ll be doing pointer comparisons, not string comparisons. So yes, you''ll have to use == with std::string or strcmp with C strings in a big if/else statement.As for the text color: Borland compilers used (at least in versions 3.x/4.x) to have the DOS stuff in conio.h. Current MSVC (6 and probably 7) don''t, so you have to use the abovementioned console functions. I don''t know what other compilers have.
---visit #directxdev on afternet <- not just for directx, despite the name
Off topic...
W32 C++ compilers can be found around 100USD in the least expensive version.
On topic word-for-word cin !
cin >> var1 >> var2;
I didn`t know that till I took a C++ this semester. :D
itoa()
int to char*...or sume function in that family.
Make a file format.
class ROOM
{
...//data.
public:
DropToFile(char* fn);
PullFromFile(char* fn);
};
A word of warning.
It can be a colossal pain to do binary file i/o, don`t get too frusturated.
Bugle4d
W32 C++ compilers can be found around 100USD in the least expensive version.
On topic word-for-word cin !
cin >> var1 >> var2;
I didn`t know that till I took a C++ this semester. :D
itoa()
int to char*...or sume function in that family.
Make a file format.
class ROOM
{
...//data.
public:
DropToFile(char* fn);
PullFromFile(char* fn);
};
A word of warning.
It can be a colossal pain to do binary file i/o, don`t get too frusturated.
Bugle4d
~V'lionBugle4d
Here''s the long and short of it ...
* Convert int to string *
* read cin input word by word *
you can try using cin.getline () and then split up the line with a parsing method along the lines of:
Now you have a vector of strings, each of which is one of the tokens of your input - kinda useful.
* using switch for string cases *
try using if/elses - they''re just as good. Otherwise, you could make your syntax data driven - then you could load in your statement templates, and what operation they''d perform. If you do that, it''ll make your game a bit more customizable. It may take you a bit to set up the file loading functions, but you will benefit greatly when you want to add new commands.
* Reading different info from a single file *
try making a format for the file, like :
* color *
erm.. I''m not sure - mayhaps someone else is more familiar with bc++?
Good luck anyhow!
KB
* Convert int to string *
// INTLEN is the number of base 10 digits in MAXINT + 2string itos (int i){ char buffer [INTLEN + 1]; string rv; sprintf (buffer, "%i", i); rv = buffer; return (rv);}
* read cin input word by word *
you can try using cin.getline () and then split up the line with a parsing method along the lines of:
vector parse (string s){ string current; vector rv; int cnt; for (cnt = 0; cnt < s.size (); cnt++) { if (isspace (s[cnt])) { if (current.size() > 0) { rv.push_back (current); current.erase (); } } else { current.push_back (s[cnt]); } } if (current.size () > 0) { rv.push_back (current); } return (rv);}
Now you have a vector of strings, each of which is one of the tokens of your input - kinda useful.
* using switch for string cases *
try using if/elses - they''re just as good. Otherwise, you could make your syntax data driven - then you could load in your statement templates, and what operation they''d perform. If you do that, it''ll make your game a bit more customizable. It may take you a bit to set up the file loading functions, but you will benefit greatly when you want to add new commands.
* Reading different info from a single file *
try making a format for the file, like :
RoomFileEntry #1Description:BEGINEND;Name:
* color *
erm.. I''m not sure - mayhaps someone else is more familiar with bc++?
Good luck anyhow!
KB
__KB
quote: Original post by Ragadast
- Putting color to text if it''s possible.
That''s pretty easy although you have to make sure you use the standard input/output functions( ie: cprintf )
That''s right, it''s cprintf, the c is important. And to set the text color, if I remember correctly, the function is TextColor( color )( note, that since this is for dos, you only have 16 colors, unless it has changed since the last time I was programming for DOS... )...
"And that''s the bottom line cause I said so!"
Cyberdrek
danielc@iquebec.com
Founder
Laval Linux
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
In the cstring library (ANSI C string.h) ther is a function called strtok wich will tokenize your string into words.
May work... Didn''t test the code, you might have to correct some typos..
#define Email Lex & Yacc Function Pointers Virtual Terrain Knowledge Base Real Programmers
"Strong typing is for the weak minded!"
#include <cstring>#include <iostream>using namespace std;int main(){ char buffer[256]; cout << "Enter a string: " << endl; cin.get(buffer,255,''\n''); char* pch; pch = strtok(buffer, " "); while(pch != NULL) { cout << pch << endl; pch = strtok(NULL, " ,."); } return 0;}
May work... Didn''t test the code, you might have to correct some typos..
#define Email Lex & Yacc Function Pointers Virtual Terrain Knowledge Base Real Programmers
"Strong typing is for the weak minded!"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement