473,493 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help With Character Output for Data table.

Hello Everyone..
I wrote a function called "totalsales" for a short business program I
wrote, and I can't keep the decimal point aligned in the colums in the
printf output statement. The data comes from an array
sales[4][5], which is passed to the function .

like ---- Tracey John Terry
0.00 10.00 51.00
0.00 12.50 50.00

Usually what it prints is like this:

like ---- Tracey John Terry
100.00 10.00 51.00
0.00 12.50 50.00

I tried simple code like

if (subtototal 10) {
printf("\b");
}
if (subtotal 100) {
printf("\b\b");
}

Sometimes it works, but I can't find how make all the data line up,
cause the data can vary from 2 digits with the decimal point or up to 5
digits with the decimal point.

Does anyone know how to code this?

Thanks for all your help
Neil.
..

Jan 10 '07 #1
8 1774
On 9 Jan 2007 17:34:01 -0800, "Neil" <ne*********@hotmail.comwrote:
>Hello Everyone..
I wrote a function called "totalsales" for a short business program I
wrote, and I can't keep the decimal point aligned in the colums in the
printf output statement. The data comes from an array
sales[4][5], which is passed to the function .

like ---- Tracey John Terry
0.00 10.00 51.00
0.00 12.50 50.00

Usually what it prints is like this:

like ---- Tracey John Terry
100.00 10.00 51.00
0.00 12.50 50.00

I tried simple code like

if (subtototal 10) {
printf("\b");
}
if (subtotal 100) {
printf("\b\b");
}

Sometimes it works, but I can't find how make all the data line up,
cause the data can vary from 2 digits with the decimal point or up to 5
digits with the decimal point.

Does anyone know how to code this?
The conversion specifiers support numerous "modifiers". A - can be
used to left adjust the converted value. A minimum field width can be
used to pad with blanks. A precision can be used to insure the
correct number of digits after the decimal point. Check your
references.
Remove del for email
Jan 10 '07 #2
Neil wrote:
Hello Everyone..
I wrote a function called "totalsales" for a short business program I
wrote, and I can't keep the decimal point aligned in the colums in the
printf output statement. The data comes from an array
sales[4][5], which is passed to the function .

like ---- Tracey John Terry
0.00 10.00 51.00
0.00 12.50 50.00

Usually what it prints is like this:

like ---- Tracey John Terry
100.00 10.00 51.00
0.00 12.50 50.00

I tried simple code like

if (subtototal 10) {
printf("\b");
}
if (subtotal 100) {
printf("\b\b");
}

Sometimes it works, but I can't find how make all the data line up,
cause the data can vary from 2 digits with the decimal point or up to 5
digits with the decimal point.

Does anyone know how to code this?

Thanks for all your help
Neil.
The '\b' stuff really won't work for you. Think of printing a value to
some decimals within a field of some width. How big will the largest
number be? Less than 1 million? That's 999999.99 or less. That maximum
will take nine print positions to 'print'. Assume..

printf("%9.2f\n", f);

Various values of f will print ..

100000.00
10000.00
1000.00
100.00

...etc. You are not supposed to figure this out. How could you? You are
supposed to read about it in your C reference, or your man or info pages.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 10 '07 #3
"Neil" <ne*********@hotmail.comwrote in message
news:11*********************@p59g2000hsd.googlegro ups.com...
Hello Everyone..
I wrote a function called "totalsales" for a short business program I
wrote, and I can't keep the decimal point aligned in the colums in the
printf output statement. The data comes from an array
sales[4][5], which is passed to the function .

like ---- Tracey John Terry
0.00 10.00 51.00
0.00 12.50 50.00

Usually what it prints is like this:

like ---- Tracey John Terry
100.00 10.00 51.00
0.00 12.50 50.00

I tried simple code like

if (subtototal 10) {
printf("\b");
}
if (subtotal 100) {
printf("\b\b");
}

Sometimes it works, but I can't find how make all the data line up,
cause the data can vary from 2 digits with the decimal point or up to 5
digits with the decimal point.
You might try this URL:

http://www.die.net/doc/linux/man/man3/printf.3.html

You might also try other URLs that come up by searching by:

"man 3 printf"

"man printf"

"printf format specifier"

etc.

I don't often work with floating-point numbers and I'm too lazy to look
anything up, but if "f" specifies a floating-point number, something like
"10.2f" is likely to work.
Jan 10 '07 #4
Neil wrote:
>
I wrote a function called "totalsales" for a short business program
I wrote, and I can't keep the decimal point aligned in the colums
in the printf output statement. The data comes from an array
sales[4][5], which is passed to the function .
You can try variations on the following, which is slightly more
accurate than using fprintf directly. Note that the following is
expected to display values with exponents:

/* format doubles and align output */
/* Public domain, by C.B. Falconer */

#include <stdio.h>

#define dformat(r, d, f) fdformat(stdout, r, d, f)

/* output r in field with fpart digits after dp */
/* At least 1 blank before and after the output */
/* Returns neg on param error, else field used */
/* Allows for exponents from -999 to +999. */
/* Too small fields are automatically expanded */
int fdformat(FILE *fp, double r, int fpart, int field)
{
#define CPMAX 100
char cp[CPMAX];
int n, spacebefore, spaceafter, minchars;

/* Protect against evil arguments */
if (fpart < 1) fpart = 1;
if (r < 0.0) minchars = 9;
else minchars = 8;
if (field < (fpart + minchars)) field = fpart + minchars;
if (field >= CPMAX) return -1;

/* Try the effect of "%.*g" and "%.*e" below */
n = sprintf(cp, "%.*e", fpart, r);
if (n < 0) return n;
spacebefore = field - minchars - fpart;
spaceafter = field - spacebefore - n;
return fprintf(fp, "%*c%s%*c",
spacebefore, ' ', cp, spaceafter, ' ');
} /* fdformat */

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 10 '07 #5

