473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to display 12345 as 12,345 ??

suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??
Jul 22 '05 #1
23 2806
Użytkownik "news.hku.hk" <bi******@hkusua.hku.hk> napisał w wiadomo¶ci
news:40******@newsgate.hku.hk...
suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??


Not in the standard. You'll have to do it yourself. I believe it is quite
simple.

regards,
Marcin

Jul 22 '05 #2
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40******@newsgate.hku.hk...
suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??


You could look up facets, and check out Dinkumware.
Jul 22 '05 #3


I've done this before. I've adapted it for the purpose of this post, but
here goes (Check for bugs before you use it!):
char szBuffer[30]; //Global Variable

char* GenerateString(unsigned long);

int main(void)
{
cout << GenerateString(12345);
}
char* GenerateString(unsigned long Numbr)
{
szBuffer[29] = '\0';

char* pChar = &m_szBuffer[28];

unsigned long char_counter = 0;
unsigned long comma_counter = 0;
unsigned long nResult; //will be initialized
unsigned long nTempValue = Numbr;

for(;;)
{
nResult = 0;

//I used to use the method below, but it's FAR too slow
/*
while ( nTempValue >= 10 )
{
nTempValue -= 10;
nResult += 1;
}*/

nResult = nTempValue / 10;
nTempValue %= 10;

*pChar = ( ( '0' ) + ( nTempValue ) );
pChar -= 1;
char_counter +=1;

nTempValue = nResult; //Very Important!

if (!nTempValue) break; //This line prevents ",345" being
generated

char_counter += 1;
if ( comma_counter == 3 )
{
*pChar = ',';
pChar -= 1;
char_counter += 1;
comma_counter = 0;
}
} //End of "For"

return pChar;

}
Hope that helps. Also, you may try incorporating it into a class ( I did! )
and also overide the ostream "<<" operator! That'd be dead handy. I'd post
the class I wrote for it but it had a very specific purpose other than just
genrating these strings.
-JKop
Jul 22 '05 #4
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:<40******@newsgate.hku.hk>...
suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??


Hi,
I am new to C++ too so my solution might not be very good. Anyway, I
will try something like:

void _display_number(int v, int n){
if(v >= 1000){
int r = v % 1000;
_display_number(v / 1000,n);
printf(",%03d",r); // how to translate that into
std::cout<<...?
}else{
printf("%s%d\n",n ? "-":"",v);
}
}

void display_number(int v){
_display_number(v < 0 ? -v : v,v < 0);
}

int main(int argc,char** argv){
display_number(35000);
display_number(600);
display_number(8765);
display_number(120000);
display_number(3800000);
display_number(-3800000);
}

The program prints something like:

35,000
600
8,765
120,000
3,800,000
-3,800,000

HTH. I am here to learn as well so if anyone can help me improve the
program, I am happy to see it. Thanks!
Jul 22 '05 #5

"JKop" <NU**@NULL.NULL> wrote in message
news:CU******************@news.indigo.ie...


I've done this before. I've adapted it for the purpose of this post, but here goes (Check for bugs before you use it!):
char szBuffer[30]; //Global Variable

char* GenerateString(unsigned long);

int main(void)
{
cout << GenerateString(12345);
}
char* GenerateString(unsigned long Numbr)
{
szBuffer[29] = '\0';

char* pChar = &m_szBuffer[28];

unsigned long char_counter = 0;
unsigned long comma_counter = 0;
unsigned long nResult; //will be initialized
unsigned long nTempValue = Numbr;

for(;;)
{
nResult = 0;

//I used to use the method below, but it's FAR too slow /*
while ( nTempValue >= 10 )
{
nTempValue -= 10;
nResult += 1;
}*/

nResult = nTempValue / 10;
nTempValue %= 10;

*pChar = ( ( '0' ) + ( nTempValue ) );
pChar -= 1;
char_counter +=1;

nTempValue = nResult; //Very Important!

if (!nTempValue) break; //This line prevents ",345" being generated

char_counter += 1;
if ( comma_counter == 3 )
{
*pChar = ',';
pChar -= 1;
char_counter += 1;
comma_counter = 0;
}
} //End of "For"

return pChar;

}
Hope that helps. Also, you may try incorporating it into a class ( I did! ) and also overide the ostream "<<" operator! That'd be dead handy. I'd post the class I wrote for it but it had a very specific purpose other than just genrating these strings.
-JKop


How about (untested code, and YMMV)

#include <iostream>
#include <locale>

