Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Please comment on my integer to string code.

Reply
Thread Tools

Please comment on my integer to string code.

 
 
manoj1978@gmail.com
Guest
Posts: n/a
 
      03-09-2006
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.

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      03-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi All,
> I wrote the following to print an integer in its string
> representation for base -36 to 36.
>
> [code redacted]


How do you display numbers in a *negative* base?????
 
Reply With Quote
 
 
 
 
manoj1978@gmail.com
Guest
Posts: n/a
 
      03-09-2006

red floyd wrote:

> (E-Mail Removed) wrote:
> > Hi All,
> > I wrote the following to print an integer in its string
> > representation for base -36 to 36.
> >
> > [code redacted]

>
> How do you display numbers in a *negative* base?????


Consider negadecimal.

27 will be 187

since 187 = 1 * -10 * -10 + 8 * -10 + 7 = 27.

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-09-2006
(E-Mail Removed) wrote:

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


Unit tests are the most important aspect of programming. Your printBase
should return a string, so you can test it, and so it won't "couple" with
its environment. Here you have some cout outside printBase, and some
inside. Move all outside, then write unit tests like these:

assert("whatever" == printBase(-55);
assert("whatever" == printBase(-55);
assert("whatever" == printBase(-55);
assert("whatever" == printBase(-55);

Now each time you change the program, run those tests. If you make certain
kinds of mistakes (but not any mistake) the test will catch you, and you
can use Undo to get rid of the mistake. Undo is much more efficient than
debugging.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
 
Reply With Quote
 
manoj1978@gmail.com
Guest
Posts: n/a
 
      03-09-2006

Phlip wrote:

> Unit tests are the most important aspect of programming. Your printBase
> should return a string, so you can test it, and so it won't "couple" with
> its environment. Here you have some cout outside printBase, and some
> inside. Move all outside, then write unit tests like these:
>
> assert("whatever" == printBase(-55);
> assert("whatever" == printBase(-55);
> assert("whatever" == printBase(-55);
> assert("whatever" == printBase(-55);
>
> Now each time you change the program, run those tests. If you make certain
> kinds of mistakes (but not any mistake) the test will catch you, and you
> can use Undo to get rid of the mistake. Undo is much more efficient than
> debugging.
>
> --
> Phlip
> http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

Hi Philip,
Thanks.I will surely keep this in mind.
This time I had the expected results in a file.
I was redirecting the output and doing file compare.
Regards,
manoj.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-09-2006
Phlip wrote:
> (E-Mail Removed) wrote:
>
>
>>int main()
>>{
>> for(int i = -55; i <= 55; i++)
>> {
>> cout << i << " = ";
>> printBase(i,-10);
>> }
>>}

>
>
> Unit tests are the most important aspect of programming. Your printBase
> should return a string, so you can test it, and so it won't "couple" with
> its environment. Here you have some cout outside printBase, and some
> inside. Move all outside, then write unit tests like these:
>
> assert("whatever" == printBase(-55);
> assert("whatever" == printBase(-55);
> assert("whatever" == printBase(-55);
> assert("whatever" == printBase(-55);


Why four of them in a row? Are there side effects expected that affect
the behaviour of the function?

> Now each time you change the program, run those tests. If you make certain
> kinds of mistakes (but not any mistake) the test will catch you, and you
> can use Undo to get rid of the mistake. Undo is much more efficient than
> debugging.


V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-09-2006
Victor Bazarov wrote:

>>assert("whatever"┬*==┬*printBase(-55);
>>assert("whatever"┬*==┬*printBase(-55);
>>assert("whatever"┬*==┬*printBase(-55);
>>assert("whatever"┬*==┬*printBase(-55);

>
> Why four of them in a row?┬*┬*Are┬*there┬*side┬*effects┬*expected┬*that┬*affect
> the behaviour of the function?


I forgot to edit:

assert("whatever" == printBase(-55));
assert("whatever" == printBase(-54));
assert("whatever" == printBase( -1));
assert("whatever" == printBase( 0));
...

The next, more complex topic is: How to make sure the tests use enough
inputs to exercise all the branches in the code. Don't go there.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      03-09-2006
Phlip wrote:
>
> The next, more complex topic is: How to make sure the tests use enough
> inputs to exercise all the branches in the code. Don't go there.


Classification trees and code coverage tests. Don't know of any free
software to do that.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-09-2006
Phlip wrote:

>
> The next, more complex topic is: How to make sure the tests use enough
> inputs to exercise all the branches in the code. Don't go there.
>


Especially since branch testing is known to be inadequate. <g>

--

Pete Becker
Roundhouse Consulting, Ltd.
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-09-2006
Pete Becker wrote:

> Especially since branch testing is known to be inadequate. <g>


Inadequate for converting an integer to a string???

<vbgd&rc>

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
Comment on trim string function please swengineer001@gmail.com C Programming 102 07-23-2008 08:27 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
Please Comment on this Integer to String Code. manoj1978@gmail.com C Programming 7 03-10-2006 11:32 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM



Advertisments