char data type, only for printing characters?

Started by
3 comments, last by GROUDON 16 years, 8 months ago
can i use a char as a number? cause i would like to be able to use any number from 0 to 255 and be able to print out the number and NOT it's specific character. Or do i have to use short int? How many bits is a short int, and its number range? is there a way to have a 4-bit variable? (0-16) i'm using structures for weapons, character info, etc, and i figure that when i get around to learning file IO to save my game, i want to make the file only as big as it needs. or is it easier to make every variable int's for file IO? are int's and char's by default signed or unsigned? I think it's unsigned. I'm not asking for file IO turorials (for save-games) right now, but if you have any in mind, feel free to gimme some.
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
Advertisement
Quote:Original post by GROUDON
can i use a char as a number?


Yes, except for printing.

Quote:cause i would like to be able to use any number from 0 to 255 and be able to print out the number and NOT it's specific character.
Or do i have to use short int?


No, you can use any integral type. (Also, note that "short int" is redundant; "short" already names that type.) You can also just cast an existing char at the time that you need it.

Quote:How many bits is a short int, and its number range?


At least enough to hold all the bits of a char, assuming that's what you're wondering. (Same for any other integral type).

If you need an exact-sized type, you're not going to get it directly from the language, sorry. You'll need either a platform-specific typedef, or something more advanced.

Quote:is there a way to have a 4-bit variable? (0-16)


Sort of. It depends what you really are asking for. Though first of all, the representable range would be 0 to 15.

You can make a class that overloads the mathematical operators, using any unsigned type as a "storage" data member. Then either provide an accessor for the value that masks off all but the four low bits; or do the masking after each operation and have the accessor return the raw value. (Don't forget to provide a constructor to set the initial value :) )

If you want to save data to disk as efficiently as possible, and you know your input values range 0 to 16, then the pragmatic approach is to take values two at a time, shift the first, bitwise-or in the second, and write the resulting byte to the file.

If you're not looking to do math, though, and just want something to be in any of 16 logical states, you probably just want an enum.

But you cannot create something that takes up only four bits of physical memory. Not in C++ and not in any other language. The hardware just doesn't work like that.

Quote:or is it easier to make every variable int's for file IO?


Use what makes sense. But in general, use ints unless you have a specific reason not to.

Saving space does not, generally, count as a reason not to use int. Your hard drive is capable of holding a reasonable save file for any beginner project millions of times over.

Quote:are int's and char's by default signed or unsigned? I think it's unsigned.


int, short and long are signed by default.
char will behave like either signed char or unsigned char; it is up to the compiler vendor to decide this, and char is still a separate type from either of the other two - even different from the one that it behaves exactly like.

can you have an array of bools?

Example: bool equiparmor[];
Use the index to reference which armor,
and use the values true or false to determine if it's equippable.
Then have a seperate struct that includes char idno; for each armor (to provide an index for the equiparmor array) and other various attributes.

As you can probaly see, i'm trying my hands at making an RPG. right now i'm putting all the structs i'll need in a seperate header file. I'll need to make a game structure too, to provide various things like fighting, and shops, but i'm not sure how to lay it out for the player because i'm not too familiar with text-based RPG's... I'd like an example of the gameflow, such as how the player travels and enounters enemies and such. I'll try and search gamedev for one, until i succeed at finding one, feel free to give me a link.
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
I found one, it's made by Black Night, it's called Nightfall, i just downloaded the exe and source code. Thanks Black Night! This just might help... but i'm gonna credit Black Night in my game or in an included text file if it helps by any amount.

@Black Knight: Thanks again and i'll check it out. Tell me if you see my post, and any help or hints you can give, give then. BTW: i will definately put ur name in a text file and include that in any text RPG that i use sny of your code in. I haven't even looked at it, and i'm excited!
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
How does a text-based RPG use the mouse!?!
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.

This topic is closed to new replies.

Advertisement