csv Inventory file I/O

Hello,

I am an avid reader of this forum, and I know that it is frowned upon to ask for help when it comes to homework, but I have an urgent problem that I just cannot solve alone.

I am a beginner when it comes to C++ as I have always more focused on web languages than programming languages as such (dabbling in VB and python excluded), but decided to study it as my GF of two years switched her major to IT and had to take C++ over the summer to stay on schedule for graduation.

So far I was able to help her usually get her projects done and was trying to catch up by studying of cplusplus while she used her college book, but now I am far beyond my abilities and don't know what to do. She has a deadline due and is freaking, and I would sincerely appreciate any help possible.

The project wants us to read values from a csv file and display them unsorted the first time, and sorted alphabetically from top to bottom the second time.

I tried a few things and would appreciate any help.

The header file so far is:

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
// Product.h

#ifndef PRODUCT_H
#define PRODUCT_H
#include <string>		  // header file used to manipulate and process strings and arrays
using namespace std;	  // instructs compiler to use names in standard library

// declare Product Class
class Product
{
private:
string SKU; // SKU
string ProductName; // ProductName
string Category; // Category
double UnitPrice; // UnitPrice
int OnHand; // OnHand
int ReorderLevel; // ReorderLevel
int ReorderLeadTime; // ReorderLeadTime

public:
Product();
Product(string SKU, string ProductName, string Category,
	double UnitPrice, int OnHand, int ReorderLevel,
	int ReorderLeadTime);
void displayProductInfo(int heading=0);
void setProductInfo(string SKU, string ProductName, string Category,
	double UnitPrice, int OnHand, int ReorderLevel,
	int ReorderLeadTime);
string getProductName();
double getValue();
bool operator < (Product & aProduct);
bool operator > (Product & aProduct);
};

#endif 


The cpp file is:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Inventory Report.cpp
//
// Purpose: Write a program that 
//          1. 
//          2. 
//          3. 
//          4. 
//
// Author: Anonymous
// Reference: http://www.cplusplus.com
// Date: 23 July 2011

#include <iostream>		  // header file for console I/O
#include <iomanip>		  // header file for stream manipulators
#include <string>		  // header file used to manipulate and process strings and arrays
#include <fstream>		  // header file used to read data from a file
#include "Product.h"	  // header file for class and function declarations
#include <cmath>		  // header file for math
using namespace std;	  // instructs compiler to use names in standard library

/******************* Implementation of Product class ********************/
		// Construct a default product
Product::Product()
{
}

		// Construct a Product object with specified data
Product::Product(string SKU, string ProductName, string Category,
	double UnitPrice, int OnHand, int ReorderLevel,
	int ReorderLeadTime)
{
	setSKU();
	setProductName();
	setCategory();
	setUnitPrice();
	setOnHand();
	setReorderLevel();
	setReorderLeadTime();
}

		// Display ProductInfo
void displayProductInfo(int heading=0)
{
	cout << getSKU() << " " ;
	cout << getProductName() << " " ;
	cout << getCategory() << " " ;
	cout << getUnitPrice() << " " ;
	cout << getOnHand() << " " ;
	cout << getReorderLevel() << " " ;
	cout << getReorderLeadTime() << endl;
}
		// Set ProductInfo
void setProductInfo(string SKU, string ProductName, string Category,
double UnitPrice, int OnHand, int ReorderLevel,
int ReorderLeadTime)
{

}

string Product::getProductName()
{
	return ProductName;
}

double Product::getValue()
{
	return Value;
}
		// function to get Value
double getValue(int OnHand, double UnitPrice)
{
	return (OnHand * UnitPrice);
}


/********************************* main() *******************************/
int main()
{
	// Display author name to the right, title of program, and dashes
	cout << right;
	cout << setw(60) << "Anonymous" << endl;
	cout << "Product Inventory Report - Sort by Product" << endl;
	cout << setfill ('-') << setw(42) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	// Open the file for input
	cout << "Products READ FROM FILE" << endl;
	cout << setfill ('-') << setw(23) << "-" << endl;
	cout << endl << endl;					// extra lines (for legability)

	ifstream input("inventory.csv");
	Product ProductInfo;

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));
	displayProduct(ProductInfo);

	input.close();



	// Sort the Products alphabetically based on the Product Name
	cout << "Products SORTED BY Product Name" << endl;
	cout << setfill ('-') << setw(31) << "-" << endl;
	cout << endl << endl;			// extra lines (for legability)

	system("pause");
	return 0;
}





The contents of the csv file are:


