Struct: Help with my grade function please
Jul 21, 2014 at 9:40pm UTC
My code has no errors. I'm just having problems with getting the grades of the file sorted. grades is not a struct, but is a int inside struct School. Below is what I;m having problems with(this is just the code I started working with,I made a few changes but it didn't work,I have no clue)
void selectionSortG()
{
Students min;
int key;
for (int i = 0; i < size; i++)
{
min = student[i];
key = i;
for (int k = i; k < size; k++)
{
if (student[k].name.firstName < min.name.firstName)
{
min = student[k];
key = k;
}
}
student[key] = student[i];
student[i] = min;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int data = 1000;
const int studentSize = 1000;
struct Name
{
string firstName;
string lastName;
string middleName;
bool names()
{
for (int i = 0; i<studentSize; i++)
{
if (&Name::firstName && &Name::lastName && &Name::middleName)
{
return true ;
}
else
return false ;
}
}
};
struct Dates
{
string exceptedGradution;
string dob;
string admissionDate;
bool dates()
{
for (int i = 0; i<studentSize; i++)
{
if (&Dates::exceptedGradution && &Dates::dob && &Dates::admissionDate)
{
return true ;
}
else
return false ;
}
}
};
struct Address
{
string address;
string city;
string state;
string zip;
};
struct Students
{
Name name;
Address address;
Dates dates;
int grades;
};
struct School
{
Students student[20];
int size;
void selectionSortF()
{
Students min;
int key;
for (int i = 0; i < size; i++)
{
min = student[i];
key = i;
for (int k = i; k < size; k++)
{
if (student[k].name.firstName < min.name.firstName)
{
min = student[k];
key = k;
}
}
student[key] = student[i];
student[i] = min;
}
}
void selectionSortL()
{
Students min;
int key;
for (int i = 0; i < size; i++)
{
min = student[i];
key = i;
for (int k = i; k < size; k++)
{
if (student[k].name.lastName < min.name.lastName)
{
min = student[k];
key = k;
}
}
student[key] = student[i];
student[i] = min;
}
}
void selectionSortG()
{
Students min;
int key;
for (int i = 0; i < size; i++)
{
min = student[i];
key = i;
for (int k = i; k < size; k++)
{
if (student[k].name.firstName < min.name.firstName)
{
min = student[k];
key = k;
}
}
student[key] = student[i];
student[i] = min;
}
}
void print()
{
for (int i = 0; i<size; i++)
{
cout << student[i].name.firstName << "\t" << student[i].name.lastName << "\t" << student[i].grades << endl;
}
}
};
int main()
{
School s1;
ifstream file;
file.open("data.txt" );
for (int i = 0; !file.eof(); i++)
{
file >> s1.student[i].name.firstName >> s1.student[i].name.lastName >> s1.student[i].name.middleName
>> s1.student[i].address.address >> s1.student[i].address.city >> s1.student[i].address.state >> s1.student[i].address.zip
>> s1.student[i].dates.dob >> s1.student[i].dates.exceptedGradution >> s1.student[i].dates.admissionDate >> s1.student[i].grades;
s1.size = i;
}
while (file.eof())
{
char user;
s1.print();
cout << "For sorting using first name enter: 'f' for using last name enter 'l' for using grade enter 'g'" << endl;
cin >> user;
if (user == 'f' )
{
s1.selectionSortF();
}
else if (user == 'l' )
{
s1.selectionSortL();
}
else if (user == 'g' )
{
s1.selectionSortG();
}
}
return 0;
}
Last edited on Jul 22, 2014 at 3:08am UTC
Jul 22, 2014 at 2:07am UTC
Better to comment in code where you want us to look for you..
Jul 22, 2014 at 4:15am UTC
BTW, you press 'g' but check for FirstName !
1 2 3 4 5
if (student[k].name.firstName < min.name.firstName)
{
min = student[k];
key = k;
}
Last edited on Jul 22, 2014 at 6:54am UTC
Topic archived. No new replies allowed.