Please help! I need help on outputting the correct info from users input in C++. My binarySearchSSN function asks user for a SSN from the sorted SSN pointer array but doesn't print the correct SSN for some.
My header file and cpp file:
My SortingSSN function:
My main with array of pointers and output showing that it works for some:
Please help and thank you.
1 2 3 } void binarySearchSSN(Student studArray[], int size) 1/grades.h #ifndef GRADES_H #define GRADES_H #include
#include #include #include #include 138 139 140 141 142 //asks user for SSN cout > ssn; 143 6 7 144 145 146 147 148 149 10 11 using namespace std; struct Student int first = 0, last = size - 1, middle, position = 1; bool found = false; //first array element 7/last array element //midpoint of search //position of search value //flag 12 while(!found && first ssn) //if value is in lower half 24 25 162 void menu(); void readCsvToArray(Student array[], int size, ifstream& file); void sortByLastname (Student studArray[], Student* indexBy Lastname[]); void sortBySSN(Student studArray[], Student* indexBySSN[]); void binarySearchLastName(Student studArray[], int size); void binarySearchSSN(Student studArray[], int size); #endif 163 last = middle - 1; 27 28 165 else 29 166 167 first = middle + 1; //if value is in upper half 168 if(position != -1) //if position was found then output 170 171 172 173 174 175 176 Student s = studArray[position); cout (studArray[j].ssn)) minIndex = j; //here we swap ith position with the minimum index variable Student s = studArray[minIndex]; studArray[minIndex] = studArray[i]; studArray[i] = s; //we assign it to the ith position of indexByssn variable indexBySSN[i] = estudArray[i]; 96 } 15 16 17 // global number of students const int NUM_STUDENTS = 20; int main() Student studArray[NUM STUDENTS]; // array of pointers Student* indexBySSN[NUM_STUDENTS]; Student* indexByLastname[NUM STUDENTS]; 26 //opening csv file ifstream readFile; readFile.open("students.csv"); PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL 1,Alfalfa, Aloysius, 123-45-6789,40,90, 100,83,49,D- 2,Alfred, University, 123-12-1234,41,97,96,97,48,D+, 3, Gerty,Gramma, 567-89-0123,41,80,60,40,44,C 4, Android, Electric, 087-65-4321,42,23,36,45,47,B- 5,Bumpkin, Fred, 456-78-9012,43,78,88,77,45,- 6, Rubble, Betty, 234-56-7890,44,90,80,90,46,C- 7, Noshow, Cecil, 345-67-8901,45,11,-1,4,43,F 8, Buff, Bif,632-79-9939,46,20,30,40,50,B+ 9,Airpump, Andrew, 223-45-6789,49,1,90,100,83,A 10, Backus, Jim, 143-12-1234,48,1,97,96,97,A+, 11, Carnivore, Art, 565-89-0123,44,1,80,60,40,D+ 12, Dandy, Jim, 087-75-4321,47,1,23,36,45,C+ 13, Elephant, Ima, 456-71-9012,45,1,78,88,77,B- 14, Franklin, Benny,234-56-2890,50,1,90,80,90,B- 15, George, Boy, 345-67-3901,40,1,11,-1,4,B 16, Heffalump, Harvey,632-79-9439,30,1,20,30,40, 17, de Paul, Martin, 829-34-3412,34,49,12,90,34,B+ 18, Madrigal, Jay, 309-12-4312,18,2,34,34,5,F 19, Osborne, Demi, 025-24-7821,50,98, 100,89,99,A 20, Garrett, Keith, 723-35-9882,23,53,10,34,84,C+ 1. Print data in ascending order by last name 2. Print data in ascending order by SSN 3. Search by last name 4. Search by SSN 5. Exit Select a Menu:4 Enter SSN: 456-71-9012 ID Last Name First Name SSN Grade 2 Alfred University 123-12-1234 D+ Enter SSN: 723-35-9082 ID Last Name First Name SSNGrade 20 Garrett Keith 723-35-9082 C+ 1 2 3 } void binarySearchSSN(Student studArray[], int size) 1/grades.h #ifndef GRADES_H #define GRADES_H #include #include #include #include #include 138 139 140 141 142 //asks user for SSN cout > ssn; 143 6 7 144 145 146 147 148 149 10 11 using namespace std; struct Student int first = 0, last = size - 1, middle, position = 1; bool found = false; //first array element 7/last array element //midpoint of search //position of search value //flag 12 while(!found && first ssn) //if value is in lower half 24 25 162 void menu(); void readCsvToArray(Student array[], int size, ifstream& file); void sortByLastname (Student studArray[], Student* indexBy Lastname[]); void sortBySSN(Student studArray[], Student* indexBySSN[]); void binarySearchLastName(Student studArray[], int size); void binarySearchSSN(Student studArray[], int size); #endif 163 last = middle - 1; 27 28 165 else 29 166 167 first = middle + 1; //if value is in upper half 168 if(position != -1) //if position was found then output 170 171 172 173 174 175 176 Student s = studArray[position); cout (studArray[j].ssn)) minIndex = j; //here we swap ith position with the minimum index variable Student s = studArray[minIndex]; studArray[minIndex] = studArray[i]; studArray[i] = s; //we assign it to the ith position of indexByssn variable indexBySSN[i] = estudArray[i]; 96 } 15 16 17 // global number of students const int NUM_STUDENTS = 20; int main() Student studArray[NUM STUDENTS]; // array of pointers Student* indexBySSN[NUM_STUDENTS]; Student* indexByLastname[NUM STUDENTS]; 26 //opening csv file ifstream readFile; readFile.open("students.csv"); PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL 1,Alfalfa, Aloysius, 123-45-6789,40,90, 100,83,49,D- 2,Alfred, University, 123-12-1234,41,97,96,97,48,D+, 3, Gerty,Gramma, 567-89-0123,41,80,60,40,44,C 4, Android, Electric, 087-65-4321,42,23,36,45,47,B- 5,Bumpkin, Fred, 456-78-9012,43,78,88,77,45,- 6, Rubble, Betty, 234-56-7890,44,90,80,90,46,C- 7, Noshow, Cecil, 345-67-8901,45,11,-1,4,43,F 8, Buff, Bif,632-79-9939,46,20,30,40,50,B+ 9,Airpump, Andrew, 223-45-6789,49,1,90,100,83,A 10, Backus, Jim, 143-12-1234,48,1,97,96,97,A+, 11, Carnivore, Art, 565-89-0123,44,1,80,60,40,D+ 12, Dandy, Jim, 087-75-4321,47,1,23,36,45,C+ 13, Elephant, Ima, 456-71-9012,45,1,78,88,77,B- 14, Franklin, Benny,234-56-2890,50,1,90,80,90,B- 15, George, Boy, 345-67-3901,40,1,11,-1,4,B 16, Heffalump, Harvey,632-79-9439,30,1,20,30,40, 17, de Paul, Martin, 829-34-3412,34,49,12,90,34,B+ 18, Madrigal, Jay, 309-12-4312,18,2,34,34,5,F 19, Osborne, Demi, 025-24-7821,50,98, 100,89,99,A 20, Garrett, Keith, 723-35-9882,23,53,10,34,84,C+ 1. Print data in ascending order by last name 2. Print data in ascending order by SSN 3. Search by last name 4. Search by SSN 5. Exit Select a Menu:4 Enter SSN: 456-71-9012 ID Last Name First Name SSN Grade 2 Alfred University 123-12-1234 D+ Enter SSN: 723-35-9082 ID Last Name First Name SSNGrade 20 Garrett Keith 723-35-9082 C+