int main()
{
using namespace std;
cout.imbue(locale("English"));
cout << 12345 << '\n';
}

;) JE
Jul 22 '05 #6
thanks for all of your's help
but i still can't grasp the idea of the algorithm
maybe could anyone explain in words(preferably with c++ codes) that how the
algorithm works ??
Thanks

"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message

news:<40******@newsgate.hku.hk>...
suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??


Hi,
I am new to C++ too so my solution might not be very good. Anyway, I
will try something like:

void _display_number(int v, int n){
if(v >= 1000){
int r = v % 1000;
_display_number(v / 1000,n);
printf(",%03d",r); // how to translate that into
std::cout<<...?
}else{
printf("%s%d\n",n ? "-":"",v);
}
}

void display_number(int v){
_display_number(v < 0 ? -v : v,v < 0);
}

int main(int argc,char** argv){
display_number(35000);
display_number(600);
display_number(8765);
display_number(120000);
display_number(3800000);
display_number(-3800000);
}

The program prints something like:

35,000
600
8,765
120,000
3,800,000
-3,800,000

HTH. I am here to learn as well so if anyone can help me improve the
program, I am happy to see it. Thanks!

Jul 22 '05 #7
On Mon, 26 Apr 2004 21:37:07 +0800, "news.hku.hk"
<bi******@hkusua.hku.hk> wrote:
suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??

Yet another alternative. This one converts the number into a string
and puts the commas into the string. Not necessarily better, just
different.

rossum
#include <string>
#include <sstream>
#include <iostream>
#include <cstdlib>

std::string comma(int num) {
// Deal with negatives
if(num < 0) { return "-" + comma(std::abs(num)); }

// Convert number to string
std::stringstream ss;
ss << num;
std::string num_string = ss.str();

// Insert a comma every third position
for (int i = num_string.length() - 3; i > 0; i -= 3) {
num_string.insert(i, 1, ',');
} // end for

return num_string;
} // end comma()

//---------------------------

int main() {
std::cout << comma(-3800000) << '\n'
<< comma(-120000) << '\n'
<< comma(-35000) << '\n'
<< comma(-8765) << '\n'
<< comma(-600) << '\n'
<< comma(-50) << '\n'
<< comma(-4) << '\n'
<< comma(0) << '\n'
<< comma(4) << '\n'
<< comma(50) << '\n'
<< comma(600) << '\n'
<< comma(8765) << '\n'
<< comma(35000) << '\n'
<< comma(120000) << '\n'
<< comma(3800000) << std::endl;
return EXIT_SUCCESS;
} // end main()

--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #8
"John Ericson" <je************@pacbell.net> wrote in message news:Eijjc.1921

This is a good solution. Not sure why everyone is re-inventing the wheel.
using namespace std;
cout.imbue(locale("English"));
cout << 12345 << '\n';


But, is there a locale named "English"?
Jul 22 '05 #9
Thanks, i see your point
but how to return to the original state??
because i just want to insert the comma in someline while later i just want
to display ordinary numbers.

also, from your quotation, if i add one more cout line after cout << 12345
<<'\n';

i.e. cout << 543532 << '\n';
there will be runtime error.......what's up ??

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:vN*********************@bgtnsc04-news.ops.worldnet.att.net...
"John Ericson" <je************@pacbell.net> wrote in message news:Eijjc.1921
This is a good solution. Not sure why everyone is re-inventing the wheel.
using namespace std;
cout.imbue(locale("English"));
cout << 12345 << '\n';


But, is there a locale named "English"?

Jul 22 '05 #10
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:408f9591
Thanks, i see your point
but how to return to the original state??
because i just want to insert the comma in someline while later i just want to display ordinary numbers.
Haven't tried it yet, but approaches I suggest is: (1) use 2 locales, and
imbue cout with first locale when you want num_put to put comma symbols and
default locale when you don't want comma symbols, (2) create a new type
Amount for which operator<<(ostream&, Amount) uses num_put for Amount
objects and this num_put uses comma symbols. See my response in the thread
with subject "convert hex to oct" for this imbue, use_facet, etc. But there
was a C++ magazine that had and maybe still has lots of articles on these
ideas, and I think the articles were by Plauger, so maybe you can browse for
that too.
also, from your quotation, if i add one more cout line after cout << 12345
<<'\n';

i.e. cout << 543532 << '\n';
there will be runtime error.......what's up ??
No error. I don't know what quotation you're talking about.

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:vN*********************@bgtnsc04-news.ops.worldnet.att.net...
"John Ericson" <je************@pacbell.net> wrote in message

