C++ help
-
liquid_gen
- Posts: 3
- Joined: Fri Nov 02, 2007 2:15 pm
C++ help
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++?
So anyone know how i can treat int as strings in C++?
-
xenon
- Posts: 63
- Joined: Wed Sep 20, 2006 7:09 pm
Re: C++ help
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:
In the second argument, the format string, you can use all formatting options that you also have with the normal printf function.
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;
}
-
liquid_gen
- Posts: 3
- Joined: Fri Nov 02, 2007 2:15 pm
Re: C++ help
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
Thanks for your help so far mate
- daniel.is.fischer
- Posts: 2400
- Joined: Sun Sep 02, 2007 11:15 pm
- Location: Bremen, Germany
Re: C++ help
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
)
Il faut respecter la montagne -- c'est pourquoi les gypaètes sont là.
-
xenon
- Posts: 63
- Joined: Wed Sep 20, 2006 7:09 pm
Re: C++ help
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):
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++).
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;
}
Last edited by xenon on Tue Nov 20, 2007 5:30 am, edited 1 time in total.
- ed_r
- Posts: 1009
- Joined: Sun Jul 29, 2007 10:57 am
Re: C++ help
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!
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!
!647 = &8FDF4C
- stijn263
- Posts: 1505
- Joined: Sat Sep 15, 2007 11:57 pm
- Location: Netherlands
- daniel.is.fischer
- Posts: 2400
- Joined: Sun Sep 02, 2007 11:15 pm
- Location: Bremen, Germany
Re: C++ help
But couldn't the same be said about practically all languages?ed_r wrote:Scroll down to the bit that starts "C++ is philosophically and cognitively unsound ..." and draw your own conclusions!
Good luck!
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.
Il faut respecter la montagne -- c'est pourquoi les gypaètes sont là.
- ed_r
- Posts: 1009
- Joined: Sun Jul 29, 2007 10:57 am
Re: C++ help
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!
It was only supposed to be a lighthearted remark!
!647 = &8FDF4C
- daniel.is.fischer
- Posts: 2400
- Joined: Sun Sep 02, 2007 11:15 pm
- Location: Bremen, Germany
Re: C++ help
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.
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.
Il faut respecter la montagne -- c'est pourquoi les gypaètes sont là.
-
joshbowman205
- Posts: 59
- Joined: Wed Oct 31, 2007 4:28 pm
Re: C++ help
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?
anyone willing to dispute that its the fastest language?
- daniel.is.fischer
- Posts: 2400
- Joined: Sun Sep 02, 2007 11:15 pm
- Location: Bremen, Germany
Re: C++ help
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.
Choose the language(s) you're most comfortable with.
Il faut respecter la montagne -- c'est pourquoi les gypaètes sont là.