-1
item,price,qty,ordno,trdno
abc,54,2,123,32
xyz,34,2,345,21
item: string (char[])
price,qty (int)
ordno (long long)
trdno (int)

Make a structure for above mentioned fields
Make a vector (array, or any other container type) to hold multiple instances of this structure
1: Read file
2: read line, split values
3: initialize above mentioned structure object
4: add this object of structure to container
5: when whole file is read.. iterate over the container and print each elements values
(serialno, orderno, tradeno, price, qty, item)

I tried this and it is not working-

#include<bits/stdc++.h>
using namespace std;

struct item {  
    string name;
    double price;
    int quantity;
    int order_no;
    int trd_no;
};

int main()
{
    int n;cin>>n;
    string str, T;

    ifstream read("input.txt");

    while(getline(read,str))
    {
        cout<<str<<endl;
    }
    stringstream X(str);  // X is an object of stringstream that references the S string

    cout<<endl;

    while (getline(X, T, ','))
    {
        cout << T << endl;    // print split string
    }
    read.close();

    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44
  • 2
    Whichever C++ textbook taught you to use `` -- you need to throw it away and get a different C++ textbook. If you copied that off some web site, without any explanation, don't visit that web site any more. If you saw this in some clown's Youtube video, unsubscribe from that channel, you're not learning proper C++. Most C++ compilers in the world don't have this header file, and will not compile the shown code. [mrsam@monster stackoverflow – Sam Varshavchik Oct 02 '22 at 17:26
  • Similar for [using namespace std;](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Thomas Weller Oct 02 '22 at 18:09
  • Specifically, what part of the problem are you having issues with? – Thomas Matthews Oct 02 '22 at 18:17

1 Answers1

0

For the code that you are showing, you misplaced just one '}' after the first while. So the code will read in the first while-loop all lines of the file and is then empty. And, then you try to split the lines. This can of course not work.

If you move the closing bracket to in front of read.close(); then your code will work. Like this:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

struct item {
    string name;
    double price;
    int quantity;
    int order_no;
    int trd_no;
};

int main()
{
    int n; cin >> n;
    string str, T;

    ifstream read("input.txt");

    while (getline(read, str))
    {
        cout << str << endl;

        stringstream X(str);  // X is an object of stringstream that references the S string

        cout << endl;

        while (getline(X, T, ','))
        {
            cout << T << endl;    // print split string
        }
    }
    read.close();

    return 0;
}

Caveat: this will not work, if the source file contains the header row! You can read this and throw it away, if needed.


If we follow the instruction of your homework, line by line, and assume that the first line contains header rows, then we would do like the following.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

struct Item {
    std::string name;
    double price;
    int quantity;
    long long order_no;
    int trd_no;
};

int main()
{
    std::vector<Item> items;
    std::string str, T;
    std::ifstream read("input.txt");
    
    // Read first line and ignore it
    std::getline(read, str);

    while (std::getline(read, str))
    {
        std::istringstream X(str);  

        Item tempItem;
        getline(X, T, ',');
        tempItem.name = T;
        getline(X, T, ',');
        tempItem.price = std::stod(T);
        getline(X, T, ',');
        tempItem.quantity = std::stoi(T);
        getline(X, T, ',');
        tempItem.order_no = std::stoll(T);
        getline(X, T, ',');
        tempItem.trd_no = std::stoi(T);

        items.push_back(tempItem);
    }
    read.close();

    for (const Item& item : items)
        std::cout << item.name << ' ' << item.price << ' ' << item.quantity << ' '
        << item.order_no << ' ' << item.trd_no << '\n';
}

And, with a little bit more advanced C++, where we especially keep data and methods in a class, we could do the following:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <iterator>

// The item
struct Item {
    std::string name{};
    double price{};
    int quantity{};
    long long order_no{};
    int trd_no{};

    // Overwrite extraction operator for easier stream IO
    friend std::istream& operator >> (std::istream& is, Item& i) {
        char c;
        return std::getline(is >> std::ws, i.name, ',') >> i.price >> c >> i.quantity >> c >> i.order_no >> c >> i.trd_no;
    }
    // Overwrite inserter for easier output
    friend std::ostream& operator << (std::ostream& os, const Item& i) {
        return os << "Name: " << i.name << "\tPrice: " << i.price << "\tQuantity: " << i.quantity << "\tOrder no: " << i.order_no << "\tTRD no: " << i.trd_no;
    }
};

// The Container
struct Data {
    std::vector<Item> items{};

    // Overwrite extraction operator for easier stream IO
    friend std::istream& operator >> (std::istream& is, Data& d) {

        // Read header line and ignore it
        std::string dummy; std::getline(is, dummy);
        // Delete potential old data
        d.items.clear();

        // Read all new data from file
        std::copy(std::istream_iterator<Item>(is), {}, std::back_inserter(d.items));
        return is;
    }
    // Overwrite inserter for easier output
    friend std::ostream& operator << (std::ostream& os, const Data& d) {
        std::copy(d.items.begin(), d.items.end(), std::ostream_iterator<Item>(os, "\n"));
        return os;
    }
};

int main()
{
    // Open file and check, if it could be opened
    if (std::ifstream sourceFileStream("input.txt"); sourceFileStream) {
        
        // Define container
        Data data{};

        // Read and parse complete source file and assign to data
        sourceFileStream >> data;

        // Show result
        std::cout << data;
    }
    else std::cerr << "\n\nError: Could not open source file:\n\n";
}

A M
  • 14,694
  • 5
  • 19
  • 44