Write a program that lets you know if you can have a key or not, based on your role at the school.
First ask for the user’s role at the school. They can be a student, administrator, or a teacher. (And remember that capitalization is important! ‘Student’ is not the same as ‘student’.)
Example 1: Administrator or Teacher
For example, if this was the input:
Are you an administrator, teacher, or student?: teacher
This should be the output:
Administrators and teachers get keys!
Example 2: Student
And if this was the input:
Are you an administrator, teacher, or student?: student
This should be the output:
Students do not get keys!
(Note: You should also be able to handle a situation where the user enters a value other than administrator, teacher or student and tell them they must be one of the three choices!)
Example 3: Other
If they input anything else:
Are you an administrator, teacher, or student?: secretary
This should be the output:
You can only be an administrator, teacher, or student!
Ответ:
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main() {
// відкриваємо файл для зчитування
ifstream file(«products.txt»);
// перевіряємо, чи файл відкрито успішно
if (!file) {
cerr << «Не вдалося відкрити файл» << endl;
return 1;
}
// отримуємо поточний час
time_t now = time(nullptr);
// проходимо по файлу
string name;
time_t production_time;
while (file >> name >> production_time) {
// конвертуємо час виробництва у структуру tm
struct tm* ptm = localtime(&production_time);
// порівнюємо рік виробництва з поточним роком
if (ptm->tm_year < (1900 + (now — (2 * 365 * 24 * 60 * 60)) / (365 * 24 * 60 * 60))) {
cout << name << endl;
}
}
// закриваємо файл
file.close();
return 0;
}
Объяснение:
Ми будемо зберігати дату та час виробництва кожного товару у вигляді time_t значення. За допомогою функції localtime ми можемо конвертувати це значення у структуру tm, щоб дізнатися рік виробництва товару.
Потім ми можемо порівняти рік виробництва кожного товару з поточним роком, щоб знайти товари, строк яких більше 2 років.