Return to Snippet

Revision: 70448
at February 19, 2016 01:10 by random_programmer


Initial Code
#include <iostream>
#include <fstream>
#include <string.h>
#include <string>

using namespace std;

class Contact {
public:
  Contact() :
    m_firstname(""), 
    m_lastname(""), 
    m_email(""), 
    m_phone(""), 
    m_debug_mode(false),
    m_cnt(0)
  {};
  ~Contact()
  {};

  void displayHelp() {
    cout << "   Welcome to new_user. USAGE:" << endl;
    cout << "-d, --debug Enable debug mode" << endl;
    cout << "-e, --email <email-address> Email of the user" << endl;
    cout << "-h, --help Print this help" << endl;
    cout << "-n, --name <first-name> First name of the user" << endl;
    cout << "-p, --phone <phone-number> Phone number of the user" << endl;
    cout << "--show Show addresslist" << endl;
    cout << "-s, --surname <last-name> Last name of the user" << endl;
  };

  bool welcomeScreen(int argc, char* argv[]) {
    int step = 2;
    if (argc == 1) return false;
    else {
      for (int i = 1; i < argc; i = i + step) {
	if (!strcmp(argv[i],"-n") || !strcmp(argv[i],"--name")){
	  m_firstname = argv[i+1];
	  step = 2;
	  
	} else if (!strcmp(argv[i],"-s") || !strcmp(argv[i],"--surname")){
	  m_lastname = argv[i+1];
	  step = 2;
	  
	} else if (!strcmp(argv[i],"-e") || !strcmp(argv[i],"--email")) {
	  m_email = argv[i+1];
	  step = 2;
	 
	} else if (!strcmp(argv[i],"-p") || !strcmp(argv[i],"--phone")) {
	  m_phone = argv[i+1];
	  step = 2;
	  
	} else if (!strcmp(argv[i],"-d") || !strcmp(argv[i],"--debug")) {
	  m_debug_mode = true;
	  step = 1;
	} else if (!strcmp(argv[i],"--show")) {
	  readAddressList();
	 
	} else if (!strcmp(argv[i],"-h") || !strcmp(argv[i],"--help")) {
	  displayHelp();
	  step = 1;
	  
	} else {
	  cout << "[UNKNOWN OPTION " << argv[i] << "]" << endl;
	  displayHelp();
	  step = 1;
	  
	}
      } //end for-loop
      if (m_firstname != "" || m_lastname != "" || m_email != "" || m_phone != "") m_cnt++;
    } //end if-else-block
    return true;
  };
  
  int printWelcomeHeader() {
    int option;
    cout << "********************************************" << endl;
    cout << "*********WELCOME TO THE ADDRESSLIST*********" << endl;
    cout << "********************************************" << endl;
    if (m_debug_mode == true) cout << "[DEBUG] mode is on" << endl;
    if (m_cnt != 0) cout <<  m_cnt << " contact(s) ready to be saved. Select option 2" << endl;
    cout << "----------OPTIONS----------" << endl;
    cout << ".  1. Show contacts        ." << endl;
    cout << ".  2. Save contact to file ." << endl;
    cout << ".  3. Exit                 ." << endl;
    cout << "---------------------------" << endl;
    cout << "Option: "; cin >> option;
    cout << "---------" << endl;
    return option;
  };

  void readAddressList() {
    string line;
    ifstream addresslist_in("addresslist.txt");
    if (m_debug_mode) cout << "[DEBUG]Reading from file..." << endl;
    if (addresslist_in.is_open()) {
      cout << "                             @@@@@@@@@@@@@@@@@@@@@[addresslist.txt]@@@@@@@@@@@@@@@@@@@@@" << endl;
      while ( getline (addresslist_in, line) ) {
	cout << "                             " << line << "\n";
      }
      addresslist_in.close();
      cout << "                             @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
      if (m_debug_mode) {
	cout << "[DEBUG]File was read successfully..." << endl;
      }
    } else cout << "[ERROR]Unable to open file." << endl;
  };

  void writeToAddressList() {
    ofstream addresslist_out;
    if (m_debug_mode) cout << "[DEBUG]Saving contact to addresslist.txt ..." << endl;
    if (m_firstname == "" && m_lastname == "" && m_email == "" && m_phone == "") {
      cout << "Cannot creaty empty contact." << endl;
      return;
    } else {
      addresslist_out.open("addresslist.txt", ios::out | ios::app);
      addresslist_out << "----------------------------------------------\n";
      addresslist_out << "Name: " + m_firstname + " " + m_lastname + "\n";
      addresslist_out << "Email: " + m_email + "\n";
      addresslist_out << "Phone number: " + m_phone + "\n";
      addresslist_out.close();
      if (m_debug_mode) {
	cout << "[DEBUG]Contact successfully saved!!" << endl;
	if (m_cnt > 0) m_cnt--;
      }
    }
  };

public:
  string m_firstname;
  string m_lastname;
  string m_email;
  string m_phone;
  bool m_debug_mode;
  int m_cnt;
};


int main(int argc, char* argv[]) {

  Contact* contact = new Contact();
  int opt;
  string name, surname, email, phone,line;
  bool cont = true;
  
  if (!contact->welcomeScreen(argc,argv)) {
    cout << "(You can add the -h or --help flag to display help)" << endl;
    cout << "Exiting application.." << endl;
    return 0;
  } else { //input arguments
    if (argc > 2) cout << "New contact already created from command line!" << endl;
  }

  while (cont ==true) {
    opt = contact->printWelcomeHeader();
    switch (opt) {
    case 1:
      {	
	contact->readAddressList();
      }
      break;
    case 2:
      {
	contact->writeToAddressList();
	break;
      }
    case 3:
      {
	cont = false;
	cout << "Exiting application..." << endl;
	break;
      }
    default:
      cout << "Wrong option entered." << endl;
      break;
    } //end of switch
    
  } //end of while
  
  return 0;
}

Initial URL
asdfas

Initial Description
asdfas

Initial Title
C++ example with input arguments using flags

Initial Tags

                                

Initial Language
C++