1

I need to read data from a text file, and insert the data in an array of struct. The data file is in the following format:

productname  price  quantity

my main concern is to read the product name, which consist of one, and two words. Should I approach product name as a c-string or as a string literal?

any help appreciated

#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 15; //drink name char size
const int ITEMS = 5; //number of products

struct drinks
{
       char drinkName[SIZE];
       float drinkPrice;
       int drinkQuantity;
};      


int main()
{
    //array to store drinks
    drinks softDrinks[ITEMS];

    //opening file
    ifstream inFile;
    inFile.open("drinks.txt");

    char ch;
    int count = 0; //while loop counter


    if(inFile)
    {
         while(inFile.get(ch))
         {
             //if(isalpha(ch)) { softDrinks[count].drinkName += ch; }
             //if(isdigit(ch)) { softDrinks[count].drinkPrice += ch; }
             cout << ch;

         }
         cout << endl;
         count++;  
    }
    else
    {
        cout << "Error opening file!\n";
        system("pause");
        exit(0);
    }

    system("pause");
    return 0;
}

1 Answer 1

3

Since you ask for "any help", here's my view: Forget everything you wrote, and use C++:

#include <fstream>   // for std::ifstream
#include <sstream>   // for std::istringstream
#include <string>    // for std::string and std::getline

int main()
{
    std::ifstream infile("thefile.txt");
    std::string line;

    while (std::getline(infile, line))
    {
        std::istringstream iss(line);

        std::string name;
        double price;
        int qty;

        if (iss >> name >> price >> qty)
        {
            std::cout << "Product '" << name << "': " << qty << " units, " << price << " each.\n";
        }
        else
        {
          // error processing that line
        }
    }
}

You could store each line of data in a std::tuple<std::string, int, double>, for example, and then put those into a std::vector as you go along.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for Forget everything you wrote, and use C++. How can you make someone forget? :)
@FailedDev: I wish I knew. My basic rule is like this: "If you're using pointers, new, or using namespace std;, You're Doing it Wrong." Take it for what it's worth. I guess "fixed-sized arrays with magic constants" should go in there, too.
Also best loc I have ever seen #define private public - This in product code :)
@FailedDev: Haha, best way to make those pesky compiler errors go away. Me, I prefer #define abusing using.
yeah, this is great if all lines are the same format, but some lines contain a product of one word, and others contain a product of two words. so this: "if (iss >> name >> price >> qty)" would not give me the desired result
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.