As you can see, the comments are not in english as is some of the code. Nor is the code posted anywhere near complete, but i don't think it is necessary to post the enterie thing just for this problem (Unlss otherwise specified by you guys ofcource). I really should start to write my code in english as it would probably be alot easier to work with. If there are any questions regarding the language, variable names and such, post away!
The basic gist of the program is that the user can input data about a person, here; name and a number. The name (varible "navn") gets sent to the "nyStudent" -function, inserted into the object "nyStudent" of type "Student", then the object gets pushed into the vector "studenter". Now the number (variable "studentnummer") can be added to the specified name "navn" if found in the vector.
Now for the problem at hand; how do i achive the exact same thing, but not from user input in the program but rather from a file instead?. In the program the user has the option to save all of the data into a CSV file. Then at a later time, the user can opt to load the data back into the "studenter" -vector to continue working on it.
Now i've figured out how to read and split up the data. My problem is getting that data into the "studenter" -vector again in the correct way, so that when i run my code snippet for getting and displaying the variables, it comes out correct.
Any helpful advice on this issue would be very much appreciated!
Thanks in advance
William
//The following code belongs to the class "Student":.......
Student::Student() //Default konstruktør; Implementsjon
:navn(""),
karakter("")
{}
Student::Student(const string &studNavn) //Konstruktør 1; Implementsjon
:navn(studNavn),
karakter("")
{}
//SET-FUNKSJONER
void Student::setNavn(const string &_Navn){
navn = _Navn;
}
void Student::setStudentnummer(const string &_Studentnr){
studentnummer = _Studentnr;
}
//GET-FUNKSJONER
const string &Student::getNavn() const{
return navn;
}
const string &Student::getStudentnummer() const{
return studentnummer;
}
//The following code belongs to the class "Studentadmin":....
//It has a private vector "vector<Student> studenter;" wich contains user inputted data or data from file.
void Studentadmin::nyStudent(const string &navn){ //tar i mot navn fra bruker som argument
Student nyStudent(navn); //plasserer navn inn i objektet "nyStudent" av typen "Student"
studenter.push_back(nyStudent); //plasserer objektet "nyStudent" inn i vektoren "studenter"
}
//SET-FUNKSJONER
bool Studentadmin::setStudentnr(const string &navn, const string &studnr)
{
Student student(navn); //oppretter et objekt "student" av typen "Student" og putter "navn" inn i objektet
vector<Student>::iterator studenten = find(studenter.begin(), studenter.end(), student); //bruker en iterator til vektoren og leter i fra begynnelse til slutt etter den gitte studenten
if (studenten < studenter.end()){ //hvis studenten befinner seg i vektoren
studenten->setStudentnummer(studnr); //setter inn "studnr" for studenten i setStudentnummer-funksjonen (studenten er her da navnet)
return true; //returnerer true hvis OK
}else
return false; //ellers returnerer false
}
//GET-FUNKSJONER
string Studentadmin::getStudentnr(const string &navn) const
{
Student student(navn); //oppretter et objekt "student" av typen "Student" og putter "navn" inn i objektet
vector<Student>::const_iterator studenten = find(studenter.begin(), studenter.end(), student); //bruker en iterator til vektoren og leter i fra begynnelse til slutt etter den gitte studenten
if (studenten < studenter.end()){ //hvis studenten befinner seg i vektoren
return studenten->getStudentnummer(); //returnerer studentnummeret
}else
return 0; //ellers returnerer 0
}
const Student *Studentadmin::getStudent(int indeks) const //henter ut innholdet for en gitt indeks i vektoren
{
if (indeks >= 0 && indeks < studenter.size()){ //hvis indeksen er innenfor antall elementer
return &studenter[indeks]; //returnerer innholdet på gitt indeks
}else{
return 0; //ellers returneres 0
}
}
void Studentadmin::delOppData(string &inndata)
{
//This function when called, gets passed a filepath from the user as argument. Based on the specified filepath, it opens the CSV type file, reads the contents, splits up the data by delimitersign and then data is supposed to be passed to the "studenter" -vector so the user can modify or add data.
}
//Here comes the 'main' part of the program:...
int main(){
Studentadmin studentdata; //oppretter et objekt, "studentdata", av typen "Studentadmin"
{
visOverskrift(); //viser programmets overskrift
valg menyvalg = (valg) visHovedmeny(); //viser programmets hovedmeny
while (menyvalg != avslutt){ //fortsetter så lenge brukeren ikke velger avslutt
visMenyvalg(menyvalg, studentdata); //kaller på menytreet, sender inn menyvalg og objekt
menyvalg = (valg) visHovedmeny(); //setter variabelen "menyvalg" lik variabelen "valg" fra funksjonen visHovedmeny();
}
}
return 0;
}
//This function contains most of the clientprogram:.....
int visMenyvalg(valg menyvalg, Studentadmin &data) {
//Here is a submenu where the user can choose to open a CSV file of previous stored data so he can modify the data as he sees fit. The user selects a filepath wich is passed to the "delOppData" -function.
//User input, all user input is of type string:....
do{
cout << " Navn: ";
getline(cin, navn, '\n'); //navn fra bruker
cout << endl;
}while(navn.empty()); //gjenta hvis bruker ikke skriver inn noe
do {
cout << " Studentnummer: ";
getline(cin, studentnummer, '\n'); //studentnummer fra bruker
cout << endl;
}while(studentnummer.empty()); //gjenta hvis bruker ikke skriver inn noe
//The user input "navn" and "studentnummer" gets passed to their respective set-functions.
data.nyStudent(navn); //setter studentens navn
data.setStudentnr(navn, studentnummer); //setter studentens studentnummer
//The following code is used to extract data from the vector and view it:...
for (int indeks = 0; indeks < data.getAntStudenter(); indeks++) //lister opp og viser tilgjengelige studenter
{
const Student *studenten = data.getStudent(indeks); //går igjennom indeksen til hvert element
cout << " <" << (indeks + 1) << ">"; //indeksnummer
cout << " :: " << studenten->getStudentnummer() << " :: "; //studentnummer
cout << studenten->getNavn() << endl; //navn
}
//here comes a submenu for the user to specify a file to wich the data can be stored for later use and the ability to save the data to file
return 0;
}
End of program.....

New Topic/Question
Reply




MultiQuote





|