read data functions

I have an assignment to create a C++ program to use bubble sort to arrange the names in my text file by last name. I'm having trouble setting my read data function to read the file, store the output in a temp variable and display the output outside of the text file

This is what I have so far:
#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>

using namespace std;

struct Date
{
int month, day, year;
};

struct Student
{
string firstName;
string lastName;
Date bday;

};

void ascNameSort(Student* ascStudent);

void decNameSort(Student*decStudent);

void printData(Student* nameList);

void readData(ifstream& StudentData, Student* StuData);
int main(){
int choice;
ifstream txtFile;
//int student[10];
struct Student studentfile[10];
txtFile.open("data.txt");



cout << "Ascending order" <<endl;
cout << "Descending order" <<endl;
readData(txtFile,studentfile);

ascNameSort(studentfile);
printData(txtFile);






txtFile.close();
return 0;
}
void ascNameSort(Student* ascStudent){// smallest to largest
int i,j;
Student temp;
int flag=1;
for(i=0; i<10;i++){
flag=0;
for(j=0; j<9; i++){
if(ascStudent[j+1].lastName < ascStudent[j].lastName){
temp=ascStudent[j];
ascStudent[j]=ascStudent[j+1];
ascStudent[j+1]=temp;
flag=1;
}
}
}
return;
}

void decNameSort(Student*decStudent){// largest to smallest

int i,j;
Student temp;
int flag=1;
for(i=0; i<10; i++){
flag=0;
for(j=0; j<9; i++){
if(decStudent[j+1].lastName > decStudent[j].lastName){
temp=decStudent[j];
decStudent[j]=decStudent[j+1];
decStudent[j+1]=temp;
flag=1;
}
}
}
return;
}

void printData(Student* nameList){
cout<<nameList->firstName<<nameList->lastName<<nameList->bday.month<<nameList->bday.day
<<nameList->bday.year<<endl;

}
void readData(ifstream& StudentData, Student* StuData){
int file;
int tempmonth, tempday, tempyear;
string tempfirstname, templastname;
while(StudentData >> file){
StuData=tempfirstname(file);
StuData->templastname;
StuData->tempmonth;
StuData->tempday;
StuData->tempyear;
}



}

This is my text file:
Yvon Feaster 10 7 1963
Toby Dean 10 7 1963
Mary Poppins 1 15 1953
Mitch Rapp 12 15 1952
Mickey Mouse 11 18 1928
Grace Hopper 12 9 1906
Steve Jobs 2 24 1955
Tom Selleck 1 29 1945
Jason Bourne 9 13 1970
Robert Ludlum 5 25 1927
Last edited on
the first thing to do when getting help is to at least give a program that can compile. also put the code in code tags <> on the side bar editor.

printData(txtFile); //txtfile is a file. printdata takes something else.
StuData=tempfirstname(file); //this appears to be random typing... file is of all things, an integer, tmpfirstname is a string, and studata is a pointer. I can't even guess what you wanted to do here, and as a side note, use names that make sense, not "int file" which is just confusing as ppl think its going to be an ifstream or something.

StuData->templastname; //these statements do NOTHING at all.
StuData->tempmonth; its like saying int x; x; … x; compiles but does nothing.
StuData->tempday;
StuData->tempyear;

what you want is

int counter = 0;
while( StudentData >> StudentData[counter].firstname) //this assumes each line is correct. if you have a bad line in the file, this code will not work properly.
{
StudentData >> StudentData[counter].lastname;
StudentData >> StudentData[counter].date.month;
StudentData >> StudentData[counter].date.day;
StudentData >> StudentData[counter++].date.year; //note the counter increment
}

the idea is that you have an array of your students. Students has lots of parts, so you have to read each part and put it into the field where it belongs, including all the parts for the date part, and you have to move through your array of students, filling in each location one by one (counter).
Last edited on
Topic archived. No new replies allowed.