Help with passing a file into multiple arrays

Fandango21

Junior Member
Feb 8, 2011
23
0
0
Anyone who could give me advice on this, I would greatly appreciate it. I've spent most of my day searching for something that made sense and found a few things but I'm totally stuck at this point. I have to read a .dat file that I have into 3 separate arrays. Here is what I have so far...

students.dat file

LENNON 1 4.0
MACARTNEY 2 3.7
HARRISON 3 2.95
STARR 3 3.97
HOWARD 1 3.2
FINE 3 2.11
BESSER 2 4.0
DERITA 1 3.9


Each line represents one student. All columns are separated by one space. Assume there are up to 100 students. Will never be more than 100, but possibly up to 100. Program has to read students.dat file into 3 arrays, one array for each column.

The name has to be a string type
The second column is an int type
The third is a double

I'm totally lost about how to pass these into the arrays. Here is my code so far. It has to be 3 arrays, can't use anything else. Thanks in advance for any help.


Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

//Set constant for length of array
const int arrayLength = 100;

int main( ) {

	//Set file variable and open file
	ifstream inFile;
	inFile.open("students.dat");

	//Set error if file name is not correct
	if(inFile.fail()) {
		cout << "Error: Can't open file. Recheck your filenames"<< endl;
		return 0;
	}
	
	//Declare arrays for file input	
	string lastName[arrayLength];
	int classID[arrayLength];
	double gpa[arrayLength];

	while (! inFile.eof()) {

		}
			
	cout << "Welcome to Miss Winfred Fowl's GPA Report" << endl;
	cout << endl;
	cout << "All Students Report" << endl;
	

	//Set while loop with break to run loop until DONE is entered
	while(true) {
		
		string studentName;
	
		// Have the user input a name or DONE to quit the loop
                cout << endl;
		cout << "Please enter a student name: ";
		cin >> studentName;
	
		if(studentName == "DONE") {
			break;
		}
	}	


	
	return 0;
}
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
use the >> operator with the ifstream object, it will automatically parse a word for type string, an integer for int and a real for double. so in your loop you just have to call << three times, one for each array.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
use the >> operator with the ifstream object, it will automatically parse a word for type string, an integer for int and a real for double. so in your loop you just have to call << three times, one for each array.

The input operator is making life easy for you.

Utilize it!
 

Fandango21

Junior Member
Feb 8, 2011
23
0
0
Awesome. Thanks for the help everyone. I've got the info into arrays now, but I'm getting an issue when I'm trying to print. All kinds of errors that I'm unsure how to fix. I know I need to use a loop to print out the arrays, but they need to be in the line format like above plus I can't even get them to print. Here's where I'm at now. Thanks for the help.


Here's the errors I'm getting

pass9.cpp: In function 'int main()':
pass9.cpp:50: error: assignment of function 'char* index(const char*, int)'
pass9.cpp:50: error: cannot convert 'int' to 'char* ()(const char*, int)throw ()' in assignment
pass9.cpp:50: error: ISO C++ forbids comparison between pointer and integer
pass9.cpp:50: error: ISO C++ forbids incrementing a pointer of type 'char* (*)(const char*, int)throw ()'
pass9.cpp:50: error: invalid lvalue in increment
pass9.cpp:52: error: invalid types 'std::string [100][char* ()(const char*, int)throw ()]' for array subscript
pass9.cpp:53: error: invalid types 'int [100][char* ()(const char*, int)throw ()]' for array subscript
pass9.cpp:54: error: invalid types 'double [100][char* ()(const char*, int)throw ()]' for array subscript


Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

//Set constant for length of array

const int arrayLength = 100;

int main( ) {

        //Set file variable and open file
        ifstream inFile;
        inFile.open("students.dat");

        //Set error if file name is not correct
        if(inFile.fail()) {
                cout << "Error: Can't open file. Recheck your filenames" <<endl;
                return 0;
        }

        //Declare arrays for file input 
        string studentName[arrayLength];
        int classID[arrayLength];
        double gpa[arrayLength];

        //While loop to run until the end of the file.
        while (! inFile.eof()) {

                //Initialize variable
                int numberofStudents = 0;

                //While loop to input file data to array
                while (inFile && numberofStudents < 100) {
                        numberofStudents++;
                        inFile >> studentName[numberofStudents];
                        inFile.ignore(100, '\n');
                        inFile >> classID[numberofStudents];
                        inFile.ignore(100, '\n');
                        inFile >> gpa[numberofStudents];
                        inFile.ignore(100, '\n');

                }
      }
//This is the part I'm having a problem with. I need it to print in a list format like I showed above.
        /*
        for(index = 0; index < arrayLength; index++) {
                
                cout << studentName[index] << endl;
                cout << classID[index] << endl;
                cout << gpa[index] << endl;
        }
        */

        cout << "Welcome to Miss Winfred Fowl's GPA Report" << endl;
        cout << endl;
        cout << "All Students Report" << endl;


        //Set while loop with break to run loop until DONE is entered
        while(true) {

                string studentName;

                // Have the user input a name or DONE to quit the loop
                cout << endl;
                cout << "Please enter a student name: ";
                cin >> studentName;

                if(studentName == "DONE") {
                        break;
                }
        }



        return 0;
}
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Array subscripts can not be declared as a variable, even with the const in front.

