7 Replies - 765 Views - Last Post: 26 January 2015 - 05:03 AM Rate Topic: -----

#1 Maduin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-September 14

Loading Data From File Into An Array

Posted 25 January 2015 - 02:13 PM

Hello, I am currently working on a project that requires me to "load the data in the file into array at the beginning of the program."

I have a text file with data, and I need to populate an array with the information. From then on, I am supposed to be able to add, display, and search that array. However, I can't figure out how to add the data from the file into an array. This is causing me a lot of confusion, because up until the instructor informed me to load the data into the array, I was trying to find out how to search the text file itself. So it threw me off balance and I've been staring so long at the screen I can't really focus.

I appreciate all your time and help.

Is This A Good Question/Topic? 0
  • +

Replies To: Loading Data From File Into An Array

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Loading Data From File Into An Array

Posted 25 January 2015 - 02:21 PM

Well to be honest you sound a bit depressed..


What language is it for C or C++ ?

Cheer up.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 Maduin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-September 14

Re: Loading Data From File Into An Array

Posted 25 January 2015 - 02:40 PM

Well to be honest I'm very stressed and put-out, I had to drop this class last term due to too much going on, and now I just have this class again but it is proving very difficult. Last class I need to graduate. It is for C++.

I have to create a text-based menu that allows you options to enter a task/assignment, display them all, and find a particular task by course name. The tasks take three parameters (course name, description, and due date). I have a struct called Task that holds an array of character values (also a requirement, we can not use string only c-string) for the three parameters. In the text file, they are delimited by a ";", thus looking like: "CS162;Finish Lab 2;9/26/2009". Somehow I have to load the data in the filed into an array, and then be able to search that array for the course name (i.e. "CS162").

Your time and efforts are greatly appreciated, I can't go forward on this without guidance.
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Loading Data From File Into An Array

Posted 25 January 2015 - 03:08 PM

OK then,

but we are not a code writing service,

we are more a teaching website ,

so what have you tried ?

what code do you currently have ?

and what is the contents of your text file.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 Maduin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-September 14

Re: Loading Data From File Into An Array

Posted 25 January 2015 - 03:25 PM

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <istream>
#include <cctype>

using namespace std;

const int MAX_CHAR = 100;

struct Task 
{
	char courseName[MAX_CHAR];
	char courseDescription[MAX_CHAR];
	char dueDate[MAX_CHAR];
} newTask;


int main()
{
	ifstream inFile;
	ofstream outFile;
	char displayData(char pathName[MAX_CHAR]);
	char pathName[MAX_CHAR];
	char answer;
	char selector;
	int  count = 0;

		cout << "Welcome to the Task Manager, please select an option below:" << endl;
		cout << "(A)dd (D)isplay all (F)ind (Q)uit" << endl;
		cin >> selector;
		selector = toupper(selector);
		switch(selector)
		{
			case 'A':
				cout << "PLACEHOLDER TEXT FOR FUNCTION" << endl;
				break;
			case 'D':
				cout << displayData(pathName) << endl;
				break;
			case 'F':
				cout << "PLACEHOLDER TEXT FOR FUNCTION" << endl;
				break;
			case 'Q':
				cout << "PLACEHOLDER TEXT FOR FUNCTION" << endl;
				break;
			default:
				cout << "Invalid input, please enter an option from the menu list." << endl;
              }

	cout << "Do you want to add more tasks? " << endl;
	cin >> answer;
	cin.ignore();

	while ((answer == 'Y') || (answer == 'y'))
	{
		cout << "Name of the course: " << endl;
		cin.get(newTask.courseName, MAX_CHAR, '\n');
	
		cout << endl;
		cin.clear();
		cin.ignore(100, '\n');

		cout << "Description of the task: " << endl;
		cin.get(newTask.courseDescription, MAX_CHAR, '\n');
		cout << endl;
		cin.clear();
		cin.ignore(100, '\n');

		cout << "Due date of the task: " << endl;
		cin.get(newTask.dueDate, MAX_CHAR, '\n');
		cout << endl;
		cin.clear();
		cin.ignore(100, '\n');

		cout << "Do you want to add more tasks? " << endl;
		cin >> answer;
		cin.ignore();

		outFile << newTask.courseName << ";" << newTask.courseDescription << ";" << newTask.dueDate << endl;
	 }

	cout << '\n' <<"Thank you." << '\n' << endl;

	system("pause");

	inFile.close();
	outFile.close();
	return 0;
}

char displayData(char pathName[MAX_CHAR])
{
	ifstream inFile;
   
	inFile.open("tasks.txt");
      
	if (!inFile)
    {
		cout << "Cannot open backup file" << endl;
		return -1;
	}

	cout << endl;
	cout << "Displaying all entries:" << endl;
	cout << endl;
	cout << inFile.rdbuf() << endl;
	cout << endl;
}


Above, we can see that I was able to create a separate display() function (which works), to show the contents of the file, which are:
CS162;Finish Lab 2;9/26/2009
CS201;Take Quiz 1;9/28/2009

I have a basic text interface menu created, where each process will call a function to act on the array.

I haven't made the "add" code into its own function yet to be called, I have it hard-coded into main(). The same goes for the find() function, and quit().

