Arrays

I'm trying to figure out how to print one element of an array based on the user's input. Let's say I made an array that contains the student's first and last name, student ID, e-mail, and GPA. Let's also say that John Smith is the first name on the array. I ask the user to enter in a number, they enter 0 for John Smith and it prints all his information. Here's the code I have so far but it doesn't seem to be working. Any help would be greatly appreciated, thank you in advance.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct studentInfo
{
	string firstName;
	string lastName;
	unsigned int studentID;
	string email;
	float gpa;
};

class studentsProc
{
	public:
		void loadList();
		void retrieveStudent();
		void displayStudent();
		studentsProc();
	
	private:
		studentInfo students[9];
		int size;
		void printList() const;
};

studentsProc::studentsProc()
{
	size = 0;
}

void studentsProc::loadList()
{
	ifstream infile;
	
	infile.open("students.dat");
	size = 0;
	
	while (!infile.eof())
	{
			infile >> students[size].firstName;
			infile >> students[size].lastName;
			infile >> students[size].studentID;
			infile >> students[size].email;
			infile >> students[size].gpa;
			size++;

	}
infile.close();
}

void studentsProc::retrieveStudent() //THIS IS THE FUNCTION I NEED HELP WITH
{
	int number;
	cout << "Please Enter the Number of the Record You Wish to View " << endl;
	cin >> number;
	{
		cout << "First Name: " << students[number].firstName << " || "
		<< "Last Name: " << students[number].lastName << " || "
		<< "Student ID: " << students[number].studentID << " || "
		<< "Student E-mail: " << students[number].email << " || "
		<< "Student GPA: " << students[number].gpa << endl;
	}
}

void studentsProc::displayStudent()
{
	studentsProc::printList();
}

void studentsProc::printList() const
{
	cout << "Student List" << endl;
	for (int i = 0; i < size; i++)
	{
		cout << "First Name: " << students[i].firstName << " || "
			 << "Last Name: " << students[i].lastName << " || "
			 << "Student ID: " << students[i].studentID << " || "
			 << "Student E-mail: " << students[i].email << " || "
			 << "Student GPA: " << students[i].gpa << endl;
	}	
}

int main()
{
	studentsProc student1;
	int choice;

    do
    {
      cout << "Please Choose an Option from this Menu" << endl << endl;
      cout << "1 Retrieve and print a student from the student list " << endl;
      cin >> choice;
      
      switch (choice) 
      {
             case 1:     student1.retrieveStudent();
             			 break;
             default:    cout << "Please enter a value 1-3" << endl;
      }//end switch

   cout << endl << endl;  
   } while (choice != 1);
	
return 0;
}
Last edited on
loadList() is never called, so the array contains no information.
Thanks!!!
Topic archived. No new replies allowed.