0

I just started classes for my college programming course, so I am a complete beginner. I want to align my decimals between my input and output phases. I can get my output phase all aligned, but the input phase is not lining up. Is there a way to ensure my decimals line up? I have added a picture to show the code and the line that I would like to align. (lines 16/17 to line up with lines 30-33). Thank you for any help you can provide. I am completely new to programming and am not even 100% sure of what I am doing, a lot of trial and error at this point.

program/code screenshot

Here is the code I have written so far:

#include <iomanip>
using namespace std;
int main()
{
//define variables
float number_books;
float cost_book;
float total_order;
float total_shipping;
float total_order_shipping;

// input phase
cout << "Enter number of books to order:  ";
cin >> number_books;
cout << "Enter cost per book:   $  ";
cin >> cost_book;

// process phase
total_order = number_books * cost_book;

if (total_order > 50)
    total_shipping = 0;
else
    total_shipping = 25.00f;

total_order_shipping = total_shipping + total_order;

// output phase
cout << setprecision(2) << fixed;
cout << "Book order total:      $" << setw(8) << total_order << endl;
cout << "Shipping total:        $" << setw(8) << total_shipping << endl;
cout << "Order & Shipping total:$" << setw(8) << total_order_shipping << endl;


system("pause");

return 0;
}
gucci_S
  • 1
  • 1
  • I don't think you can align what the user is typing. To do that you would need to know how many characters the user would type. There are ways to do what you want but they aren't standard C++ (how you do it depends on your OS and stuff) so you need a library like curses or something. – Jerry Jeremiah Sep 12 '21 at 21:48

1 Answers1

0

It's not possible to directly format a cin with setw, in your case setw(8).

See the reference: Issues with cin and Aligning a cin to the right

But in your case, you can hack the format into setw(8) by the help of the user:

Enter number of books to order:  4
Enter cost per book:   $    2.15
Book order total:      $    8.60
Shipping total:        $   25.00
Order & Shipping total:$   33.60

User can calculate how many spaces until 8 is proper, in your case, put two spaces. If that's not what you want, then unfortunately it's out of luck to directly use cin to achieve what you want...

hddmss
  • 231
  • 1
  • 12
  • ahhh, I see! Thank you for your input and advice. I understand why it was not working now. I am excited to learn more, so I can do more, but I am not there yet. I look forward to learning more. I appreciate you taking the time to explain this to me! :) – gucci_S Sep 13 '21 at 00:46