Computer Science Canada

scanf - integer to string

Author:  Ktomislav [ 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.

Author:  OneOffDriveByPoster [ Sat Oct 18, 2008 10:58 am ]
Post subject:  Re: scanf - integer to string

erm... scanf...
So you have things like %f and %d...

Author:  btiffin [ 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

Author:  wtd [ Sat Oct 18, 2008 11:37 am ]
Post subject:  RE:scanf - integer to string

Yes. Stringstreams are your friend.

Author:  md [ 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.

Author:  Ktomislav [ 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. Mr. Green

Author:  wtd [ 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.

Author:  md [ 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.

Author:  wtd [ Sun Oct 19, 2008 1:23 am ]
Post subject:  RE:scanf - integer to string

I think that's what I was implying.

Author:  Rigby5 [ 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.

Author:  md [ 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.

Author:  wtd [ 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#...

Author:  Rigby5 [ 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.

Author:  md [ 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.

Author:  btiffin [ 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

Author:  Rigby5 [ Wed Oct 29, 2008 12:11 pm ]
Post subject:  Re: RE:scanf - integer to string

btiffin @ Sat Oct 25, 2008 12:02 am wrote:
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



I still disgree.
Sure you can set the width, but only for a single output object at a time.
If you have a dozen things to output on a line of a table, that is a real pain.
C does is easier and more clearly controlled, because you can do dozens of variables in one printf.
The formatting is associated with each variable, and not with the output steam object.

I don't know anyone who uses the C++ I/O operations.

Author:  btiffin [ Wed Oct 29, 2008 5:32 pm ]
Post subject:  Re: scanf - integer to string

Quote:

I still disgree.
Sure you can set the width, but only for a single output object at a time.
If you have a dozen things to output on a line of a table, that is a real pain.
C does is easier and more clearly controlled, because you can do dozens of variables in one printf.
The formatting is associated with each variable, and not with the output steam object.

I don't know anyone who uses the C++ I/O operations.

Disagreeing is a good thing, imho. The computer field is a vast, wide and deep field with tools that'll float just about any boat.

old guy ramble follows
I've only worked C++ on one serious large scale project. I'm not a particular fan of C++ as I feel the complications involved require above avergage programming and management skills. BUT, for above average developers, C++ can build awesome environments for other programmers that follow or assist in projects. The KDE system, is huge and unwieldy, but it has a layer now that is very amenable to most programmers and useful "good" applications can be built fairly quickly. All due to efforts of some above average programmers that laid down foundations.

So, the system I did work on, we built a very useful output stream that you could throw just about any data at, and configuration controlled the final look. With C you are constrained to functional I/O, C++ can be used to build some very clean and useful toolkits. I was distressed at the time, watching how fragile the tool was when normal IQ coders were tasked with building the foundations, not simply using those foundations, but hey, large corporations can somewhat afford the luxury of building tools that make the next application more powerful and easier to deal with.

Sadly, our industry is usually chasing the next shiny new ball, so many of these corporate toolkits are tossed before the end of the first project, and not built upon as Bjarne actually intended when C++ was on the drawing board.

In comes down to the moldy old expression, Your Mileage May Vary. And in my not so humble opinion, programmers are far more productive using tools they "believe in", and all the bells and whistles and tools and technology are completely secondary, contrary to what management may believe when forcing tools on or dictating environments for developers. So for anyone free to choose, go right ahead and choose. You'll build better programs using tools you like than using tools your buddy may think is the best thing since sliced bread (unless of course, you happen to believe that as well). Having said that, if you are not free to choose then suck it up and make the best of it. Leave the negativism at home, on the shitter, between friends over drinks or here on the forums. A complainer can taint an entire project with useless and counter-productive bitching. It is best to be the positive bright light in whatever task is at hand and management will likely take notice. I say this having been a negative nelly on occasion ... it doesn't help. Wink

Cheers
edits; typos and corrections

Author:  Rigby5 [ Fri Oct 31, 2008 11:51 pm ]
Post subject:  RE:scanf - integer to string

The point is that in reality, with C printf you can do a format and output a whole table row in a single simple instruction.

While in C++ it could easily take 20 instructions.
It takes 1 instruction to set the width for every single variable, and you have to output one variable at a time, in its own instruction.

C printf is then at least 10 times easier and faster to use to format output then cout.

Author:  btiffin [ Sat Nov 01, 2008 12:34 am ]
Post subject:  RE:scanf - integer to string

Well, yes with the "stock" manipulators. Or (with some coded toolkits) one line of code can set an option and then stream as many variables as you can pack on a line, all formatted to the particular bosses' liking or to a dataform or ...

That level of usability does not come for free, but it's possible. And that is how Bjarne envisioned C++ in my humble opinion. A compiler for building tools that build apps. Many coders go straight from C++ to app, so they really do lose out on the potential. But that potential (as I mentioned before) is not for the faint of heart or the impatient. It all comes down to choice. Personally, I avoid C++ as I don't have the level of discipline to use it effectively. So I pick other tools. The YMMV dealy.

Cheers

Edit; and we risk getting in trouble with the good folks of compsci.ca, dragging this thread so far off topic. We can argue the ++ or -- of C and C++ for decades. Which I'm always up for, but this is getting less and less fair to Ktomislav if he tries to glean any usable info from his thread now.


: