473,467 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Please comment on my integer to string code.

Hi All,
I wrote the following to print an integer in its string
representation for base -36 to 36.
Please comment on this code.

#include <iostream>
#include <string>

using std::abs;
using std::cout;
using std::endl;
using std::reverse;
using std::string;

char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void printBase(int i, int base)
{
if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
{
cout << "0" << endl;
return;
}

string str = "";
if ((base > 0) && (i < 0))
{
str += "-";
i = -i;
}

do {
if (i % base >= 0)
{
str += charTable[i % base];
i = i / base;
} else {
str += charTable[i % base - base];
i = i /base + 1;
}
} while(i);
reverse(str.begin(), str.end());
cout << str << endl;
}

int main()
{
for(int i = -55; i <= 55; i++)
{
cout << i << " = ";
printBase(i,-10);
}
}

Thanks and Regards,
manoj.

Mar 9 '06
58 3362
ma*******@gmail.com wrote:
A quick search of comp.lang.c shows that what you say is true for C90
also.but differs in C99.


I know that this was addressed in C99 and it is likely to work in
C++, too. However, this does not really help if you want to have
your code portable.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 10 '06 #51

Dietmar Kuehl wrote:
ma*******@gmail.com wrote:
A quick search of comp.lang.c shows that what you say is true for C90
also.but differs in C99.


I know that this was addressed in C99 and it is likely to work in
C++, too. However, this does not really help if you want to have
your code portable.

Yes.Especially since I dont work with that many C99 compilers also.
I have taken care of that now.
Regards,
manoj

Mar 10 '06 #52

ma*******@gmail.com wrote in message ...

BobR wrote:
// ------------------------------------
class Manoj{ [snip]
}; //class Manoj
// ------------------------------------
int main(){
Manoj Man;
Man.execute( 10, std::cout);
Man.execute( 16 );

ofstream File("MyTest.txt");
if( !File ){
std::cerr<<"ofstream File FAILURE!!"<<std::endl;
return EXIT_FAILURE;
}
for(int i( -36 ); i <= 36; ++i){ Man.execute( i, File );} // for(i)
return 0;
} // main()end
// ------------------------------------
--
Bob R
POVrookie
Hi Bob,
Thank you very much for the code.
I changed ofstream File("MyTest.txt"); to std::ofstream
File("MyTest.txt");


Oooops. It was very late and I got in a hurry to get it posted. Glad you
figured it out.
Changed charTable into character array and added step to handle
INT_MIN.
It works fine.Once again thanks.
Regards,
manoj.


I didn't know if you knew 'class', but, thought it would be easier to test
that way (and I wouldn't have to transfer/translate code from the form I test
in ( that class is inside another class of same design, in my 'testbench'
wxWidgets program)).

So, is it *all* working to your satisfaction now?

--
Bob R
POVrookie
Mar 10 '06 #53

BobR wrote:
ma*******@gmail.com wrote in message ...
Oooops. It was very late and I got in a hurry to get it posted. Glad you
figured it out. No probs :-)
I didn't know if you knew 'class', but, thought it would be easier to test
that way (and I wouldn't have to transfer/translate code from the form I test
in ( that class is inside another class of same design, in my 'testbench'
wxWidgets program)). Yeah,I know class.Not used in the code,since i initially wrote this in
c.I just modified it so as to post here :-)
So, is it *all* working to your satisfaction now?

Yes.I thought it may need much work to handle % and / truncating to
infinity.
But on testing with the following functions,found that code needed
change only in INT_MAX handler,since "if (i % base >= 0)" take care of
this change automatically.

int divToInf(int a,int b)
{
bool sign = true;
int add = 0;
if (a < 0)
{
a = -a;
sign = !sign;
add = 1;
}
if (b <0)
{
b = -b;
sign = !sign;
}

if (sign)
{
return a/b + add;
} else {
return -( a/b + add);
}
}

int modToInf(int a,int b)
{
return a - b * divToInf(a,b);
}

Once again thanks.
Regards,
manoj.

Mar 11 '06 #54
In message <11**********************@p10g2000cwp.googlegroups .com>,
ma*******@gmail.com writes
Hi All,
I wrote the following to print an integer in its string
representation for base -36 to 36.
Please comment on this code.

#include <iostream>
#include <string>

using std::abs;
using std::cout;
using std::endl;
using std::reverse;
using std::string;

char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void printBase(int i, int base)
{
if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
{
cout << "0" << endl;
return;
}

[snip rest of code]

You've had lots of feedback on the other stuff, but I haven't noticed
anybody commenting on this combination of two completely unrelated
tests:

If the argument is zero, output "0". Fine; that's the expected result.

If the base is out of bounds, output "0". Do you really want an input
error to produce the same result as valid data?

(Incidentally, what have you got against base-1 representation? ;-)

--
Richard Herring
Mar 14 '06 #55

Richard Herring wrote:
I havent seen this post due to google's interface.Sorry about the
delay.
You've had lots of feedback on the other stuff, but I haven't noticed
anybody commenting on this combination of two completely unrelated
tests:

If the argument is zero, output "0". Fine; that's the expected result.

If the base is out of bounds, output "0". Do you really want an input
error to produce the same result as valid data?
I got that comment also :-)
I have seperated them.
(Incidentally, what have you got against base-1 representation? ;-) Nothing.Somebody asked about negative base numbers in a group.I
searched in math forum about it and wrote the code.
--
Richard Herring

Regards,
manoj.

Mar 15 '06 #56
In message <11**********************@u72g2000cwu.googlegroups .com>,
ma*******@gmail.com writes

Richard Herring wrote:
I havent seen this post due to google's interface.Sorry about the
delay.
You've had lots of feedback on the other stuff, but I haven't noticed
anybody commenting on this combination of two completely unrelated
tests:

If the argument is zero, output "0". Fine; that's the expected result.

If the base is out of bounds, output "0". Do you really want an input
error to produce the same result as valid data?


I got that comment also :-)
I have seperated them.

(Incidentally, what have you got against base-1 representation? ;-)

Nothing.Somebody asked about negative base numbers in a group.I
searched in math forum about it and wrote the code.


Base *1*, not general non-negative bases. It's very easy: 1, 11, 111,
1111, 11111, ...

--
Richard Herring
Mar 15 '06 #57

Richard Herring wrote:
Base *1*, not general non-negative bases. It's very easy: 1, 11, 111,
1111, 11111, ...


:-)

Mar 15 '06 #58
Richard Herring wrote:

Base *1*, not general non-negative bases. It's very easy: 1, 11, 111,
1111, 11111, ...


Looks like a classic off by one bug to me!

Surely it's:
0, 00, 000, 0000, ...

At least, in a C++ newsgroup.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 15 '06 #59

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

Similar topics

2
by: Sathyaish | last post by:
I am writing a program that will take a string that has a C source file and it would format all the multi-line comments like these: //Jingle bells, jingle bells, jingles all the way //O what...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
1
by: Mark Hollander | last post by:
Hi All, Could you please help me convert this code so that it will run in VB.NET Thank You Mark Hollander Private Type USER_INFO Name As String
3
by: Joe | last post by:
Hello All, I am developing a webform which creates ArrayLists of people's names and addresses (the values are retrieved from an xml file) and dynamically drops a user control onto the webform...
7
by: manoj1978 | last post by:
Hi All, I wrote the following to convert an integer to its string representation from base -32 to 32(32 since strtoul takes 32). Used recursion so as not to crowd the logic. Please Comment on...
5
by: Registered User | last post by:
I'm a newbie and have attempted to create a program to convert a string containing the representation of a number in a specified base to a decimal integer. Here's the code: #include <stdio.h> ...
8
by: Greg Lyles | last post by:
Hi all, I'm trying to develop an ASP.NET 2.0 website and am running into some real problems with what I thought would be a relatively simple thing to do. In a nutshell, I'm stuck on trying to...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
2
by: BairnsRus | last post by:
Wonder if anyone can point me in the right direction? For part of my coursework I have been asked to create a BMI monitor which calculates height, weight and displays BMI for up to 5 records. So far...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
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...
0
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...
0
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.