Find highest and lowest marks

Hi,
I'm a complete new beginner in C++.

I have bee tasked to create a console app that extracts data from a text file.
The text file consists of Student ID's, Student Names and Mark achieved.

I have figured out how to create a menu and cout the results, but I'm struggling to cout the highest mark gained and lowest mark gained. (These need to be two different functions)

I also need to sort the Students in descending order according to the mark they received.

Please help.

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

void menu(void);
void storeData(void);
void retrieveData(void);
void Highest_Mark(void);
string * split(string, char);

const char FileName[] = "testData.txt";

int main () {
	menu();
	return 0;
}

void menu(void) {
	char userChoice = ' ';
	
	system("cls");

	cout << "\n\n                        ******************" << endl;
	cout << "                        *                *" << endl;
	cout << "                        *  *** MENU ***  *" << endl;
	cout << "                        *                *" << endl;
	cout << "                        ****************** \n\n" << endl;
	cout << "                      (1) Store Student Data" << endl;
	cout << "                      (2) Retrieve Student Data" << endl;
	cout << "                      (3) Highest Student Mark" << endl;
	cout << "                       Please enter your choice:  ";
	cin >> userChoice;

	while (userChoice != 'e')

		switch (userChoice)
	{
		case '1':
			{
				storeData();
			}
			break;

		case '2':
			{
				retrieveData();
			}
			break;

		case '3':
			{
				Highest_Mark();
			}
			break;

		default:
			cout << "Invalid Choice" << endl;
			break;

	}


}

void storeData(void){

	system("cls");

	string id = " ";
	string name = " ";
	string mark = " ";

	char response = 'Y';

	ofstream outStudentData("testData.txt", ios::app);


	outStudentData.is_open();


	do{
		cout << endl;
		getline(cin,id);
		cout << "Enter Student ID: ";
		getline(cin, id);

		cout << "Enter Student Name: ";
		getline(cin, name);

		cout << "Enter Student Mark: ";
		getline(cin, mark);

		outStudentData << id << " " << name << " " << mark << endl;

		cout << "\n Enter another record? (Y/N) ";
		cin >> response;

	}while (response == 'y' || response == 'Y');

	outStudentData.close();

	menu();

}

void retrieveData(void){

	system("cls");

	int recordCount = 0;
	string lineBuffer;

	ifstream inStudentData("testData.txt", ios::in);

	inStudentData.is_open();

	cout << "Show Record(s):" << endl;

	getline	(inStudentData, lineBuffer);

	while(!inStudentData.eof())
	{
		string * theFields = split(lineBuffer, ' ');

		recordCount++;

		cout << "\n Record No. " << recordCount;

		cout << endl;

		cout << "___________________________________________" << endl;
		cout << "\nStudent Number:     " << theFields[0] << endl;
		cout << "First Name:         " << theFields[1] << endl;
		cout << "Mark:               " << theFields[2] << endl;
		cout << "___________________________________________" << endl;

		getline(inStudentData, lineBuffer);
	}

	inStudentData.close();
	cout << endl;

	system("pause");

	menu();

}


string * split(string theLine, char theDeliminator){

	int splitCount = 0;
	for(int i = 0; i < theLine.size(); i++){
		if (theLine[i] == theDeliminator)
			splitCount++;
	}
	splitCount++;

	string* theFieldArray;
	theFieldArray = new string[splitCount];

	string theField = "";
	int commaCount = 0;

	for(int i = 0; i < theLine.size(); i++){
		if (theLine[i] != theDeliminator) {
			theField += theLine[i];
		}
		else {
			theFieldArray[commaCount] = theField;
			theField = "";
			commaCount++;
		}
	}
	theFieldArray[commaCount] = theField;

	return theFieldArray;
}



The text file looks as follow:

<text>
2001 jaeery 89
2002 larry 09
2003 bunti 59
2004 james 89
2007 harases 44
2034 mark 73
2033 mathew 52
2048 mary 35
2045 eino 46
2042 rukoro 84
2043 ravid 83
2046 katji 82
2067 andrew 51
2021 andreas 83
2023 robert 45
2051 shipanga 85
2052 shikongo 35
2601 paulus 24
2701 david 43
2901 gariseb 55
</text>
Last edited on
Welcome to programming. What you thought was programming was in fact memorising syntax. This is programming - thinking about how to solve a problem using the tools available.

How do YOU know which score is the lowest? Look at that list. Start at the top. Now tell me which is the lowest score. How did you do that? It might be something like:

Look at first score. That's the lowest score so far.
Look at next score. If next score is lower than the lowest score so far, it's the new lowest score so far.
Repeat until reached last score.

Now turn that into code.
Thank you Moschops for the guideline.

I have a code snippet for finding the highest value.

<code>
#include "stdafx.h"
#include <conio.h>
#include <fstream>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
system("cls");

int x,y,z,biggest;

cout << "Enter 3 integers : ";

cin>>x>>y>>z;

biggest=x>y?(x>z?x:z):(y>z?y:z);

cout << "The biggest integer out of the 3 integers you typed ";
cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n";

getch();

return 0;
}

</code>

My problem is integrating the code into my main code.

My main problem is I'm struggling to put the text file into array and drawing the values from the 3rd column.

Can you please assist me in the coding...
Last edited on
system("cls");
do not use.

for this program learn how to sort variables in a data structure. bubble sorting is the simplest to understand.
My problem is integrating the code into my main code.


Please don't. Grabbing code you don't understand and trying to whack it into something it's not suitable for is not a good idea.

My main problem is I'm struggling to put the text file into array and drawing the values from the 3rd column.

You have chosen a particularly complicated way of doing this. You can just read the values one at a time, rather than read in a whole line and then have to break it into pieces.

Here is some code that does what you've done above for reading data, but simpler, and presents the data in a way that it then easy to examine (see the end where I output the values for an example of how to get to the values in the vector).

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

using namespace std;


struct Student
{
  int id;
  string name;
  int score;
};

int main()
{

  ifstream inStudentData("testdata.txt", ios::in);

  Student tempStudentObject;
  vector<Student> aVectorOfStudents;
  while (1)
  {
    // if read fails, stop trying.
    if (!(inStudentData >> tempStudentObject.id)) break;
    inStudentData >> tempStudentObject.name;
    inStudentData >> tempStudentObject.score;
    aVectorOfStudents.push_back(tempStudentObject);
  }

  // aVectorOfStudents now contains all the data in an easy to access format

   for (int i = 0; i< aVectorOfStudents.size(); ++i)
   {
       cout << i << " " << aVectorOfStudents[i].id << " "
              << aVectorOfStudents[i].name << " "
              << aVectorOfStudents[i].score << endl;
   }
}
Thanks a lot Moschops...

I understand that part better now.

1) I now need to find the student(s) with the highest and lowest marks and output them in the following manner:

Student No: xxxx
First Name: xxxx
Marks: xxxx

Note: I saw from the text file that there are two students with the same highest score.

2) I then need to sort the text file data in descending order according to the marks obtained.
Topic archived. No new replies allowed.