Advertisement

String -> Integer

Started by October 22, 2002 02:17 PM
18 comments, last by vbuser 22 years ago



      int cInt(const char *input) // takes an array of chars {  int result = 0;  for(;*input>='0'&&*input<='9';++input)   result=result*10+*input-'0';   return result; }  


Actually, since it's signed, this might be better. . .


  int cInt(const char *input) {  int result = 0;  while(*input==' ')   ++input;  if(*input=='-')   {    ++input;    for(;*input>='0'&&*input<='9';++input)     result=result*10-*input-'0';   }  else   for(;*input>='0'&&*input<='9';++input)    result=result*10+*input-'0';   return result; }  


[edited by - smart_idiot on October 24, 2002 7:19:52 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
You don''t want char* const, but const char*. The former doesn''t prevent you from modifying the characters.

Still... I agree with Fruny.
Advertisement
When in doubt, odds are that Fruny is right.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
"You don''t want char* const, but const char*. The former doesn''t prevent you from modifying the characters."

wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?
"You don''t want char* const, but const char*. The former doesn''t prevent you from modifying the characters."

wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?
quote: Original post by smart_idiot
When in doubt, odds are that Fruny is right.

I don''t know if I want to assume the implied responsibility, I''m wrong way too often for my taste. (especially since there should be a ss.clear(); before ss.str( "1.5e6" ), just to be safe )

quote:
wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?


Read it right to left :
char* p             = p is pointer to charconst char* p       = p is pointer to const charchar* const p       = p is const pointer to charconst char* const p = p is const pointer to const char
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
quote: Original post by Beowulf_
wouldn''t const char* just make the pointer constant? i.e. a constant pointer to a char? as opposed to a pointer to a constant char?
Nope. Try it out:

void f(char* const foo) { *foo = ''T''; }
void g(const char* foo) { *foo = ''T''; }
Ok, thanks for the info. (I just learned about pointers yesterday) So is that right to left thing a real rule, or is that just a trick to remember pointers or what?

Hey, as long as you''re helping out a NB, do you know any good intros to making windows apps? I tried dissassembing the default visual studio one, but it had way too much baggage, and was really hard to figure out what each thing did.
quote: Original post by Fruny
I don''t know if I want to assume the implied responsibility, I''m wrong way too often for my taste. (especially since there should be a ss.clear(); before ss.str( "1.5e6" ), just to be safe )



Yes, there should be a ss.clear(). You wouldn''t believe the amount of time I lost trying to debug this.
Gaiomard Dragon-===(UDIC)===-
quote: Original post by Outworlder
Yes, there should be a ss.clear(). You wouldn''t believe the amount of time I lost trying to debug this.


About as much as I lose everytime I forget Nowadays, I just stick a boost::lexical_cast in there ( int i = boost::lexical_cast<int>( "1000" ); )

quote:
Ok, thanks for the info. (I just learned about pointers yesterday) So is that right to left thing a real rule, or is that just a trick to remember pointers or what?


Your type is cut in two parts : before the * and after the *. What is before refers to what you''re pointing to, what is after refers to the pointer itself. So yeah, it''s as close as a ''rule'' as you can get. To the point that some people advise to write char const* p instead of const char* p so that reading it from right to left exactly reads "p is a * (pointer) to const char". Which you may, or may not want to do (the compiler doesn''t care, it is still on the left of the *).

quote:
Hey, as long as you''re helping out a NB, do you know any good intros to making windows apps? I tried dissassembing the default visual studio one, but it had way too much baggage, and was really hard to figure out what each thing did.


Nope, sorry.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement