Page 1 of 1
C++ help
Posted: Mon Nov 19, 2007 7:08 pm
by liquid_gen
I dont know if this is allowed but i need help with C++. I dont think it should be a problem cos i dont want help with an algorithm for a problem. its just that i quite often need to manipulate ints as strings etc and i dont really know how to so i cant solve the question despite knowing what to do as far as my algorithms concerned.
So anyone know how i can treat int as strings in C++?
Re: C++ help
Posted: Mon Nov 19, 2007 7:20 pm
by xenon
I don't exactly understand what you mean, but you can 'print' an integer to a string using the function sprintf. It is more C then C++ and it targets a char array rather than a string class, but I know too little of C++ to be of help for it. In C:
Code: Select all
#include <stdio.h>
int main(){
int my_int;
char my_string[16];
my_int=123456;
sprintf(my_string,"%d",my_int);
/* my string now contains "123456" */
return 0;
}
In the second argument, the format string, you can use all formatting options that you also have with the normal printf function.
Re: C++ help
Posted: Mon Nov 19, 2007 7:30 pm
by liquid_gen
Yes thats basically what i need though i'd prefer a C++ version and also i'd like to know how to do the reverse (i.e. strings to ints)
Thanks for your help so far mate
Re: C++ help
Posted: Mon Nov 19, 2007 8:03 pm
by daniel.is.fischer
Again rather C than C++: string to int conversion via 'atoi', 'atoll', 'strtoull' and such, just do "man atoi" in bash (if you're on windows, I don't know if that has man pages

)
Re: C++ help
Posted: Mon Nov 19, 2007 9:07 pm
by xenon
Atoi and it's cousins are fine. You can also use sscanf which is the 'opposite' of sprintf (just like scanf is the opposite of printf):
Code: Select all
#include <stdio.h>
#include <string.h>
int main(){
char my_string[16];
int my_int;
strcpy(my_string,"987654");
sscanf(my_string,"%d",&my_int);
/* my int now contains 987654 */
return 0;
}
Still, this is pure C. I found this
http://www.parashift.com/c++-faq-lite/m ... ssues.html for C++, which looks unnecessarily complex to me (but I always have that with C++).
Re: C++ help
Posted: Mon Nov 19, 2007 9:36 pm
by ed_r
liquid_gen, I am in awe of your courage. It seems like C++ might be your first programming language? I mean,
before having learnt C? That is astounding bravado

- well done, sir!
In case it's not too late to turn you away from the dark side, I googled "C++ rant" (just on the off-chance, y' know) and found
this. Scroll down to the bit that starts
"C++ is philosophically and cognitively unsound ..." and draw your own conclusions!
Good luck!

Re: C++ help
Posted: Tue Nov 20, 2007 11:59 am
by stijn263
nvm
Re: C++ help
Posted: Tue Nov 20, 2007 3:58 pm
by daniel.is.fischer
ed_r wrote:Scroll down to the bit that starts
"C++ is philosophically and cognitively unsound ..." and draw your own conclusions!
Good luck!

But couldn't the same be said about practically all languages?
Take C, there you find declarations like char p, *q, a[10];
That is not only
philosophically and cognitively unsound as it mixes quite different types, but also plain wrong because neither '*' nor '[' or ']' are valid identifier characters.
Re: C++ help
Posted: Tue Nov 20, 2007 6:35 pm
by ed_r
From what I could make out, I figured the C++ rant was more to do with program design than syntax. Your
char p, *q, a[10] example looks to me like nothing more serious than syntactic sugar. But yeah, I agree with your point: C++ is not alone in having its detractors. I'm not about to get into a fight about this.
It was only supposed to be a lighthearted remark!

Re: C++ help
Posted: Tue Nov 20, 2007 8:19 pm
by daniel.is.fischer
Wasn't taken as anything else, I just pitied the poor C++ folks and wanted to comfort them a little
Edit: Just saw the rant was by Erik Naggum. That explains the harsh words.
P.S.: Being a Haskeller, thus having been exposed to the advantages of static (and strict) typing, my example seems a bit more than just syntactic sugar to me. Had I one quid for every time I've fallen into the trap and wrote a declaration like int[] ar; or int* a,b,c; meaning to define three pointers, I'd be rather well off. Fortunately I always compile my C code with gcc -O3 -Wall, so they're always caught at compile time.
Re: C++ help
Posted: Wed Nov 21, 2007 12:05 am
by joshbowman205
I agree c++ isnt perfect but i think perhaps its a useful step before moving on to C# or java.
anyone willing to dispute that its the fastest language?
Re: C++ help
Posted: Wed Nov 21, 2007 12:15 am
by daniel.is.fischer
As far as I know C is faster than C++ (unless you write C in C++) and if you know what to do, assembly language should be the fastest. But speed isn't everything, safety is often also important and there C/C++ are less impressive.
Choose the language(s) you're most comfortable with.