-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
87 lines (73 loc) · 1.47 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct Student
{
std::string Name;
std::string StudentNumber;
};
int main()
{
std::string filename;
std::vector<Student> students;
//1.
std::cout << "Please enter filename(with extension): ";
std::cin >> filename;
std::ifstream file(filename);
if (!file)
std::cerr << filename << " could not be opened!" << std::endl;
else
{
while(file)
{
std::string line;
std::getline(file, line);
if (line == "#") //Ending found
break;
Student tmp;
tmp.Name = line;
std::getline(file, line);
tmp.StudentNumber = line;
students.push_back(tmp);
}
file.close();
if (students.empty())
std::cout << "no data found!" << '\n';
else
{
for (auto student : students)
std::cout << student.Name << ' ' << student.StudentNumber << '\n';
}
std::cout << '\n';
//2.
std::string searchNum;
while (true)
{
std::cout << "Please enter a student number(or x to end): ";
std::cin >> searchNum;
if (searchNum == "X" || searchNum == "x")
break;
else
{
bool found = false;
for (auto student : students)
{
if (searchNum == student.StudentNumber)
{
std::cout << student.Name << " found." << '\n';
found = true;
break;
}
}
if (!found)
std::cout << "not found!" << '\n';
}
}
std::cout << '\n';
}
std::cout << "Press Enter to continue . . . ";
std::cin.ignore();
std::cin.get();
return 0;
}