473,586 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int to char[]: What's the reciprocal of std::atoi?

I am completely stumped after many hours pouring through my books, but
I need to write fractions. I have a method to convert a string
expression representing a fraction as "n/d" into a float and separate
numerator and denominator integers (using std::atoi(std:: string)), but
now I need to convert an integer variable numerator and denominator
pair into a char array or std::string class as "n/d". How can this be
done?

Thanks!
Mark
Jul 19 '05 #1
1 8061
"Magig Boatman" <fr*****@www.co m> wrote in message
news:c2******** *************** ***@posting.goo gle.com...
I have a method to convert a string expression representing a fraction as
"n/d" into a float and separate numerator and denominator integers (using
std::atoi(std:: string)),
std::atoi takes a const char *. I guess you're calling std::string's
c_str() function and then passing the result to std::atoi? In any case, the
preferred way to convert from a std::string to a number is to use
std::istringstr eam. See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-38.2
now I need to convert an integer variable numerator and denominator
pair into a char array or std::string class as "n/d". How can this be
done?


Use std::ostringstr eam. Again, see the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-38.1

Your code would probably look something like this:

std::ostringstr eam oss;
oss << n << '/' << d;
return oss.str();

--
Russell Hanneken
rh*******@pobox .com
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
7434
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()" for more information. > > **************************************************************** > Personal firewall software may warn about the...
3
1931
by: GGG | last post by:
I have a situation where at tool is passing me a large array of strings that I need to process in a particular type of data. Each item in the array gets to me as a pair of std::strings, basically, one string represents the data, one string represents the data type. Here's a quick example of what it sort of looks like string data = {...
6
3447
by: Henry Jordon | last post by:
This is a piece of code that I have left to complete my project. I have hopefully one small error that needs to be fixed. This portion of the code evaluates the postfix notation that is passed to it. I have marked the error line. Thank you very much for your help. void evaluates(char *postfix) { int position; char number1, number2,...
9
3628
by: Steven T. Hatton | last post by:
I know I can write one, or use QString, etc., but is there a native C++ function to convert a NTBS of digits to an integer? -- "If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in which we never know what we are...
6
4259
by: Steve Gallagher | last post by:
Can someone tell me (hopefully with a pointer to the appropriate place in the Standard), what atoi() ought to do with the following: int outp = 0; outp = atoi("2+*&^%$#@!"); One school of thought on my team is that atoi() is supposed to
4
2541
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
6
19021
by: Travis | last post by:
I'm curious what the most reliable way to convert a char * (or a char for that matter) to a bool is. It's safe to assume that char * will be either '0' and or '1' and I want the bool set accordingly. I'm seeing stuff (from others) like this : bool myBool = (char)atoi(myCharPtr);
11
12747
by: hamishd | last post by:
Is this possible? Sorry if this question isn't relevant here. actually, I'm really trying to convert a unsigned char * to an int
50
5203
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was success or not. Am I wrong? Bill
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.