Console output alignment in C

luke127

The Ghost
Messages
868
Location
Australia
Hey guys,

So I've got a fun problem for you all. @Carnage, I know you're good with C#, but are you good with C? ;)

Code:
        printf("| Bakov Invoice (keep for internal billing)   |\n");
	printf("|---------------------------------------------|\n");
	// %-26s adds 26 spaces to trail the variable.
	printf("| Company ID      : %26s|\n", companyIdLookup);
	printf("| Company Name    : %26s|\n", companyNameLookup);
	printf("| Product ID      : %26s|\n", productCodeLookup);
	printf("| Product Name    : %26s|\n", productDescriptionLookup);
	printf("| Products Ordered: %26d|\n", numberOrdered);
	printf("| Tax             : $ %24.2f|\n", taxPaid);
	printf("| Invoice Amount  : $ %24.2f|\n", totalCost);
	printf("|---------------------------------------------|");

So there's the code, and here's what it currently spits out.

http://imgur.com/a/cTSkd

Circled is the problem. The massive amount of space between the $ signs, and the actual values. My research has concluded that I could probably do some wacky space insertion into the string using an extension method, but I'm not entirely sure how to go about this.

The reason why I need the spaces inserted is to keep the | characters aligned with eachother, but I still want the variables to line up nicely too :( Right now, the %24 is keeping the pipe characters aligned, but at the sacrifice of variable alignment.

Any ideas people?

Cheers,
Luke
 
Last edited:
Well I definitely ain't no carnage :p but here's something you could do that I've actually bothered to test this time lol, seems to work

char retstr[80];
sprintf(retstr, "$%.2f",UrTaxReturnFloat);
printf("| Invoice Amount : %26s|\n",retstr);

BfbarAO.png
 
Last edited:
Well I've almost never touched c but you can try a dirty shortcut. Before you print a price (I'll just use
), create a string like this:
String taxPaidDollar = "$"+convertToString(taxPaid);
and try printing that. The syntax is probably nothing like C but hopefully you know what I mean :p
 
Last edited:
@Luke: Soul's solution should work fine though. Curious: any reason you're falling back to C? Wanting to learn something new, or school assignments?

Well I've almost never touched c but you can try a dirty shortcut. Before you print a price (I'll just use ), create a string like this:
and try printing that. The syntax is probably nothing like C but hopefully you know what I mean :p

Silly you, thinking there's strings in C :p.
 
Well I definitely ain't no carnage :p but here's something you could do that I've actually bothered to test this time lol, seems to work

char retstr[80];
sprintf(retstr, "$%.2f",UrTaxReturnFloat);
printf("| Invoice Amount : %26s|\n",retstr);

BfbarAO.png

Thanks S0UL, I'll test it out shortly :)

I haven't done C in forever lol. Been like 4 or 5 years.

lel :p

Well I've almost never touched c but you can try a dirty shortcut. Before you print a price (I'll just use ), create a string like this:
and try printing that. The syntax is probably nothing like C but hopefully you know what I mean :p

M8 that looks like straight Java. Secondly, do you really think there's a convertToString() function in C? xD Like Carnage says below, STRINGS don't even have a native class in C XD Everything has to be done using character arrays if you want to manipulate strings. (Though this is quite fun, because it allows a lot more customisation than is normally possible in most higher level langauges, and on a side note, playing with memory in C is quite... amusing. Try making a string in C and not putting the \0 terminator on the end of it and see what happens :p

@Luke: Soul's solution should work fine though. Curious: any reason you're falling back to C? Wanting to learn something new, or school assignments?

Silly you, thinking there's strings in C :p.

lolol :p

University language. It is however interesting to learn C#'s parent, and certainly makes me appreciate C#'s higher functions.


EDIT: S0UL, for my future reference and understanding, what exactly is sprintf() doing there? It looks like it's taking a value, and printing it to a variable?

EDIT 2: Nevermind lmao, MSDN to the rescue:
https://msdn.microsoft.com/en-us/library/ms860374.aspx

It looks like sprintf() could be very useful in future... I could do a lot of formatting with this lol. My Marked Tute 2 will be far more difficult than this one was according to the lecturer, so I'm mentally bracing myself for a scenario where he's going to ask us to make a program where multiple companies could order multiple amounts of different products... that would be a friggin bitch to code.
 
Last edited:
Try making a string in C and not putting the \0 terminator on the end of it and see what happens :p
The good 'ol null terminator. Was a pain when I forgot to include that in the array length when creating a char array.
Speaking of... you may want to do char[81] on Soul's solution so you get the full 80 characters of the command prompt window.

University language. It is however interesting to learn C#'s parent, and certainly makes me appreciate C#'s higher functions.

Indeed - I started out with C++, and we had to start out doing things in C-style (that means no libraries for things like strings, stacks/queues, etc. besides the standard library and I/O lib). We had to make all of the "advanced" things from scratch, like stacks, queues, linked lists, binary trees, etc. Definitely gave me an appreciate for libraries, as well as a better understanding in how they work "under the hood". I still have all of my old source / programs from my classes from when I started to when I graduated.
 
Ohhh well bloody played Carnage xD I honestly didn't even think of that Char[81] thing xD

Edit 2: Ok so here's another question. I would like to format my currency in a more readable fashion. So for example, 50,000.00 or 5,000,000.00

The way to do this in most C compilers use to use the ' argument for printf(). Unfortunately, the only compiler which DOESN'T support this, is microsoft's C++ compiler, which happens to be the one I'm using in Visual Studio 2015. (rip me). So, anyone got any bright ideas on how we might be able to achieve this? :O
 
Last edited:
Back
Top Bottom