However, I just can't wrap my head around loading the "struct Task" information from "tasks.txt" into an array, whereas I could search it. I don't even know how to begin (I can't think too far ahead at the moment from all of the stress).

I appreciate your help, and yes I want to learn this thoroughly. I spent 11 or so hours yesterday trying to finish this, I'm not about to give up learning it now.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 19,880
  • Joined: 05-May 12

Re: Loading Data From File Into An Array

Posted 25 January 2015 - 09:43 PM

View PostMaduin, on 25 January 2015 - 05:25 PM, said:

However, I just can't wrap my head around loading the "struct Task" information from "tasks.txt" into an array, whereas I could search it. I don't even know how to begin (I can't think too far ahead at the moment from all of the stress).

The Structures tutorial at Cplusplus.com may help get you unstuck.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 5305
  • View blog
  • Posts: 16,524
  • Joined: 25-December 09

Re: Loading Data From File Into An Array

Posted 25 January 2015 - 10:12 PM

Also let's look at this snippet:
int main()
{
	ifstream inFile;
	ofstream outFile;
	char displayData(char pathName[MAX_CHAR]);
	char pathName[MAX_CHAR];
...

		outFile << newTask.courseName << ";" << newTask.courseDescription << ";" << newTask.dueDate << endl;
	 }

	cout << '\n' <<"Thank you." << '\n' << endl;

	system("pause");

	inFile.close();
	outFile.close();
	return 0;
}


You never actually open either of the streams in main, so the out file should have no effect and you don't need to close the streams, just let the destructor close the file.


It looks like you need to review basic file io as well.

Next let's look at this snippet:

char displayData(char pathName[MAX_CHAR])
{
//	ifstream inFile;
   
//	inFile.open("tasks.txt"); // Why are you using the open function? And why aren't you using the parameter?


      ifstream inFile(pathName); // Why not? You should prefer the constructor when possible.
      
	if (!inFile)
    {
		cout << "Cannot open backup file" << endl;
		return -1;
	}

////////////////////////////  WHY? 
	cout << endl;
	cout << "Displaying all entries:" << endl;
	cout << endl;
	cout << inFile.rdbuf() << endl;
	cout << endl;


}



You should be reading the file line by line, or item by item. By the way why are you using C-strings? You're writing a C++ program so you should be using std::string instead. I recommend reading an entire line into a string then use a stringstream to process that string to separate the individual items with getline() using the optional third parameter, the delimiter (in this case a semicolon).


Jim
Was This Post Helpful? 1
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6966
  • View blog
  • Posts: 14,572
  • Joined: 16-October 07

Re: Loading Data From File Into An Array

Posted 26 January 2015 - 05:03 AM

Functions, functions, functions. Actually, how the hell did you ever call that display?

I'm going to throw a skeleton at you, using mostly your code. Now just fill in the blanks.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <istream>
#include <cctype>

using namespace std;

const int MAX_CHAR = 100;

struct Task {
    // why not C++ strings
    char courseName[MAX_CHAR];
    char courseDescription[MAX_CHAR];
    char dueDate[MAX_CHAR];
}; // NO newTask;

// you need functions
int menuDisplay(const char *pathName);
int menuAdd(const char *pathName);
int menuFind(const char *pathName);
char menu();

void print(Task &);
void loadTaskFromUser(Task &);
bool read(istream &, Task &);
void add(ostream &, Task &);
int add(const char *pathName, Task &);
bool find(const char *pathName, Task &, char *);


int main() {
    char *pathName = "tasks.txt";
    int result = 0;

    bool done = false;
    while(!done && result==0) {
        switch(menu()) {
            case 'A':
                result = menuAdd(pathName);
                break;
            case 'D':
                result = menuDisplay(pathName);
                break;
            case 'F':
                result = menuFind(pathName);
                break;
            case 'Q':
                done = true;
                break;
            default:
                cout << "Invalid input, please enter an option from the menu list." << endl;
        }
    }
    cout << '\n' <<"Thank you." << '\n' << endl;

    return 0;
}

char menu() {
    char selector;
    cout << "Welcome to the Task Manager, please select an option below:" << endl;
    cout << "(A)dd (D)isplay all (F)ind (Q)uit" << endl;
    cin >> selector;
    return toupper(selector);
}

// why return char?!? Why c-string?  Why the hell aren't you using it.
// char displayData(char pathName[MAX_CHAR]) {
int menuDisplay(const char *pathName) {
    ifstream inFile;

    inFile.open(pathName);

    if (!inFile) {
        cout << "Cannot open backup file" << endl;
        return -1;
    }

    Task task;
    cout << endl << "Displaying all entries:" << endl;
    while(read(inFile, task)) { print(task); }
    cout << endl;
    return 0;
}

void loadTaskFromUser(Task &newTask) {
    cout << "Name of the course: " << endl;
    cin.get(newTask.courseName, MAX_CHAR, '\n');
    cout << endl;
    cin.clear();
    cin.ignore(100, '\n');

    cout << "Description of the task: " << endl;
    cin.get(newTask.courseDescription, MAX_CHAR, '\n');
    cout << endl;
    cin.clear();
    cin.ignore(100, '\n');

    cout << "Due date of the task: " << endl;
    cin.get(newTask.dueDate, MAX_CHAR, '\n');
    cout << endl;
    cin.clear();
    cin.ignore(100, '\n');

}

int menuAdd(const char *pathName) {
    Task task;
    loadTaskFromUser(task);
    return add(pathName, task);
}

int menuFind(const char *pathName) {
    // ask the user what they're looking for, I guess
    // call bool find(const char *pathName, Task &, char *);
    return 0;
}


bool read(istream &in, Task &t) { /* your code here */ }
void add(ostream &out, Task &t) { /* your code here */ }
void add(const char *pathName, Task &t) { /* your code here */ }
bool find(const char *pathName, Task &t, char *searchValue) { /* your code here */ }
void print(Task &t) { /* your code here */ }



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1