Untitled

 avatar
unknown
plain_text
24 days ago
3.6 kB
7
Indexable
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void checkInbox();
int displayMenu();
void WriteToOutbox();

int main() {
  // TODO: Display a menu of options and prompt the user to choose from those
  // available (including quitting)
  switch (displayMenu()) {
  case -1:
    cout << "Error selection out of bound" << endl;
    break;
  case 1:
    checkInbox();
    main();
  case 2:
    WriteToOutbox();
    main();
  case 5:
    return 0;

  default:
    /*cout << "default" << endl;*/
    main();
  }
  // Make sure to handle invalid input!

  return 0;
}

void checkInbox() {
  // create a vector of strings to store the filenames
  vector<string> filenames = {};

  for (const auto &entry : std::filesystem::directory_iterator("inbox")) {
    if (entry.is_regular_file()) { // Ensure it's a file, not a directory
      string filename = entry.path();
      // add each filename to a vector of strings
      filenames.push_back(filename);
    }
  }

  while (true) {
    // Present the user with a numbered list of their inbox emails
    //       include the filename and the subject
    int counter = 1;
    for (const string &s : filenames) {
      // read the email
      ifstream email;
      email.open(s);
      string line = "";
      getline(email, line); // this will read the recipient email
      getline(email, line); // this will read the subject line

      cout << counter << ". filename:" << s << "; subject line:" << line
           << endl;
      email.close();
      // increment the list number
      counter++;
    }

    // TODO: Ask the user for the number of the email to open, or 0 to return to
    // the main menu
    //       if they open an email, display its full contents, then ask if they
    //       want to open another

    int choice = 0;
    cout << "Enter the number of the email to open(0 to go back to main menu):";
    cin >> choice;
    cin.ignore();

    // validate the choice
    if (choice == 0) {
      break;
    }
    if (choice < 1 && choice > filenames.size()) {
      // out of range
      cout << "error, choice out of range, returing to main menu" << endl;
    }

    // print out the content of the chosen email
    ifstream email;
    string line;
    email.open(filenames.at(choice - 1));
    while (getline(email, line)) {
      cout << line << endl;
    }
  }
  main();
}

int displayMenu() {
  int num = 0;
  cout << "Email Management System" << endl;
  cout << "1. Check Inbox" << endl;
  cout << "2. Write Email" << endl;
  cout << "3. Filter Spam" << endl;
  cout << "4. Filter Spam + Ham" << endl;
  cout << "5. Exit" << endl;
  cout << "Enter your choice: ";
  cin >> num;
  cin.ignore();
  if (num < 1 || num > 5) {
    return -1;
  } else {
    return num;
  }
}

void WriteToOutbox() {
  string recipient = "";
  cout << "Enter recipient email" << endl;
  cin >> recipient;
  cin.ignore();

  string subjectLine = "";
  cout << "Enter the subject line" << endl;
  cin >> subjectLine;
  cin.ignore();

  int emptyCounter = 0;
  string body = "";
  string line = "";
  cout << "Please enter email body. Enter 2 empty lines to finish:\n";
  while (true) {
    getline(cin, line);
    if (line.empty()) {
      emptyCounter++;
    } else {
      emptyCounter = 0;
    }
    if (emptyCounter == 2) {
      break;
    }
    body += line + '\n';
  }
  string filename = "";
  cout << "Enter the filename" << endl;
  cin >> filename;
  cin.ignore();

  ofstream EmailFile(filename);
  EmailFile << recipient << endl << subjectLine << endl << body << endl;
  EmailFile.close();
}
Leave a Comment