Author |
Message |
Ktomislav
|
Posted: Sat Oct 18, 2008 8:57 am Post subject: scanf - integer to string |
|
|
I would like to now how to convert an integer or a real to a string using scanf.
I now that it can be done by itoa but it's forbidden to use. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
OneOffDriveByPoster
|
Posted: Sat Oct 18, 2008 10:58 am Post subject: Re: scanf - integer to string |
|
|
erm... scanf...
So you have things like %f and %d... |
|
|
|
|
|
btiffin
|
Posted: Sat Oct 18, 2008 11:21 am Post subject: RE:scanf - integer to string |
|
|
Umm, scanf converts strings to ints (and other forms).
You may want to look into sprintf (or better snprintf). And being C++, class based and stream conversions abound.
Cheers |
|
|
|
|
|
wtd
|
Posted: Sat Oct 18, 2008 11:37 am Post subject: RE:scanf - integer to string |
|
|
Yes. Stringstreams are your friend. |
|
|
|
|
|
md
|
Posted: Sat Oct 18, 2008 11:38 am Post subject: RE:scanf - integer to string |
|
|
std::stringstream works wonders, however you can just as easily pass a integer or float to a string constructor.
When writing C++ code, use C++ libraries. It annoys me to no end when C++ is taught using C libraries. |
|
|
|
|
|
Ktomislav
|
Posted: Sat Oct 18, 2008 4:23 pm Post subject: Re: RE:scanf - integer to string |
|
|
wtd @ 18.10.2008, 17:37 wrote: Yes. Stringstreams are your friend.
Yes, I know about sstram but I wonder why other people use scanf or maybe sprintf I'm not sure. |
|
|
|
|
|
wtd
|
Posted: Sat Oct 18, 2008 6:22 pm Post subject: RE:scanf - integer to string |
|
|
Because they've been taught how to compile C code with a C++ compiler. |
|
|
|
|
|
md
|
Posted: Sat Oct 18, 2008 7:48 pm Post subject: Re: RE:scanf - integer to string |
|
|
wtd @ 2008-10-18, 6:22 pm wrote: Because they've been taught how to compile C code with a C++ compiler.
Or, they have profs who do not understand the difference between C and C++ like I have had for three "C++" courses so far. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sun Oct 19, 2008 1:23 am Post subject: RE:scanf - integer to string |
|
|
I think that's what I was implying. |
|
|
|
|
|
Rigby5
|
Posted: Mon Oct 20, 2008 8:47 pm Post subject: RE:scanf - integer to string |
|
|
Actually, I think you will find that most programmers prefer the precise way C libraries work in more intuitive ways than C++ libraries.
But the libraries are not really part of either language actually. |
|
|
|
|
|
md
|
Posted: Mon Oct 20, 2008 11:14 pm Post subject: Re: RE:scanf - integer to string |
|
|
Rigby5 @ 2008-10-20, 8:47 pm wrote: Actually, I think you will find that most programmers prefer the precise way C libraries work in more intuitive ways than C++ libraries.
But the libraries are not really part of either language actually.
The most programmers should use C instead of bastardizing C++. Besides, the standard template library is quite intuitive, and is much clearer then the equivalent C code in almost all cases. |
|
|
|
|
|
wtd
|
Posted: Mon Oct 20, 2008 11:34 pm Post subject: RE:scanf - integer to string |
|
|
The behavior of the C language is easily predictable because the language itself does very very little.
The downside to this is that it leads to less than concise code. In order to achieve somewhat concise code one must create complex libraries, and with those comes behavior that can be difficult to predict.
C++, as a much more complex language, features behavior that takes longer to understand. However, it also means that understanding the way the language works makes it easier to understand how libraries work.
Of course the quest of many programmers has been to find a reasonable compromise. See D, Objective-C, Java, C#... |
|
|
|
|
|
Rigby5
|
Posted: Fri Oct 24, 2008 2:43 pm Post subject: RE:scanf - integer to string |
|
|
The I/O libraries of C++ are simply not good at precise formatting of output.
Ever try to output good looking tables of data?
C++ is not more complex in general.
It is easier.
With C we had to work with function pointers and massive switch statements, that C++ does behind the scenes.
But C++ is not good for output.
You do not want C++ deciding data types, you want to do that yourself, when it comes to output.
The main advantage of C++ is modularity.
Functions are grouped with the associated data.
With C, it is as if all functions are global. |
|
|
|
|
|
md
|
Posted: Fri Oct 24, 2008 10:55 pm Post subject: RE:scanf - integer to string |
|
|
C++ is actually rathe reasy to output nicely formatted tabular data. As is any other language. You're simply not doing it right. |
|
|
|
|
|
btiffin
|
Posted: Sat Oct 25, 2008 12:02 am Post subject: Re: RE:scanf - integer to string |
|
|
Rigby5 @ Fri Oct 24, 2008 2:43 pm wrote: The I/O libraries of C++ are simply not good at precise formatting of output.
Ever try to output good looking tables of data?
C++ is not more complex in general.
It is easier.
With C we had to work with function pointers and massive switch statements, that C++ does behind the scenes.
But C++ is not good for output.
You do not want C++ deciding data types, you want to do that yourself, when it comes to output.
The main advantage of C++ is modularity.
Functions are grouped with the associated data.
With C, it is as if all functions are global.
I'm going to ditto md on this one; the manipulators make formatting dirt easy and clean looking to boot.
http://www.cplusplus.com/reference/iostream/manipulators/right.html
And that's just one of the many class interfaces available. STL and extensions add even more candy.
Cheers |
|
|
|
|
|
|