|
| 1 | +#include <iostream> |
| 2 | +#include <limits> |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +struct Student { |
| 9 | + string name; |
| 10 | + int score; |
| 11 | +}; |
| 12 | + |
| 13 | +int readPositiveCount() { |
| 14 | + int count = 0; |
| 15 | + while (true) { |
| 16 | + cout << "How many students? "; |
| 17 | + if (!(cin >> count)) { |
| 18 | + cout << "Invalid number. Try again.\n"; |
| 19 | + cin.clear(); |
| 20 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 21 | + continue; |
| 22 | + } |
| 23 | + if (count <= 0) { |
| 24 | + cout << "Please enter a positive number.\n"; |
| 25 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 30 | + return count; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +int readScore(const string& studentName) { |
| 35 | + int score = 0; |
| 36 | + while (true) { |
| 37 | + cout << "Score for " << studentName << " (0-100): "; |
| 38 | + if (!(cin >> score)) { |
| 39 | + cout << "Invalid score. Enter a number from 0 to 100.\n"; |
| 40 | + cin.clear(); |
| 41 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 42 | + continue; |
| 43 | + } |
| 44 | + if (score < 0 || score > 100) { |
| 45 | + cout << "Score out of range. Enter a value from 0 to 100.\n"; |
| 46 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 51 | + return score; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +int main() { |
| 56 | + const int count = readPositiveCount(); |
| 57 | + vector<Student> students; |
| 58 | + students.reserve(static_cast<size_t>(count)); |
| 59 | + |
| 60 | + for (int i = 0; i < count; ++i) { |
| 61 | + Student student; |
| 62 | + cout << "Student name " << (i + 1) << ": "; |
| 63 | + getline(cin, student.name); |
| 64 | + student.score = readScore(student.name); |
| 65 | + students.push_back(student); |
| 66 | + } |
| 67 | + |
| 68 | + int total = 0; |
| 69 | + int passCount = 0; |
| 70 | + size_t highestIndex = 0; |
| 71 | + size_t lowestIndex = 0; |
| 72 | + |
| 73 | + for (size_t i = 0; i < students.size(); ++i) { |
| 74 | + total += students[i].score; |
| 75 | + if (students[i].score >= 60) { |
| 76 | + ++passCount; |
| 77 | + } |
| 78 | + if (students[i].score > students[highestIndex].score) { |
| 79 | + highestIndex = i; |
| 80 | + } |
| 81 | + if (students[i].score < students[lowestIndex].score) { |
| 82 | + lowestIndex = i; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + const double average = static_cast<double>(total) / students.size(); |
| 87 | + |
| 88 | + cout << "\nStudents:\n"; |
| 89 | + for (const Student& student : students) { |
| 90 | + cout << "- " << student.name << ": " << student.score << '\n'; |
| 91 | + } |
| 92 | + |
| 93 | + cout << "Average: " << average << '\n'; |
| 94 | + cout << "Highest: " << students[highestIndex].name |
| 95 | + << " (" << students[highestIndex].score << ")\n"; |
| 96 | + cout << "Lowest: " << students[lowestIndex].name |
| 97 | + << " (" << students[lowestIndex].score << ")\n"; |
| 98 | + cout << "Passed: " << passCount << "/" << students.size() << '\n'; |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
0 commit comments