"Joe Wright" <jo********@comcast.netwrote in message
news:66******************************@comcast.com. ..
Neil wrote:
>Hello Everyone..
I wrote a function called "totalsales" for a short business program I
wrote, and I can't keep the decimal point aligned in the colums in the
printf output statement. The data comes from an array
sales[4][5], which is passed to the function .

like ---- Tracey John Terry
0.00 10.00 51.00
0.00 12.50 50.00

Usually what it prints is like this:

like ---- Tracey John Terry
100.00 10.00 51.00
0.00 12.50 50.00

I tried simple code like

if (subtototal 10) {
printf("\b");
}
if (subtotal 100) {
printf("\b\b");
}

Sometimes it works, but I can't find how make all the data line up,
cause the data can vary from 2 digits with the decimal point or up to 5
digits with the decimal point.

Does anyone know how to code this?

Thanks for all your help
Neil.

The '\b' stuff really won't work for you. Think of printing a value to
some decimals within a field of some width. How big will the largest
number be? Less than 1 million? That's 999999.99 or less. That maximum
will take nine print positions to 'print'. Assume..
Joe, I think he's coming at C from fortran.
>
printf("%9.2f\n", f);

Various values of f will print ..

100000.00
10000.00
1000.00
100.00

..etc. You are not supposed to figure this out. How could you? You are
supposed to read about it in your C reference, or your man or info pages.
I find K&R2's treatment of formatted output vexing. Both fortran and c have
a write specification; it's quite similar. LS
Jan 10 '07 #6

Joe Wright wrote:
Neil wrote:
Hello Everyone..
I wrote a function called "totalsales" for a short business program I
wrote, and I can't keep the decimal point aligned in the colums in the
printf output statement. The data comes from an array
sales[4][5], which is passed to the function .

like ---- Tracey John Terry
0.00 10.00 51.00
0.00 12.50 50.00

Usually what it prints is like this:

like ---- Tracey John Terry
100.00 10.00 51.00
0.00 12.50 50.00

I tried simple code like

if (subtototal 10) {
printf("\b");
}
if (subtotal 100) {
printf("\b\b");
}

Sometimes it works, but I can't find how make all the data line up,
cause the data can vary from 2 digits with the decimal point or up to 5
digits with the decimal point.

Does anyone know how to code this?

Thanks for all your help
Neil.

The '\b' stuff really won't work for you. Think of printing a value to
some decimals within a field of some width. How big will the largest
number be? Less than 1 million? That's 999999.99 or less. That maximum
will take nine print positions to 'print'. Assume..

printf("%9.2f\n", f);

Various values of f will print ..

100000.00
10000.00
1000.00
100.00

..etc. You are not supposed to figure this out. How could you? You are
supposed to read about it in your C reference, or your man or info pages.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Allright Joe, I'll try that.

Thanks
Neil

Jan 12 '07 #7

David T. Ashley wrote:
"Neil" <ne*********@hotmail.comwrote in message
news:11*********************@p59g2000hsd.googlegro ups.com...
Hello Everyone..
I wrote a function called "totalsales" for a short business program I
wrote, and I can't keep the decimal point aligned in the colums in the
printf output statement. The data comes from an array
sales[4][5], which is passed to the function .

like ---- Tracey John Terry
0.00 10.00 51.00
0.00 12.50 50.00

Usually what it prints is like this:

like ---- Tracey John Terry
100.00 10.00 51.00
0.00 12.50 50.00

I tried simple code like

if (subtototal 10) {
printf("\b");
}
if (subtotal 100) {
printf("\b\b");
}

Sometimes it works, but I can't find how make all the data line up,
cause the data can vary from 2 digits with the decimal point or up to 5
digits with the decimal point.

You might try this URL:

http://www.die.net/doc/linux/man/man3/printf.3.html

You might also try other URLs that come up by searching by:

"man 3 printf"

"man printf"

"printf format specifier"

etc.

I don't often work with floating-point numbers and I'm too lazy to look
anything up, but if "f" specifies a floating-point number, something like
"10.2f" is likely to work.
Hey Dave!! Thanks for the info, Maybe now I can get it to work.

-Neil

Jan 12 '07 #8
On 11 Jan 2007 16:17:45 -0800, "Neil" <ne*********@hotmail.comwrote:
snip 50+ lines
>Allright Joe, I'll try that.

Thanks
Neil
Please trim your quotes. You don't need to quote an entire message
just to say thanks.
Remove del for email
Jan 12 '07 #9

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

Similar topics

31
14283
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
8
5448
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
9
2395
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
1
3692
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
1617
by: hari4485 | last post by:
I'm a C proram which is used to read datas from a table in text format and compare with another table in the same format. and we have to highlight the value or mark the value to show the...
2
2747
by: KhzQas | last post by:
Hello mates. I am taking my very first programming language (C Programming) and as for practice purposes, I surf sites and try out to make programs. I am having problems with the following C...
9
2307
by: Kurt Nesworthy | last post by:
Hi guys, Got a Visual Basic test to do by wedensday, but i dont have the faintest idea on how to do it. "A company has two schemes for paying monthly shift pay to its employees. Management are...
3
3316
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
1
6005
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
0
7119
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
6989
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
7157
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,...
0
7367
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
5453
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,...
1
4889
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...
0
4579
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...
0
1400
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 ...
1
644
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.