The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Line up numbers
Discuss Line up numbers in the C Programming forum on Dev Shed. Line up numbers C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 10th, 2012, 12:30 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
|
|
|
Line up numbers
How do I line up the numbers a user inputs into a column in such a way that the place values are guaranteed to align, regardless of the size of the integer?
I get:
5
145
7
45
I want:
_ _5
145
__7
etc
|

November 10th, 2012, 12:37 AM
|
 |
Contributed User
|
|
|
|
Try say in C
printf("%5d\n", number);
Or in C++
cout << setw(5) << number << endl;
|

November 10th, 2012, 10:14 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem Try say in C
printf("%5d\n", number);
Or in C++
cout << setw(5) << number << endl; |
Nope, doesn't work.
|

November 10th, 2012, 11:14 PM
|
 |
Contributed User
|
|
|
|
|

November 10th, 2012, 11:56 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem |
It doesn't change the spacing depending on the number of places. Whenever the numbers print, I get:
1
100
45
I need:
__1
100
_45
with __representing spaces. The %5d changes the spacing for all of the numbers, so they're still off balance. The only way I can conceive of doing it is to use a ton of if-statements and printf statements.
Remember, all values are integers.
|

November 11th, 2012, 12:41 AM
|
 |
Contributed User
|
|
|
|
|
Perhaps you could remember to post your code.
"It doesn't work" is not a useful description of your problem.
|

November 11th, 2012, 12:45 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem Perhaps you could remember to post your code.
"It doesn't work" is not a useful description of your problem. |
Here you go!
Code:
for(i=0;i<size;i++)
{
printf(" %5d %5d\n",x[i],s[i]);
}
printf("\nValue Count\n");
for(i=0;i<d+1;i++)
{
printf(" %5d - %5d\n",v[i],c[i]);
}
|

November 11th, 2012, 02:04 AM
|
 |
Contributed User
|
|
|
|
No idea - it works for me.
Code:
$ cat foo.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int x[10] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int i;
for ( i = 0 ; i < 10 ; i++ ) {
printf("%5d\n", x[i] );
}
return 0;
}
$ gcc foo.c
$ ./a.out
1
2
5
10
20
50
100
200
500
1000
|

November 11th, 2012, 04:07 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 22
  
Time spent in forums: 3 h 57 m 34 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem No idea - it works for me.
Code:
$ cat foo.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int x[10] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int i;
for ( i = 0 ; i < 10 ; i++ ) {
printf("%5d\n", x[i] );
}
return 0;
}
$ gcc foo.c
$ ./a.out
1
2
5
10
20
50
100
200
500
1000
|
Could the problem be that I'm trying to print out two columns?
I'm trying to get both columns, minus the "__" to be:
1 __1
5 __5
10 __14
20 __18
50 __145
100 etc
200
500
1000
|

November 11th, 2012, 06:27 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
One way to handle this is to check the return value of printf(). It returns the number of characters that it printed. That way, you can figure out how many spaces to print before printing the second number.
Code:
#define INDENT_WIDTH 10
int i, idx;
idx = printf("%d", first_number);
/* Now print as many spaces as needed to get to the next column */
for (i = idx; i < INDENT_WIDTH; i++) {
printf(" ");
}
/* Now print the second number */
printf("%d\n", second_number);
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

November 11th, 2012, 08:40 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by Astrodude Could the problem be that I'm trying to print out two columns? | I'm confused. Multiple columns should line up as long as you don't exceed the field width. I saw your earlier post where you posted some code, but I still don't see what the problem is.
Code:
% cat test.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int x[10] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int i;
for ( i = 0 ; i < 10 ; i++ ) {
printf("%5d %5d %5d\n", x[i], x[i]*3, x[i]*7);
}
return 0;
}
% gcc -ansi -Wall -pedantic test.c
% ./a.out
1 3 7
2 6 14
5 15 35
10 30 70
20 60 140
50 150 350
100 300 700
200 600 1400
500 1500 3500
1000 3000 7000
|

November 11th, 2012, 11:36 PM
|
 |
Contributed User
|
|
|
|
|
> Could the problem be that I'm trying to print out two columns?
No, the problem is you can't be bothered to add a few more lines to your test code to make it a COMPLETE program we can run, and see the problem that you're seeing.
So instead there have been 3 separate guesses at trying to figure out what possible reason "%5d" isn't lining up for you.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|