//////////////////////////////////////// // Spam Cupcakes //////////////////////////////////////// // Standard included things #include #include #include #include using namespace std; //////////////////////////////////////// // Function prototypes void print_menu(); void load_emails(vector &, vector &); void show_emails(vector , vector ); int count_appearances(string, vector); void print_vector(vector); vector unique_strings (vector); vector get_names(string, vector, vector); void show_emails_with_names (vector, vector); //////////////////////////////////////// // Main function // int main () { // Hold the emails we read in vector emails; // Hold the names associated with emails vector names; char choice; bool done = false; string input; int count = 0; vector temp; do { // Print the menu for the user print_menu(); cin >> choice; switch(choice){ case 'L': case 'l': load_emails(emails, names); break; case 'S': case 's': show_emails(emails, names); break; case 'C': case 'c': cout << "What email address do you want to use: "; cin >> input; count = count_appearances(input,emails); cout << endl << "\"" << input << "\" appeared " << count << " times." << endl; break; case 'E': case 'e': cout << endl << setw(40) << "Unique Emails: " << endl; temp = unique_strings(emails); print_vector(temp); break; case 'N': case 'n': temp = unique_strings(names); cout << endl << setw(40) << "Unique Names: " << endl; print_vector(temp); break; case 'U': case 'u': show_emails_with_names(emails, names); break; case 'X': case 'x': case 'Q': case 'q': done = true; cout << "Goodbye" << endl; break; default: cout << "Bad command option." << endl; } // end switch } while (!done); return 0; } //////////////////////////////////////// // load emails // loads a file that is formatted with an email address in the first column // and then a person's name in the second and later columns. // ask the the user for the file that they want, then load that void load_emails(vector & emails, vector & names) { string input_file_name; ifstream inputFile; string inputEmail, inputName; cout << "Please enter the name of the cupcake file to use: "; cin >> input_file_name; // open file if it is there inputFile.open(input_file_name.c_str()); if (!inputFile) { cerr << "Could not open file: "<< input_file_name << " for reading." << endl; exit(1); } names.clear(); emails.clear(); // Read the lines from the file while (inputFile >> inputEmail){ getline (inputFile, inputName); emails.push_back(inputEmail); names.push_back(inputName); } } //////////////////////////////////////// // Print the menu so users know what options they have void print_menu(){ cout << endl << endl; cout << "Menu options:" << endl; cout << setw(50) << "Load a file:" << setw(3) << "L" << endl; cout << setw(50) << "Show file contents:" << setw(3) << "S" << endl; cout << setw(50) << "Count number of names for an email:" << setw(3) << "C" << endl; cout << setw(50) << "Show unique emails:" << setw(3) << "E" << endl; cout << setw(50) << "Show unique names:" << setw(3) << "N" << endl; cout << setw(50) << "Show names for unique emails:" << setw(3) << "U" << endl; cout << setw(50) << "Exit:" << setw(3) << "X" << endl << endl; cout << "Your choice: "; } //////////////////////////////////////// void show_emails(vector emails, vector names){ cout << setw(35) << "Emails" << setw(40) << "Names" << endl; for (int i=0; i < emails.size(); i++){ cout << setw(35) << emails[i] << setw(40) << names[i] << endl; } } //////////////////////////////////////// int count_appearances(string target, vector list){ int count = 0; for (int i = 0; i < list.size(); i ++){ if (list[i] == target){ count ++; } } return count; } //////////////////////////////////////// vector unique_strings (vector list){ vector retvec; string current; for (int i=0; i < list.size(); i++){ if (count_appearances(list[i], retvec) == 0){ retvec.push_back(list[i]); } } return retvec; } //////////////////////////////////////// void print_vector(vector vec){ for (int i = 0; i < vec.size(); i++){ cout << setw(40) << vec[i] << endl; } } //////////////////////////////////////// // given an email address, return a vector of names // associated with that email vector get_names(string email, vector emails, vector names){ vector retvec; for (int i = 0; i < emails.size(); i++){ if (emails[i] == email){ retvec.push_back(names[i]); } } return retvec; } //////////////////////////////////////// // void show_emails_with_names (vector emails, vector names){ vector unique_emails; vector names_for_email; unique_emails = unique_strings(emails); cout << setw(35) << "Emails" << setw(40) << "Names" << endl; for (int i = 0; i < unique_emails.size(); i++){ names_for_email = get_names(unique_emails[i], emails, names); cout << setw(35) << unique_emails[i] << setw(40) << names_for_email[0] << endl; if (names_for_email.size() > 1){ for (int j = 1; j < names_for_email.size(); j++){ cout << setw(35) << "" << setw(40) << names_for_email[j] << endl; } cout << endl; } } }