output spacing

Mar 7, 2010 at 2:42am
As a project for school I am doing a simple fraction calculater. However, I have decided to go a little further with it and have it reduce the fractions for me

currently i have the expression output on one line then the reduced answer on the secdond, for example:


4/8 * 1/2 = 4/16
= 1/4

is there a way to format my output so the "=" lines up everytime as if you were to write out the expression on paper?

ps. the output for the first expression is as follows:

cout<<f.Numerator_A<<"/"<<f.Denominator_A
<<" + "<<f.Numerator_B<<"/"<<f.Denominator_B
<<" = "<<f.Numerator_Sum<<"/"<<f.Denominator_Sum<<endl;

needless to say it is slightly long....the second line is:
cout<<" = "<<f.Numerator_Sum<<"/"<<f.Denominator_Sum<<endl;

as you can sort of see. i am improvising the spacing.

thanks in advance.
Last edited on Mar 7, 2010 at 3:03am
Mar 7, 2010 at 2:45am
You could use setwidth() to make the size of the left hand expression be at least a certain length. This wouldn't make it work in all cases, however. The first way that I think of would be to read the entire equation into a stringstream first and get the length of the equation so you know how far to space over.
Mar 7, 2010 at 2:51am
sorry i am unfimiliar with both a stringsream and setwidth could you show me the basic syntax?

EDIT:

also, i need to find the length of the expression that is output onto the screen which isnt one string but a string followed by a variable followed by a string etc. im not sure if stringstream will work with that.
Last edited on Mar 7, 2010 at 3:03am
Mar 7, 2010 at 3:10am
i have sort of come up with a solution. all i need is the syntax to find the number of digits of an integer input by the user

what i plan to do is use a loop to write out a space for every digit preceding the '=' .

it's messy but should work.
Topic archived. No new replies allowed.