Hello, this is my first post here. I'm taking c++ as an elective course, and I'm doing great until now. The problem I'm having is taking data from a txt file formatted line by line #### # ## for product id, store number, and quantity. I'm supposed to read this data and store it in three different arrays for each token. I think I can get it from there, but I don't know where to start, which library to use, confused by existing tutorials I've read, getting frustrated. Plz help.

Recommended Answers

You don't need anything complex. You will only have to use the <string>, <fstream> and <iostream> libraries. You will have to read some file input tutorials in C++. Even that is not that complex.

Jump to Post

All 4 Replies

You don't need anything complex. You will only have to use the <string>, <fstream> and <iostream> libraries. You will have to read some file input tutorials in C++. Even that is not that complex.

some progress made. i may have just needed the personal note --have been working on this stuff all day, the end of the term is coming on fast and we just took test 1 of 3 yesterday. here she is:

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


bool  loadArrays(const char fileName[], long idArray[], 
	int  storeArray[], int qtyArray[], int & count, int maxCells);

void printArrays(ostream & where, const long idArray[], 
           const int storeArray[], const int qtArray[], int  count);

bool extractData(const char newFileName[],int requestId, int baseQty,
          const long idArray[], const  int storeArray[],
		const int qtArray[], int  count, int & newcount);

const int ORDER_VALUE = 500;

int main() {


/*
fileName 	 - the Windows name of the file created in step 1
newFileName	 - the Windows file name of the file of extracted records

requestId	 - the product id number used to extract data
   ORDER_VALUE  - the quantity value used to extract data, make this
                     a global constant with a value of 500	

	idArray	 - the array of id numbers
	storeArray	 - the array of store numbers
	qtyArray	 - the array of quantities

	count		 - the actual number of cells filled in the arrays
	newcount	 - the number of extracted records written to 

*/
	char fileName[] 	= "products.txt";
	char newFileName[]  = "list.txt"; 

	int requestID;
	int idArray[ORDER_VALUE], storeArray[ORDER_VALUE], qtyArray[ORDER_VALUE];
	int count = 0, newCount = 0;
	

	if( loadArrays(fileName, idArray, storeArray, qtyArray, count, 20) )
		printArrays(cout, idArray, storeArray, qtyArray, count);

	return 0;

} // end main



bool  loadArrays(const char fileName[],long idArray[], 
				int  storeArray[], int qtyArray[], int & count, int maxCells) {
	
	ifstream in;
	in.open(fileName);

	if(!in) return false;

	for(int i = 0; !in.eof(); i++) {
		in >> idArray[i];
		in >> storeArray[i];
		in >> qtyArray[i];
		count = i;
	}
	in.close();

	return true;
}

void printArrays(ostream & where, const long idArray[], 
                const int  storeArray[], const int qtyArray[], int count) {

	for(int i = 0; i <= count; i++) 
		where << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << endl;


}

bool extractData(const char newFileName[],int requestId, int baseQty,
               	const long idArray[], const  int storeArray[],
		   		const int qtArray[], int  count, int & newcount) {


}

which gives the following errors:

project03.cpp: In function ‘int main()’:
project03.cpp:48: error: invalid conversion from ‘int*’ to ‘long int*’
project03.cpp:48: error: initializing argument 2 of ‘bool loadArrays(const char*, long int*, int*, int*, int&, int)’
project03.cpp:49: error: invalid conversion from ‘int*’ to ‘const long int*’
project03.cpp:49: error: initializing argument 2 of ‘void printArrays(std::ostream&, const long int*, const int*, const int*, int)’

I'm using gnu compiler if this matters.. what am i doing wrong? thx

That is because you have declared the second arguments of loadArray and printArray as a long array but you are inputing an int array.

Change the declaration of idArray from int to long , or just use int in the function declarations. I dont think you will want it to use long arrays. The range of int should be enough.

oh, now i see that. thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.