i cant get an perfect alignment on my decimals, my goal is to have an align
decimals on any of my input and out put.
thats my teacher saids, im beginner here in C++ and im using dev c++, please
help me to my project and thx, guys
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I would suggest that you sprintf the value to a string using "%.2f". The
return value of sprintf is the number of characters output (i.e. the length of
the string). You can then use this value it calculate the number of spaces to
output before finally outputting the string. I suggest that you create a
function to do this:
void printdecimalalign( float value, int decpos ) ;
where decpos _is the number fo characters _relative to the current cursor
position to place the decimal, and value is the number to print.
Some other points:
User code tags to post code.
Your screenshot uses different user input that your example. Also it is an
unreasonable value for a single precision float, which is accurate to only 6
significant figures.
None of your code requires <math.h>. <conio.h> and getch() are not standard;
use caution.
You should explicitly declare the return type of main(), prefereably as int.
Your code does not compile without warnings. Use teh f suffix on float literal
constants to force type agreement e.g:
VAT = .12f * Amount;
or of course use a double. There is little point in using a float; it may have
been significant on ancient systems with no FPU and slow memory, or where
computational speed is critical, but not in this case.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sample of expected output:
Enter Sales Amount: 274.25
VAT: 32.91
LGT: 9.21
Total Amount Due: 316.37
this is my program:
MY CODE:
include<stdio.h>
include<conio.h>
include<math.h>
main()
{
float VAT, LGT, TOTAL, Amount;
printf("Enter Sales Amount: \t ");
scanf("%f", &Amount);
VAT = .12Amount;
LGT = .03Amount;
TOTAL = Amount+LGT+VAT;
printf("\nVAT: \t%6.2f", VAT);
printf("\nLGT: \t%6.2f", LGT);
printf("\n\nTotal Amount Due:\t%6.2f", TOTAL);
getch();
}
i cant get an perfect alignment on my decimals, my goal is to have an align
decimals on any of my input and out put.
thats my teacher saids, im beginner here in C++ and im using dev c++, please
help me to my project and thx, guys
Enter Sales Amount: 274.25
VAT: 32.91
LGT: 9.21
Total Amount Due: 316.37
ooopps this is my sample expected out put:
Enter Sales Amount: 274.25
VAT: 32.91
LGT: 9.21
Total Amount Due: 316.37
Enter Sales Amount:----274.25
VAT:----------------------------32.91
LGT:-----------------------------9.21
Total Amount Due:-------316.37
sorry , THIS SHOULD BE THE EXAMPLE OUTPUT>..
I would suggest that you sprintf the value to a string using "%.2f". The
return value of sprintf is the number of characters output (i.e. the length of
the string). You can then use this value it calculate the number of spaces to
output before finally outputting the string. I suggest that you create a
function to do this:
where decpos _is the number fo characters _relative to the current cursor
position to place the decimal, and value is the number to print.
Some other points:
User code tags to post code.
Your screenshot uses different user input that your example. Also it is an
unreasonable value for a single precision float, which is accurate to only 6
significant figures.
None of your code requires <math.h>. <conio.h> and getch() are not standard;
use caution.
You should explicitly declare the return type of main(), prefereably as int.
Your code does not compile without warnings. Use teh f suffix on float literal
constants to force type agreement e.g:
or of course use a double. There is little point in using a float; it may have
been significant on ancient systems with no FPU and slow memory, or where
computational speed is critical, but not in this case.
... using code tags for your "example output" would be a good idea too!
... that is to say did you mean this?: