Reputation: 75
I have tried setw() function from iomanip library, wonder how this one text line can't be aligned. Here's the code :
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main() {
ifstream productlist;
productlist.open("product.txt");
if (!productlist.is_open()) {
cout << "File failed to open \n";
return 0;
}
string name, squantity, sprice;
int quantity, price;
string line;
cout << "Product Name\t" << "Quantity\t" << "Price\t" << "Value\n";
while (getline(productlist, line)) {
stringstream ss(line);
getline(ss, name, '/');
getline(ss, squantity, '/');
quantity = stoi(squantity);
getline(ss, sprice);
price = stoi(sprice);
int value = quantity * price;
cout << name << "\t\t"<< quantity << "\t\t" << price << "\t" << value << endl;
}
productlist.close();
}
and here's the text file that I used in the said code.
Apple/5/10
Water/1/40
Pencil/10/11
Orange/2/10
Lemon/1/12
Krunch/2/50
Coco/2/14
Burgers/2/25
Lemonades/2/21
Pizzas/2/250
and here is the output
Product Name Quantity Price Value
Apple 5 10 50
Water 1 40 40
Pencil 10 11 110
Orange 2 10 20
Lemon 1 12 12
Krunch 2 50 100
Coco 2 14 28
Burgers 2 25 50
Lemonades 2 21 42
Pizzas 2 250 500
I can use the setw() function and it would end up still the same as the output that I shown here.
Upvotes: 0
Views: 42