news:Eijjc.1921

This is a good solution. Not sure why everyone is re-inventing the wheel.
using namespace std;
cout.imbue(locale("English"));
cout << 12345 << '\n';


But, is there a locale named "English"?


Jul 22 '05 #11
news.hku.hk wrote:
Thanks, i see your point
but how to return to the original state??
because i just want to insert the comma in someline while later i
just want to display ordinary numbers.


cout.imbue(locale("C"));

<snip>
cout.imbue(locale("English"));
<snip> But, is there a locale named "English"?


Since the sample works fine for me, I would assume there is. At least for my
compiler.
Why not try it and see with your compiler?

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at baesystems.com X
XXXXXXXXXXXXXXXXXXXXXXXXX

Jul 22 '05 #12
"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message news:<40******@newsgate.hku.hk>...
suppose i have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and i want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

what should i do ?? any good function to do this ??


You might want to look at commafmt.c at www.snippets.org.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #13
news.hku.hk wrote:
Suppose that I have:

int price1 = 35000;
int price2 = 600;
int price3 = 8765;
int price4 = 120000;
int price5 = 3800000;

and that I want to output to screen the following:

35,000
600
8,765
120,000
3,800,000

What should I do? Are there any good functions to do this?
cat main.cc #include <iostream>
#include <iomanip>

class Price {
private:
// representation
int I;
public:
// functions
friend
std::ostream& operator<<(std::ostream& os, const Price& i);
// operators
// implicit
operator int(void) const { return I;}
operator int(void) { return I;}
// constructors
Price(int i = 0): I(i) { }
};

inline
std::ostream& operator<<(std::ostream& os, const Price& i) {
int q = (int)i/1000;
int r = (int)i%1000;
if (0 < q)
os << Price(q) << ','
<< std::setfill('0') << std::setw(3) << r;
else
os << r;
return os;
}

