@peter00 Was hast Du in der Vorlesung gemacht?
#include <cstdlib>
#include <stdexcept>
#include <utility>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
int main()
{
std::vector<std::pair<std::string, std::string>> buzzword_employee_map {
{ "Kaffee", "Paul" },
{ "Bohnen", "Paul" },
{ "Saft", "Phil" },
{ "Wasser", "Phil" }
};
auto output_filename_prefix{ "new_mail_to_ " };
auto output_filename_postfix{ ".txt" };
auto mail_greeting{ "Hi" };
auto mail_body{ "du hast eine neue Nachricht zum Thema " };
try {
std::cout << "Filename?\n";
std::string input_filename;
if (!(std::cin >> input_filename))
throw std::runtime_error{ "Input error" };
std::ifstream is{ input_filename };
if (!is.is_open())
throw std::runtime_error{ "File couldn't be opened for reading" };
std::string text(std::istreambuf_iterator<char>(is), {});
std::string buzzword;
std::string employee;
for (auto pair : buzzword_employee_map) {
std::cout << pair.first << '\n';
if (text.find(pair.first) != text.npos) {
buzzword = pair.first;
employee = pair.second;
break;
}
}
if (!buzzword.length()) {
std::cout << "No Buzzword found.\n\n";
return EXIT_SUCCESS;
}
std::stringstream output_filename;
output_filename << output_filename_prefix << employee << output_filename_postfix;
std::ofstream os{ output_filename.str() };
if (!os.is_open())
throw std::runtime_error{ "File couldn't be opened for writing" };
os << mail_greeting << ' ' << employee << ",\n" << mail_body << buzzword << ".\n";
}
catch (std::runtime_error & e) {
std::cerr << e.what() << " :(\n\n";
return EXIT_FAILURE;
}
catch (...) {
std::cerr << "An unknown error occured :(\n\n";
return EXIT_FAILURE;
}
}