Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 256 257 [258] 259 260 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 884592 times)

HavingPhun

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3855 on: February 11, 2013, 03:04:02 pm »

Code: [Select]
std::cin >> *PCommand;
Try replacing
Code: [Select]
char Command;with
Code: [Select]
char Command[10];and
Code: [Select]
std::cin >> *PCommand;with
Code: [Select]
std::cin >> Command;
Thank you is there a way to make chars without choosing how many characters to have as a limit? Like this: char Command; ? Or are those strings. I never understood that problem until now.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3856 on: February 11, 2013, 03:09:13 pm »

Yeah, I'm pretty sure that is a string.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3857 on: February 11, 2013, 03:37:47 pm »

In C, there was only one way of representing strings in code: character arrays. And, since C requires arrays to have fixed size, you had to make sure your character array was long enough for the string you wanted to store.

In C++, the std::string class was added, which contains a character array that is dynamically re-sized as needed, so you don't have to worry about lengths. However, it has more memory and CPU overhead than just plain character arrays. Also, "hello" is still a character array - std::string is not a primitive type. It's a wrapper class.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3858 on: February 12, 2013, 01:52:28 am »

I used to think c-strings had their uses in C++, but now that I fully understand stringstreams I'm not so sure anymore.
except for maybe optimization. since, as Mego pointed out, they use less memory and CPU  but it's such a tiny amount, on modern computers the difference isn't noticeable.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3859 on: February 12, 2013, 02:21:31 am »

I don't know what stringstreams are at all(except as a vague idea). But I don't feel very uncomfortable :P std::string is an awesome container.  It also has std::string::c_str() for compatibility issues.
« Last Edit: February 12, 2013, 03:29:54 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3860 on: February 12, 2013, 03:07:03 am »

I don't know what stringstreams are at all(except as a vague idea). But I don't feel very uncomfortable :P std::string is an awesome container.  It also has std::string::c_str() for compatibility issues.

FTFY.

Stringstreams are fun.

You know how you can pump virtually any data into cout, so long as it has operator>> defined? Same goes for ostringstreams. Want a string representation of everything you put in it? Call ostringstream::str().
You know how you can read in formatted data from cin, so long as the data type has operator<< defined? Same goes for istringstreams.
Want to do both? There's a class for that, too: stringstream.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3861 on: February 12, 2013, 03:29:28 am »

I have to agree with Mego, they're pretty awesome / fun.
here's a tiny example of what they are.

std::stringstream streamname;  //first you set up the stringstream, (don't know the technical term for that)
//now you can put strings in the stream, numbers, variables, etc.
x=24;
name<<"string"<<42<<"string"<<x;
name.str is how you'd call the string, so
cout>>name.str(); should output  "string42string24"


the main thing I use it for is filenames, but it has thousands of uses.


name<<"finished/"<<savednumber<<".bmp";
    SDL_SaveBMP(temp,name.str().c_str());

^^^bit of code from one of my programs^^^

ohh I've also used it for score a few times.
like,
score<<"Score: "<<scorevalue;
makes a string that attaches a variable to the end.
pretty nifty and much easier than c-strings.
« Last Edit: February 12, 2013, 03:32:21 am by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3862 on: February 12, 2013, 03:32:04 am »

Interesting. :P I did exactly the same thing with a lot of append() calls in a program I had. xD
Aren't streams slow, though?
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3863 on: February 12, 2013, 03:34:52 am »

how would you do that with append?
and I've never noticed any speed issues, but then again, i've only done small projects so what do I know.
I've never used append before, from a quick glance it looked to be the same thing as substr()
but from what I can tell, is it's like substr, but has the option of adding something to the end of the substring?
but it looks like you can only add one thing to the end of the string and it can't be a variable, is that right?
stringstreams look to be much more useful.
« Last Edit: February 12, 2013, 03:40:35 am by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3864 on: February 12, 2013, 03:43:45 am »

Append? It does what it sounds like it'd do. :P Append a string to another string.

Eg, if I have a var double x = 42, I would do:

string thisString;
thisString.assign("string");
thisString.append("42");
thisString.append("string");
thisString.append(to_string(x));
cout << thisString.c_str();

output: string42string42
Not quite sure if it's 42.0 for the second one or not.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3865 on: February 12, 2013, 03:51:30 am »

I seemed to have forgotten about to_string()s existence,
but if you were going to go that route, couldn't you just use addition?

string thisString;

thisString = "string";
thisString += to_string(42);  //the first number in my last example was a number not a string, and in your example it's a string;
thisString += "string";
thisString += to_string(x);
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3866 on: February 12, 2013, 03:55:15 am »

Well, I'm pretty sure += is defined to use append. O_o
Also, a literal 42 is the same as a string literal "42" in your example, so it doesn't matter. Unless you did some arithmetics, in which case yes, to_string().

One more thing: does anyone know if the following is legitimate?

edit
I was going to post

const const double*& strangeType();,
but upon further reflection it is, indeed, legitimate. A function that returns the address of a constant pointer to a constant double. Not that that'd be useful. Just gotta use a better method.
« Last Edit: February 12, 2013, 04:11:33 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3867 on: February 12, 2013, 04:03:15 am »

One more thing: does anyone know if the following is legitimate?

normally after one says something like this, they post something that the question is about.
liiiike, you could say.


then I would say,
noo i'm pretty sure that's a photoshop, but I dunno. I've only worked on small projects, so what do I know?

see, it implies you're going to post something that you're questioning the legitimacy of, because the following, implies it's going to follow your question.

but I think it's the other way around, that Append uses string arithmetic and substr
it wouldn't make any sense for += to be defined to use append, because append is in the <string> library, while string arithmetic is n... wait, in order to do string arithmetic you'd also have to include <string> library, because without that library you wouldn't have a string to do it on, so it might be in there as well.
but it makes more sense to me that append would be a function that is a mix of arithmetic and substr.
« Last Edit: February 12, 2013, 04:13:15 am by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3868 on: February 12, 2013, 04:10:27 am »

One more thing: does anyone know if the following is legitimate?

Error: undefined reference to `WinMain@16'
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3869 on: February 12, 2013, 04:43:55 am »

what a terrible joke.
« Last Edit: February 12, 2013, 05:43:10 am by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.
Pages: 1 ... 256 257 [258] 259 260 ... 796