C++ example with input arguments using flags


/ Published in: C++
Save to your folder(s)

asdfas


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. class Contact {
  9. public:
  10. Contact() :
  11. m_firstname(""),
  12. m_lastname(""),
  13. m_email(""),
  14. m_phone(""),
  15. m_debug_mode(false),
  16. m_cnt(0)
  17. {};
  18. ~Contact()
  19. {};
  20.  
  21. void displayHelp() {
  22. cout << " Welcome to new_user. USAGE:" << endl;
  23. cout << "-d, --debug Enable debug mode" << endl;
  24. cout << "-e, --email <email-address> Email of the user" << endl;
  25. cout << "-h, --help Print this help" << endl;
  26. cout << "-n, --name <first-name> First name of the user" << endl;
  27. cout << "-p, --phone <phone-number> Phone number of the user" << endl;
  28. cout << "--show Show addresslist" << endl;
  29. cout << "-s, --surname <last-name> Last name of the user" << endl;
  30. };
  31.  
  32. bool welcomeScreen(int argc, char* argv[]) {
  33. int step = 2;
  34. if (argc == 1) return false;
  35. else {
  36. for (int i = 1; i < argc; i = i + step) {
  37. if (!strcmp(argv[i],"-n") || !strcmp(argv[i],"--name")){
  38. m_firstname = argv[i+1];
  39. step = 2;
  40.  
  41. } else if (!strcmp(argv[i],"-s") || !strcmp(argv[i],"--surname")){
  42. m_lastname = argv[i+1];
  43. step = 2;
  44.  
  45. } else if (!strcmp(argv[i],"-e") || !strcmp(argv[i],"--email")) {
  46. m_email = argv[i+1];
  47. step = 2;
  48.  
  49. } else if (!strcmp(argv[i],"-p") || !strcmp(argv[i],"--phone")) {
  50. m_phone = argv[i+1];
  51. step = 2;
  52.  
  53. } else if (!strcmp(argv[i],"-d") || !strcmp(argv[i],"--debug")) {
  54. m_debug_mode = true;
  55. step = 1;
  56. } else if (!strcmp(argv[i],"--show")) {
  57. readAddressList();
  58.  
  59. } else if (!strcmp(argv[i],"-h") || !strcmp(argv[i],"--help")) {
  60. displayHelp();
  61. step = 1;
  62.  
  63. } else {
  64. cout << "[UNKNOWN OPTION " << argv[i] << "]" << endl;
  65. displayHelp();
  66. step = 1;
  67.  
  68. }
  69. } //end for-loop
  70. if (m_firstname != "" || m_lastname != "" || m_email != "" || m_phone != "") m_cnt++;
  71. } //end if-else-block
  72. return true;
  73. };
  74.  
  75. int printWelcomeHeader() {
  76. int option;
  77. cout << "********************************************" << endl;
  78. cout << "*********WELCOME TO THE ADDRESSLIST*********" << endl;
  79. cout << "********************************************" << endl;
  80. if (m_debug_mode == true) cout << "[DEBUG] mode is on" << endl;
  81. if (m_cnt != 0) cout << m_cnt << " contact(s) ready to be saved. Select option 2" << endl;
  82. cout << "----------OPTIONS----------" << endl;
  83. cout << ". 1. Show contacts ." << endl;
  84. cout << ". 2. Save contact to file ." << endl;
  85. cout << ". 3. Exit ." << endl;
  86. cout << "---------------------------" << endl;
  87. cout << "Option: "; cin >> option;
  88. cout << "---------" << endl;
  89. return option;
  90. };
  91.  
  92. void readAddressList() {
  93. string line;
  94. ifstream addresslist_in("addresslist.txt");
  95. if (m_debug_mode) cout << "[DEBUG]Reading from file..." << endl;
  96. if (addresslist_in.is_open()) {
  97. cout << " @@@@@@@@@@@@@@@@@@@@@[addresslist.txt]@@@@@@@@@@@@@@@@@@@@@" << endl;
  98. while ( getline (addresslist_in, line) ) {
  99. cout << " " << line << "\n";
  100. }
  101. addresslist_in.close();
  102. cout << " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << endl;
  103. if (m_debug_mode) {
  104. cout << "[DEBUG]File was read successfully..." << endl;
  105. }
  106. } else cout << "[ERROR]Unable to open file." << endl;
  107. };
  108.  
  109. void writeToAddressList() {
  110. ofstream addresslist_out;
  111. if (m_debug_mode) cout << "[DEBUG]Saving contact to addresslist.txt ..." << endl;
  112. if (m_firstname == "" && m_lastname == "" && m_email == "" && m_phone == "") {
  113. cout << "Cannot creaty empty contact." << endl;
  114. return;
  115. } else {
  116. addresslist_out.open("addresslist.txt", ios::out | ios::app);
  117. addresslist_out << "----------------------------------------------\n";
  118. addresslist_out << "Name: " + m_firstname + " " + m_lastname + "\n";
  119. addresslist_out << "Email: " + m_email + "\n";
  120. addresslist_out << "Phone number: " + m_phone + "\n";
  121. addresslist_out.close();
  122. if (m_debug_mode) {
  123. cout << "[DEBUG]Contact successfully saved!!" << endl;
  124. if (m_cnt > 0) m_cnt--;
  125. }
  126. }
  127. };
  128.  
  129. public:
  130. string m_firstname;
  131. string m_lastname;
  132. string m_email;
  133. string m_phone;
  134. bool m_debug_mode;
  135. int m_cnt;
  136. };
  137.  
  138.  
  139. int main(int argc, char* argv[]) {
  140.  
  141. Contact* contact = new Contact();
  142. int opt;
  143. string name, surname, email, phone,line;
  144. bool cont = true;
  145.  
  146. if (!contact->welcomeScreen(argc,argv)) {
  147. cout << "(You can add the -h or --help flag to display help)" << endl;
  148. cout << "Exiting application.." << endl;
  149. return 0;
  150. } else { //input arguments
  151. if (argc > 2) cout << "New contact already created from command line!" << endl;
  152. }
  153.  
  154. while (cont ==true) {
  155. opt = contact->printWelcomeHeader();
  156. switch (opt) {
  157. case 1:
  158. {
  159. contact->readAddressList();
  160. }
  161. break;
  162. case 2:
  163. {
  164. contact->writeToAddressList();
  165. break;
  166. }
  167. case 3:
  168. {
  169. cont = false;
  170. cout << "Exiting application..." << endl;
  171. break;
  172. }
  173. default:
  174. cout << "Wrong option entered." << endl;
  175. break;
  176. } //end of switch
  177.  
  178. } //end of while
  179.  
  180. return 0;
  181. }

URL: asdfas

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.