00-101,Dharamsala Tea,Beverages,18.95,11,10,10
00-102,Tibetan Barley Beer,Beverages,19.08,10,25,21
00-103,Aniseed Syrup,Condiments,10.21,8,5,10
00-104,Chef Anton's Cajun Seasoning,Condiments,22,12,10,14
00-105,Chef Anton's Gumbo Mix,Condiments,21.35,10,0,14
00-106,1st Step Multi-vitamin,Supplement,15.95,18,15,7
00-107,Horizon Organic Milk 2%,Beverages,3.89,6,20,2
00-108,Glyco-Maize,Supplement,22.49,10,6,7
00-109,Red Lentils (Hyderabad),Dry Goods,4.19,12,10,18


It is supposed to display it as

SKU# ProductName Category On Hand Unit Price Inv Value




I assume that Inv Value = OnHand*UnitPrice for every line.

I tried to use a getline (input, Product, ',')



She just emailed me the keysteps:

Key Steps
1. Create a Product class for the objects that will hold the stock data. The Product class declaration is provided below. You must code the implementation. Include this code in the header file Product.h
2. Initialize variables, etc.
3. Open the file for input
4. Verify the file opened successfully
5. Read the contents of the file into local variables first
6. Store (transfer) the data into an array of product objects
7. Close the file
8. Display the original data to the screen
9. Sort the Products alphabetically based on the Product Name. Use relational operator overloading within the Product class for this.
10. Display the sorted list of stocks to the screen


I understand if no one can help but would appreciate any help I can get. The deadline is due soon and as her BF she hopes I can come through for her somehow.
I am at the end with my knowledge and definitely would love to avoid her having a nervous breakdown over an assignment.

Thanks,

Mark
Last edited on
1
2
input.read(reinterpret_cast<char*>
		(&ProductInfo), sizeof(Product));

A csv file is a text file isn't it? Binary file I/O will not work. Binary file I/O like this definitely will not work with strings as member variables, since strings have internal char pointers that are garbage if you read them back from a file. What this type of read does is it reads bytes from the file and puts it directly into the member variables. This assumes that the data in the file is in the same order as the member variables. Since csv is a text file, it will just place garbage into your object instance.

What I would do is do something like this (assuming the file has just been opened):

1
2
3
string lineFromFile;
getline(input, lineFromFile);
//here lineFromFile == "00-101,Dharamsala Tea,Beverages,18.95,11,10,10" 

and then use a combination of std::string::find_first_of() and std::string::substr() to parse out each piece of data and assign them to the object instance's member variables.


Actually, use the next method (it's easier).

You could also do something like:

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
string datum;
getline(input, datum, ',');
//here datum == "00-101"

getline(input, datum, ',');
//here datum == "Dharamsala Tea"

getline(input, datum, ',');
//here datum == "Beverages"

getline(input, datum, ',');
//here datum == "18.95"

getline(input, datum, ',');
//here datum == "11"

getline(input, datum, ',');
//here datum == "10"

getline(input, datum, ',');
//here datum == "10"

//read to next line
string newLine;
getline(input, newLine);

For each string that needs to be converted to an int or double, you could use a stringstream to do the conversion.
http://www.cplusplus.com/reference/iostream/stringstream/


1
2
3
4
5
6
7
setSKU();
setProductName();
setCategory();
setUnitPrice();
setOnHand();
setReorderLevel();
setReorderLeadTime();


I don't see any prototypes for these functions. There is no built-in C++ feature that sets the member variables in this way. For these functions, they will have to take in a parameter of the same type as the member variable they're setting, and then set the member variable to that parameter.

e.g.
1
2
3
4
void Product::setSKU(string newSKU)
{
     SKU = newSKU;
}


Or, the constructor can call this function once its implemented

void Product::setProductInfo(string newSKU, string newProductName, string newCategory, double newUnitPrice, int newOnHand, int newReorderLevel, int newReorderLeadTime);

Also:

displayProduct(ProductInfo);

I don't see a "displayProduct" function. I see a prototype of a "displayProductInfo" function in the Product class definition. I also see a global "displayProductInfo" function in Report.cpp. I assume that the latter was supposed to be preceded with Product::. Same thing with "setProductInfo". Since these functions are methods of Product, they must be preceded with Product:: in an implementation outside the class definition. It puts the functions in the namespace of Product.

The deadline is due soon and as her BF she hopes I can come through for her somehow.

Let's hope this love story has a happy ending!
Last edited on
Topic archived. No new replies allowed.