Hi, I am trying to write a program for uni that will read two different .txt files and output some data about them. I am fairly confident I can do this I just cant figure out what arguments to pass to my functions to read the files properly?
students.txt is a long file, with one word on each line. If someone can point me in the right direction on how to properly pass the correct arguments across I am confident I can right the code in the functions to output the data.
students.txt example:
1 //number of entrys
Sarah
Jones
1111
y
The typical way is to call something like getline in a loop to read from the ifstream and store the result into an array. It's up to u. The way u'r going u'll have to create a bunch of Student instances and pass each one into readStudents which could take a lot for memory.
Line 26 doesn't make sense because u'r passing a structure and not an instance of it. This makes more sense, for example:
thanks for the reply, that newstudent does make a lot more sense but would that run through my for loop to however many students was in the .txt? or would I have to create more instances like newstudent1 etc? (which would kind of defeat the purpose of the loop) lol. I think I am just a little confused on where they are supposed to get stored, if they should even get stored?
I am only supposed to read the file once and yeah I think I would have used a getline but we are not supposed to use functions that we have not yet covered. Just so annoying I cant even try and write the rest of the code because its not even reading the .txt in
Never mind all the above code, I have spent hours re writing and re re writing my program.. Good news is that I have made much progress and it looks completely different.
Is there any way I can read from a .txt file until I reach a certain character? I want to read the top 3 lines but after that there could be unknown amount of lines (if any) until the -1.
hey man
how did you sloved it in fact i m struggling with it
its first time i have use structures and i dont know which argu for both student and course function should i pass in order to read and out put some information
like if i wanna output the student info for who have N in thire record
i thought mine was solved but its not... I'm having problems storing the info. I want to store certain lines from the txt into an array so I can access the info later but just cant figure out how to do this..
Can someone please tell me why I can't get the studentsAll[i].id to store its values in the array list? I've tried for hours and have run out of ideas.
What is happening or being stored into studentsAll[i].id instead ?
line 2 extracts into studentsAll[i].id
but then line 3 is storing whatever is in studentsAll[i].list into studentsAll[i].idagain. is this desired? what is being stored into studentsAll[i].list ?
Sorry i typed that out the wrong way.. line 2 is reading in 4 lines of a txt file, then i want to put what studentsAll[i].id reads into an array or a list of arrays so i can re read them later?
line 3 was supposed to read
studentsAll[i].list = studentsAll[i].id
but when I compile the prog I just get 3 empty spaces where I try and output the list to make sure I have stored them correctly.
Here's a nice way to organize your data. Encapsulation and safety is always good. It also promotes organization, so you'd have to go out of your way to get lost.
Thanks for the help my codes changed quite a lot, find it hard to get the private and public and what's available around my head. ^It looks a bit confusing but I think its similar to what I have, but i'm still very confused on how to actually store values so they can be accessed later? Here is my whole program
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
usingnamespace std;
constint MAX_STUDENTS=1000; //max students at uni
constint MAX_COURSES=200; //max courses available
struct Student {
string fName; //students first name
string lName; //students last name
string id; //students ID number
char fees; //students fees 'Y' paid 'N' not paid
string list;
};
struct Courses {
int identifier; //course identifier
string title; //course title
int credits; //credits on offer
int numstudents; //number of students currently enrolled
int enrolledstudents[MAX_STUDENTS];
};
void assignInfo(); //assignment info
void initializeStudentList(Student studentsAll[], int & size);
//void initializeCoursesList(Courses coursesAll[], int & size);
int main() {
assignInfo();
cout << "Students not enrolled in any paper: " << endl;
Student studentsList[MAX_STUDENTS];
int numOfStudents=0; //initialize to 0
initializeStudentList(studentsList, numOfStudents);
//cout << "\n" << "Courses with no students enrolled: " << endl;
//Courses coursesList[MAX_COURSES];
//int numOfCourses=0; //initialize to 0
//initializeCoursesList(coursesList, numOfCourses);
//cout << "\n" << "Courses with NON-EXISTENT students enrolled: " << endl;
}
void assignInfo() {
cout << "************************" << endl;
cout << "* 159.234 Assignment 1 *" << endl;
cout << "* 11079601 N, N *" << endl;
cout << "* S2 2012 *" << endl;
cout << "************************\n" << endl;
}
void initializeStudentList (Student studentsAll[], int & size) {
ifstream inFile("students.txt");
if (!inFile) {
cout << "Data file 'students.txt' was not found" << endl;
exit(EXIT_FAILURE);
} else {
inFile >> size;
if (size <= MAX_STUDENTS) {
for (int i=0; i<size; ++i) {
inFile >> studentsAll[i].fName >> studentsAll[i].lName >> studentsAll[i].id >> studentsAll[i].fees;
studentsAll[i].id = studentsAll[i].list;
cout << studentsAll[i].list << endl;
if (studentsAll[i].fees == 'N') {
cout << studentsAll[i].lName << ", " << studentsAll[i].fName << ": ID " << studentsAll[i].id << ", paid fees N" << endl;
}
}
}
}
}
/*
void initializeCoursesList(Courses coursesAll[], int & size) {
ifstream inFile("courses.txt");
if (!inFile) { //to check the file opens correctly
cout << "Data file 'courses.txt' was not found" << endl;
exit(EXIT_FAILURE);
} else {
inFile >> size;
if (size < MAX_COURSES) {
for (int i=0; i<size; ++i) {
inFile >> coursesAll[i].identifier >> coursesAll[i].title >> coursesAll[i].credits >> coursesAll[i].numstudents;
if (coursesAll[i].numstudents == -1) {
cout << coursesAll[i].identifier << ", " << coursesAll[i].title << ", credits " << coursesAll[i].credits << ", 0 students" << endl;
}
//while (coursesAll[i].numstudents != -1) {
//Courses.enrolledstudents[MAX_STUDENTS] = x;
//}
}
}
}
}
*/
So I am reading the students text, but I want to save all the student.ids I read in to access them later on in the program without re reading the file.