Use of Map function

Im using the map function to store student information for an assignment. IN my gradebook.cpp I have it to where you can list the records of students shown. I want it to throw out an error saying there are no records if nothing is currently stored. How would I do this? Or would it be easier to store the information in an array?
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
// Gradebook.cpp

#include <iostream>
#include "GradeBook.h"


GradeBook::GradeBook(void)
{
	records = 0;
}

void GradeBook::showRecords()
{
	for (map<string, StudentGradeInfo>::iterator it = studentMap.begin();
		it != studentMap.end();
		it++)
	{
		cout << it->second;
	}
}

void GradeBook::addRecord()
{
	if (records < 3)
	{
		string studentID, studentName, major;
		
		cout << "Enter ID: ";
		getline(cin, studentID);
		
		
		//only enter if the ID does not exist already
		if (studentMap.find(studentID) == studentMap.end())
		{
			cout << "Enter Name: ";
			getline(cin, studentName);

			cout << "Enter Major: ";
			getline(cin, major);

			int size = 0;
			cout << "\nHow many grades you want to add? (max allowed: 10) ";
			cin >> size;
			if (size > 10)
			{
				cout << "No more than 10 grades are allowed to be stored per student.\nPlease enter a correct amount of grades: ";
				cin >> size;
			}
			double grades[10];
			for (int i = 0; i < size; i++)
			{
				double grade;
				cout << "\nEnter grade " << i + 1 << ": ";
				cin >> grade;

				if (grade < 0 || grade >100)
				{
					cout << "\nEnter grade between 0 to 100";
					i--;
					continue;
				}
				grades[i] = grade;
			}
			studentGradeInfo[records].addGradeInfo(studentName, studentID, major, grades, size);

			studentMap[studentID] = studentGradeInfo[records];
			records++;

		}


		else
		{
			cout << "\nThis student already exists in the Gradebook and wont be re-added.";
		}
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Gradebook.h
#pragma once
#include "StudentGradeInfo.h"
#include <map>

class GradeBook
{
private:

	int records;
	StudentGradeInfo studentGradeInfo[3];
	map<string, StudentGradeInfo> studentMap;
	// I got the info for how to use mapping from the following
	// https://www.geeksforgeeks.org/map-associative-containers-the-c-standard-template-library-stl/
	// http://www.cplusplus.com/reference/map/map/map/
	//http://www.cplusplus.com/reference/map/map/

public:

	GradeBook(void);
	void addRecord();
	void showRecords();
};

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
//Source.cpp
#include <string>
#include <iostream>
#include "GradeBook.h"

using namespace std;

int main()
{
	bool valid = true;
	char choice;
	GradeBook gb;
	do
	{
		cout << "\nWelcome to the Gradebook. To begin select an option from the main menu.\n";
		cout << "To add a student enter (A).\n";
		cout << "To list all students in the gradebook enter (L).\n";
		cout << "To quit the program enter (Q).\n";
		cout << "Choice: ";
		cin >> choice;

		switch (choice)
		{
		case 'A':
			gb.addRecord();
			break;

		case 'a':
			gb.addRecord();
			break;

		case 'L':
			gb.showRecords();
			break;

		case 'l':
			gb.showRecords();
			break;

		case 'Q':
			exit(1);

		case 'q':
			exit(1);
		default:
			cout << "That is not a valid option, please choose an option from the list. " << endl;
			valid = false;
		}
	} while (!valid);

	system("pause");
	return 0;
}
Last edited on
if studentMap.empty() {...} else {...}
Registered users can post here. Sign in or register to post.