int main(int argc, char* argv[]) {
const
int price[] = {35000, 600, 8765, 120000, 3800000};
const
size_t n = sizeof(price)/sizeof(price[0]);
for (size_t j = 0; j < n; ++j)
std::cout << Price(price[j]) << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main

35,000
600
8,765
120,000
3,800,000

Jul 22 '05 #14
On 26 Apr 2004 12:30:35 -0700, pe********@yahoo.com (pembed2003)
wrote:

[snip OP]

Hi,
I am new to C++ too so my solution might not be very good. Anyway, I
will try something like:
Show what headers you are including. Code should be compilable as is,
just cut and paste.
void _display_number(int v, int n){ Avoid leading underscores, they are used internally by compilers so if
you pick the wrong name you will get a compilation error. Since
compilers differ this might also make your code non-portable.
Trailing underscores are safer and hence better. So change
_display_number() to display_number_().

You have a good self explanatory name for the function, but you use
short cryptic names for the two parameters. Better to change v to
value and n to is_negative so they are more self explanatory.

The second parameter is a yes/no flag so it is better to declare is as
a bool which gives a better indication of its purpose. It is possible
to eliminate this parameter by taking a slightly different approach to
the recursion.
if(v >= 1000){ Put a space between the if and the open bracket: if (value >= 1000) {.
This is to differentiate it from function calls. ^--added space
int r = v % 1000; Another short cryptic variable name. Change it to something
meaningful.
_display_number(v / 1000,n); Better to put a space after the comma which separates the parameters.
printf(",%03d",r); // how to translate that into
std::cout<<...? Better to put a space after the second comma which separates the
parameters.

For cout look up the I/O manipulators.
}else{
printf("%s%d\n",n ? "-":"",v); The ternary operator ?: can make for unreadable code. Better to use
it sparingly. If you do use it then put spaces round both the '?' and
the ':' for clarity. Again spaces after any commas that separate
parameters are easier to read.

Are you really sure about that '\n' there? I hope that you
cut-and-pasted this rather than typed it from scratch.
}
}

void display_number(int v){
_display_number(v < 0 ? -v : v,v < 0); Where the ternary operator has a boolean expression it is better to
put the expression in brackets: (v < 0) ? -v : v to improve
readability. Again put a space after the parameter separating comma.}

int main(int argc,char** argv){
display_number(35000);
display_number(600);
display_number(8765);
display_number(120000);
display_number(3800000);
display_number(-3800000);
Main does not require a return value, but it is better style to
include it explicitly. I tend to use the EXIT_SUCCESS and
EXIT_FAILURE macros.
}

The program prints something like:

35,000
600
8,765
120,000
3,800,000
-3,800,000

HTH. I am here to learn as well so if anyone can help me improve the
program, I am happy to see it. Thanks!


Hope you find that useful.

rossum
--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #15
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:vN*********************@bgtnsc04-news.ops.worldnet.att.net...
"John Ericson" <je************@pacbell.net> wrote in message news:Eijjc.1921
This is a good solution. Not sure why everyone is re-inventing the wheel.
using namespace std;
cout.imbue(locale("English"));
cout << 12345 << '\n';


But, is there a locale named "English"?


;) No guarantees there - on my system there is (Win XP,
VC++ 2003). I've got a few books to go before hitting Langer
and Kreft's "Standard C++ IOStreams and Locales," but I'm
looking forward to what looks like it's going to be a good
read.
- -
Best regards, JE
Jul 22 '05 #16
"Joe Hotchkiss" <no****@baesystems.com> wrote in message news:408fee36
But, is there a locale named "English"?

Since the sample works fine for me, I would assume there is. At least for

my compiler.
Why not try it and see with your compiler?


Fair enough, but the standard does not appear to mention this.
Jul 22 '05 #17
rossum <ro******@coldmail.com> wrote in message news:<eb********************************@4ax.com>. ..
On 26 Apr 2004 12:30:35 -0700, pe********@yahoo.com (pembed2003)
wrote:

[snip OP]

Hi,
I am new to C++ too so my solution might not be very good. Anyway, I
will try something like:
Show what headers you are including. Code should be compilable as is,
just cut and paste.


Is this a rule for this newsgroup to always include only complete
program as an answer and must be compilable by cut and paste? Just
curious, new to this newsgroup...
void _display_number(int v, int n){

Avoid leading underscores, they are used internally by compilers so if
you pick the wrong name you will get a compilation error. Since
compilers differ this might also make your code non-portable.
Trailing underscores are safer and hence better. So change
_display_number() to display_number_().


Great. I always forget that.

You have a good self explanatory name for the function, but you use
short cryptic names for the two parameters. Better to change v to
value and n to is_negative so they are more self explanatory.

Great. Good advice. Will most likely do but maybe not neccessary in
this case where the parameters are already pretty clear (?)

I don't feel like:

display_number_(int number_to_display_with_comma, bool is_negative)

especially:

display_number_(int v, int n)

where the function name itself already communicate back to the user
that the function is make to display a number. Maybe the second
parameter isn't so clear as what it does...
The second parameter is a yes/no flag so it is better to declare is as
a bool which gives a better indication of its purpose. It is possible
to eliminate this parameter by taking a slightly different approach to
the recursion.
Yes. Totally agree. Can you show me how?
if(v >= 1000){ Put a space between the if and the open bracket: if (value >= 1000) {.
This is to differentiate it from function calls. ^--added space


Sorry. This one I might have to disagree. I think it's a personal
thing and I never found:

if(v >= 1000){

to be less readable than:

if (v >= 1000) {

Since in this case, the compiler doesn't care. I will stick with my
way.
int r = v % 1000;

Another short cryptic variable name. Change it to something
meaningful.


More meaningful variable name is usually a good idea but just looking
at the right side of assignment, one can pretty much firgure out what
r is, right?

I mean, do you seriously think:

int remainder = v % 1000;

will communicate so much better than:

int r = v % 1000;

All the action is on the right hand side AND it is so short.
_display_number(v / 1000,n); Better to put a space after the comma which separates the parameters.


Agree.
printf(",%03d",r); // how to translate that into
std::cout<<...? Better to put a space after the second comma which separates the
parameters.


Agree.

For cout look up the I/O manipulators.

Sure.
}else{
printf("%s%d\n",n ? "-":"",v); The ternary operator ?: can make for unreadable code. Better to use
it sparingly.


I think I use it only twice in my code above. Does it count as
sparingly?

If you do use it then put spaces round both the '?' and
the ':' for clarity. Again spaces after any commas that separate
parameters are easier to read.

Make sense.

Are you really sure about that '\n' there? I hope that you
cut-and-pasted this rather than typed it from scratch.

No, it's a bug. I should have state that so OP won't make the same
mistake.
}
}

void display_number(int v){
_display_number(v < 0 ? -v : v,v < 0);

Where the ternary operator has a boolean expression it is better to
put the expression in brackets: (v < 0) ? -v : v to improve
readability. Again put a space after the parameter separating comma.
}
I never try that but looking at your code, I think it does improve
readability so I will remember to do that next time.

int main(int argc,char** argv){
display_number(35000);
display_number(600);
display_number(8765);
display_number(120000);
display_number(3800000);
display_number(-3800000);


Main does not require a return value, but it is better style to
include it explicitly. I tend to use the EXIT_SUCCESS and
EXIT_FAILURE macros.


Agree.
}

The program prints something like:

35,000
600
8,765
120,000
3,800,000
-3,800,000

HTH. I am here to learn as well so if anyone can help me improve the
program, I am happy to see it. Thanks!


Hope you find that useful.


Very useful.
rossum


I respect your comments because it will make me a better programmer.
Thanks!
Jul 22 '05 #18
>
Is this a rule for this newsgroup to always include only complete
program as an answer and must be compilable by cut and paste? Just
curious, new to this newsgroup...


Its not a rule, but its obviously a good idea if you want to get help.

If you submit partial code, then there is a good chance that people won't be
able to answer the question because you missed out some vital piece of
information. Its amazing how many posters, despite not understand why their
code doesn't work are convinced that they know where the mistake is in their
code. Eventually when you get them to post more code, you often find that
the real error was in some part of the code they didn't originally post.

If you type in code rather than cut and paste, you are liable to get your
typing mistakes corrected instead of your real question answered. Given that
many newbies post to this group, its often hard to tell the difference
between a typing mistake and a genuine misunderstanding.

john
Jul 22 '05 #19
Just a few notes from the view of someone who has implemented this stuff...

"news.hku.hk" <bi******@hkusua.hku.hk> wrote:
but how to return to the original state??
If you only need an occasional switch between locales, you can just switch
them by calling 'imbue()', eg.:

std::locale english("English");
std::cout.imbue(english);
// ...
std::cout.imbue(std::locale());

The default constructor just uses a copy of the global locale (internally,
locales are a reference counted collection of facets and copying them is
fast). However, the 'imbue()' operation itself is relatively expensive.
In particular, it is reasonable for streams to cache quite a few values
obtained from a locale to improve the overall performance. As a result,
you might consider to use a separate stream writing to the same destination
but with a different locale, eg.:

std::ostream ecout(std::cout.rdbuf());
ecout.imbue(std::locale("English"));
ecout << "english: " << 10000 << "\n";
std::cout << "normal: " << 10000 << "\n";

Since creation of streams themselves is also relatively expensive, you
might want to keep the additional stream around rather than creating it
each time around (which would probably be more expensive than switching
locales).

Also, the names for the named locales are not specified by the standard.
Of course, you can figure out which locales are around when porting your
application to another compiler/library but for portable code you would
probably create an own locale with a specialized 'std::numpunct<char>'
facet. Here is a brief code example (entirely untested, however...):

#include <locale>
class mynumpunct:
public std::numpunct<char> {
std::string do_grouping() const {
char group[] = { 3, 0 };
return group;
}
};
int main() {
std::locale myloc(std::locale(), new mynumpunct);
std::cout.imbue(myloc);
// ...
}

Actually, you can do funny things with the grouping: it does not have to
be the same amount of characters every time! For example, if 'group'
would be initialized like

char group[] = { 1, 2, 3, 0 };

you would get "1,234,56,7" when printing "1234567" (as an integer, that is;
grouping is not applied to strings, of course).
also, from your quotation, if i add one more cout line after cout << 12345
<<'\n';

i.e. cout << 543532 << '\n';
there will be runtime error.......what's up ??


This seems to be an error somewhere in the code you use: there should be
no problem using 'std::cout' even with a modified locale.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #20
"John Harrison" <jo*************@hotmail.com> wrote in message news:<c6************@ID-196037.news.uni-berlin.de>...

Is this a rule for this newsgroup to always include only complete
program as an answer and must be compilable by cut and paste? Just
curious, new to this newsgroup...


Its not a rule, but its obviously a good idea if you want to get help.

If you submit partial code, then there is a good chance that people won't be
able to answer the question because you missed out some vital piece of
information. Its amazing how many posters, despite not understand why their
code doesn't work are convinced that they know where the mistake is in their
code. Eventually when you get them to post more code, you often find that
the real error was in some part of the code they didn't originally post.


Hi John,
Yes I understand that but my confusion is not when people asking
question, it's when people post an answer to a question. If OP asks a
solution for a function to display a number in a certain format, do
you normally give OP a whole program or just the function that does
it?

Thanks!
Jul 22 '05 #21

"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote in message

news:<c6************@ID-196037.news.uni-berlin.de>...

Is this a rule for this newsgroup to always include only complete
program as an answer and must be compilable by cut and paste? Just
curious, new to this newsgroup...


Its not a rule, but its obviously a good idea if you want to get help.

If you submit partial code, then there is a good chance that people won't be able to answer the question because you missed out some vital piece of
information. Its amazing how many posters, despite not understand why their code doesn't work are convinced that they know where the mistake is in their code. Eventually when you get them to post more code, you often find that the real error was in some part of the code they didn't originally post.


Hi John,
Yes I understand that but my confusion is not when people asking
question, it's when people post an answer to a question. If OP asks a
solution for a function to display a number in a certain format, do
you normally give OP a whole program or just the function that does
it?

Thanks!


I think it depends a lot on the complexity of the answer. If it was
something I was pretty sure I could type in without any serious error then I
would just post it and say 'untested code'. On the other hand if it was
something I had to compile and test to be confident I was right, then I
would do that and cut and paste the whole program. The former is more common
than the later for me and the sort of questions I answer. Not sure about
others tho'.

You can be pretty sure that if you do post code in an answer and it's wrong
it will get picked up on, and if you didn't say 'untested code' you could
easily get flamed. A lot of people (like me for instance) try to get newbies
to post complete compilable programs (they rarely listen) so it's a bit
inconsistent to then post incomplete uncompilable answers.

john
Jul 22 '05 #22
On 28 Apr 2004 22:52:58 -0700, pe********@yahoo.com (pembed2003)
wrote:
rossum <ro******@coldmail.com> wrote in message news:<eb********************************@4ax.com>. ..

[snip]
>void _display_number(int v, int n){
[snip] The second parameter is a yes/no flag so it is better to declare is as
a bool which gives a better indication of its purpose. It is possible
to eliminate this parameter by taking a slightly different approach to
the recursion.


Yes. Totally agree. Can you show me how?


I assume that you know how to declare a bool and are asking about
eliminating the second parameter.

I could be cruel and just say that you will learn better by working
that sort of thing out for yourself, however I will give you three
hints:

1 Since the negative sign always comes at the start of the output then
it is possible to deal with it before starting work on the rest of the
number.

2 The negative sign has no effect on the distribution of commas, so
once the sign has been dealt with then printing a negative number is
going to be identical to printing the corresponding positive.

3 If you are still stuck then it may be useful to look at the various
bits of code posted in this thread for ideas.

Once you have worked that out, you can think about something similar
for the '\n' that was causing problems.

rossum
--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #23
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
std::locale myloc(std::locale(), new mynumpunct);


As an aside, how do you create a locale containing many facets? Example of
what I want:

std::locale myloc(std::locale(), new mynumpunct, new myspace);
Jul 22 '05 #24

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

Similar topics

9
by: Brian Roberts | last post by:
I have a command line Python program that sometimes takes a bit (several minutes) to run. I want to provide an optional method for an impatient user (me!) to check the status of the program. The...
0
by: pembed2003 | last post by:
"news.hku.hk" <billychu@hkusua.hku.hk> wrote in message news:<408e8476$2@newsgate.hku.hk>... > thanks for all of your's help > but i still can't grasp the idea of the algorithm > maybe could...
2
by: C CORDON | last post by:
I have read the table field information into a Byte(), so...now how can I display that picture in a specific place in a web form where there is more information...I cannot use just Response.Write...
8
by: nineteen | last post by:
for example: 16/5: 3 -------- 5)16 15 -------- 1 the user input the dividend and divisor,the program should display the upright formula like the example. and , the program must use recursive...
2
by: dbfrey | last post by:
This is the oddest thing I've ever seen. We have an aspx page with some user controls, one is a main, another is a status, which has an edit button. on the main control, we have some dropdowns...
0
by: gggram2000 | last post by:
Hi there, I'm using microsoft visual studio 2005, with c# code. I recently downloaded GemBox which is a spreadsheet for excel. I can pass parameters through textboxes, comboboxes, labels...etc...
16
by: RAZZ | last post by:
Hi, When I login to my site initially it load index.php by default. We can't see the page name index.php displayed in URL. But when I click to some link on this page it will display the whole...
2
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
I have a character(15) column filled with numbers, in my SQL database. I need it to display like it does in Access when I set a format for the column, like "000-000-000-000-000". How do I display...
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
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...

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.