vector outputs display vertically in columns

Aug 23, 2018 at 1:04am
I am trying to output my strings column-wise using the vector iterator as shown in the attached image. For now I am just displaying a test case using the names and the cider order after which I want to replicate the same for others.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  // values for controlling format
const int name_width = 15 ;
const int int_width = 7 ;
const int dbl_width = 12 ;
const int num_flds = 7 ;
const std::string sep = " |" ;
auto total_width = name_width*2 + int_width*2 + dbl_width*3 + sep.size() * num_flds ;
const std::string line = sep + std::string( total_width-1, '-' ) + '|' ;

cout<<"How many people ordered? ";
cin>>odrs; // Store number of orders

for(int i=1; i<=odrs; i++){
    cout<<"Enter the name of person #"<<i<<":"<<endl;;
    cin>>names;  // Store names of users

    odrNames.push_back(names);  // Store names as entered as

    cout<<"How many orders of cider did "<<names<<" have? ";
    cout<<endl;
    cin>>odrciderjuice;  // Store Cider order item
    sbCider = odrciderjuice * 5.5;    // Calculate Cider order per price
    odrCider.push_back(odrciderjuice); // Store Cider order item based on entry
    SbCider.push_back(sbCider);      // Store calculated Cider order per price

    cout<<"How many orders of apple juice did "<<names<<" have? ";
    cout<<endl;
    cin>>odrapplejuice;     // Store Juice order item
    sbJuice = odrapplejuice * 4.5;    // Calculate Juice order per price
    odrApple.push_back(odrapplejuice);     // Store Juice order item based on entry
    SbJuice.push_back(sbJuice);      // Store calculated Juice order per price
    cout<<endl;

    total = sbCider + sbJuice;   // Calculate total between Cider and Juice
    GTotal.push_back(total);     // Store total values after calculation
    cout<<endl;
}

for(vector<string>::iterator naming = odrNames.begin(); naming!= odrNames.end(); ++naming)
    cout << sep << std::setw(name_width) << *naming<<"\v";

for(vector<int>::iterator ciderOdr = odrCider.begin(); ciderOdr!= odrCider.end(); ++ciderOdr)
    cout <<*ciderOdr;
Aug 23, 2018 at 1:40am
Using \v is not going to work. You need to print a line at a time, not a column at a time.

Instead of a bunch of separate vectors for each field you should use a struct. It's also handy to have a constructor for it to fill in the fields easier.

Below I'm using cin.getline to read the name. Since it reads an entire line, it allows the name to contain spaces. But since cin >> x leaves the "newline" behind in the input stream, you need a function for skipping the rest of the line (skip_line below).

You don't need to save the calculations since they can be done during output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iomanip>
#include <iostream>
#include <limits>
#include <vector>
using namespace std;

struct Order {
     string name;
     int cider;
     int juice;
     
     Order(string nm, int cd, int ap)
         : name(nm), cider(cd), juice(ap) {}
};

inline void skip_line() {
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

int main() {
    vector<Order> orders;
    int numOrders;
    cout << "Number of orders: ";
    cin >> numOrders;
    skip_line();
    for (int i = 0; i < numOrders; i++) {
        string name;
        int cider, juice;
        cout << "Name: ";
        getline(cin, name);
        cout << "Ciders: ";
        cin >> cider;
        cout << "Juices: ";
        cin >> juice;
        skip_line();
        orders.push_back(Order(name, cider, juice));
    }
    cout << fixed << setprecision(2);
    for (const auto& o: orders) {
        double totCider = o.cider * 5.5;
        double totJuice = o.juice * 4.5;
        cout << left << setw(12) << o.name << right << ' '
             << setw(4) << o.cider << ' ' << setw(6) << totCider << ' '
             << setw(4) << o.juice << ' ' << setw(6) << totJuice << ' '
             << setw(7) << totCider + totJuice << '\n';
    }
}

Aug 23, 2018 at 8:50am
@tpb Thanks for the help. i appreciate it.
Topic archived. No new replies allowed.