Use a #define arrayLength 100

Where is index declared? this may be causing mulitple problems
 

iCyborg

Golden Member
Aug 8, 2008
1,329
53
91
Array subscripts can not be declared as a variable, even with the const in front.

Use a #define arrayLength 100
Definitely not true for any reasonable C++ compiler. And const is preferred in C++.

Anyway, the error is in not declaring variable index. It seems that index is being treated as a function declared in some old version of strings.h:
char *index(const char *, int); (LEGACY )

This opens up a can of worms as the compiler tries to assign stuff to a function, use ++ on it etc. So just put "for (int index=0; ..."
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
also, in your print loop, what happens when numberOfStudents is small? (hint: blank lines)

You might want to save that variable outsize your while loop for reading in the file.

Also, what happens if the file actually contains the 100 student max?
 

Fandango21

Junior Member
Feb 8, 2011
23
0
0
Thanks for all the help everyone. I've taken all the advice and have everything squared away with the problems I was having. This program is due today and I'm almost done but I'm having some trouble with my search and print functions. Basically I have to find and print the entire line for a student name when someone searches for it. ex: if they search LENNON, it would find it in the array and print out LENNON English 1010 3.75. I've set up what I thought would work but so far no luck (especially when returning -1, which I have to do) Any suggestions?

ps. I have commented out of the program the issues I have so it will still run

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

//Set constant for length of array
const int ARRAY_LENGTH= 100;

/*
//Function to search for student name
string searchForName(string studentName[], int arrayLength, string studentSearch){
                bool found = false;
                for (int index = 0; index < arrayLength; index++) {
                        if (studentName[index] == studentSearch){
                                found = true;
                                break;
                        }
                        if(found) { return studentName[index];}
                        else { return -1; }
                        
                }        
}

//Function to print out from student name search
void printStudentInfo(searchForName) {
                if(searchForName == -1) {
                        cout << "STUDENT NOT FOUND" << endl;
                        cout << endl;
                }
                else { 
                        cout << "Name" << setw(15) << "Class" << setw(14) << "GPA" << endl;
                        cout << "----" << setw(15) << "-----" << setw(14) << "---" << endl;
                        
                        cout << nameSearch << endl;
                        cout << endl;
                }                       
}
*/



//Function to determine which class the value for classID is equal to
string classNames(int classIDnumber) {if(classIDnumber == 1) {
                        return "English 101";
                } else if(classIDnumber == 2) {
                        return "History 203";
                } else if(classIDnumber == 3) {
                        return "CSCI 1010";
                }
}

int main( ) {

        //Output Header
        cout << "Welcome to Miss Winfred Fowl's GPA Report" << endl;
        cout << endl;
        cout << "All Students Report" << endl;

        //Set file variable and open file
        ifstream inFile;
        inFile.open("students.dat");

        //Declare arrays for file input 
        string studentName[ARRAY_LENGTH];
        int classID[ARRAY_LENGTH];
        double gpa[ARRAY_LENGTH];

        //While loop to run until the end of the file.
        while (! inFile.eof()) {

                cout << "Name" << setw(15) << "Class" << setw(14) << "GPA" << endl;
                cout << "----" << setw(15) << "-----" << setw(14) << "---" << endl;


                //While loop to input file data to array
                for (int index = 0; index <= ARRAY_LENGTH; index++) {

                        inFile >> studentName[index] >> classID[index] >> gpa[index];
                        if(inFile.fail()) {
                                break;
                        }
                //Call classNames function to convert int to correct string 
                string name = classNames(classID[index]);

                //Output file
                cout << fixed << showpoint << setprecision(2);
                cout << studentName[index] << right << setw(18) << name << right << setw(10) <<  gpa[index] << endl;
                }
                cout << endl;
                inFile.close();
        }


        //Set while loop with break to run loop until DONE is entered
        while(true) {

                string student;

                // Have the user input a name or DONE to quit the loop
                cout << endl;
                cout << "Please enter a student name: ";
                cin >> student;

                if(student == "DONE") {
                        break;
                }
                /*
                //Search for student
                searchForName(studentName,ARRAY_SIZE,student);
                
                //Print student search info
                printStudentInfo(searchForName);
                */
        }



        return 0;
}
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
* ARRAY_SIZE is not defined
* in function printStudentInfo(), searchForName doesn't have a type.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |