Guest User

Menu with Placeholders

a guest
Nov 4th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <iostream.h>
  2. //#include <fstream.h>
  3. #include <string.h>
  4. #include<cstdlib.h>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void displayMenu ()
  10. {
  11. cout << "What would you like to do?" << endl;
  12. cout << "New     -Creates a new data file" << endl;
  13. cout << "Open    -Opens a data file" << endl;
  14. cout << "Report  -Read a data file" << endl;
  15. cout << "Quit    -Exits the program" << endl;
  16. }//end menu
  17.  
  18. void trimTrailingSpace (char cmd1[])
  19. {
  20.     int n;
  21.     bool foundNonSpace;
  22.    
  23.     n = strlen(cmd1)-1;
  24.     foundNonSpace = false;
  25.     while (!foundNonSpace && n >= 0)
  26.     {
  27.         if (cmd1[n] != ' ')
  28.             foundNonSpace=true;
  29.         else
  30.             n = n --;
  31.     }//end while
  32.     cmd1[n + 1] = '\0';
  33. }//end trailing
  34.  
  35. void trimLeadingSpace (char cmd1[], char cmd2[])
  36. {
  37.     int i= 0;
  38.     int b=0;
  39.     while (cmd1[i]==' ')
  40.     {
  41.         //cout<<"error";
  42.         i++;//Eat leading white space
  43.     }//end while
  44.    
  45.     while (cmd1[i] != '\0')
  46.     {
  47.    
  48.     cmd2[b] = cmd1[i]; //Copy character over
  49.     b++;
  50.     i++;
  51.     }//end while
  52.     //cout<<"errorspace3";
  53. }//end leading
  54.  
  55. void doNew ()
  56. {
  57.     int a;
  58.     int b;
  59.     cout << "Enter Integer." << endl;
  60.     cin >> a;
  61.    
  62.     for (b = 1; b <= a; b++)
  63.     {
  64.         cout << b << endl;
  65.     }//end for
  66. }//end new
  67.  
  68. void doOpen ()
  69. {
  70.     cout << "You have selected Open." << endl;
  71. }//end open
  72.  
  73. void doReport ()
  74. {
  75.     cout << "You have selected Report." << endl;
  76. }//end report
  77.  
  78. void doQuit ()
  79. {
  80.     cout << "You have closed the program." << endl;
  81. }//end quit
  82.  
  83. //--------------------------------------------------------
  84.  
  85. int main ()
  86. {
  87. char cmd1[65];
  88. char cmd2[65];
  89.  
  90. char newCommand[] = "new";
  91. char openCommand[] = "open";
  92. char reportCommand[] = "report";
  93. char quitCommand[] = "quit";
  94.  
  95.  
  96. displayMenu ();
  97. //cout<<"error1";
  98. cin.getline (cmd1, 65);
  99. //cout<<"error2";
  100. trimLeadingSpace (cmd1, cmd2);
  101.  
  102. trimTrailingSpace (cmd2);
  103. //cout<<"error3";
  104. //trimLeadingSpace (cmd1, cmd2);
  105.  
  106.  
  107. while (stricmp(cmd2, quitCommand)!=0)
  108. {
  109. //cout << "Error While!" << endl; //error check
  110. if (stricmp(strstr(newCommand, cmd2), newCommand) == 0)
  111.     {
  112.     doNew ();
  113.     //return 1;
  114.     }
  115. else if (stricmp(strstr(openCommand, cmd2), openCommand) == 0)
  116.     {
  117.     cout << "Error: Open" << endl;
  118.     doOpen ();
  119.     //return 1;
  120.     }
  121. else if (stricmp(strstr(reportCommand, cmd2), reportCommand) == 0)
  122.     {
  123.     doReport ();
  124.     //return 1;
  125.     }
  126. else
  127.     {
  128.     cout << "You have entered an invalid choice. " << endl;
  129.     }
  130. strcpy(cmd1, "");
  131. displayMenu ();
  132. cin.getline (cmd1, 65);
  133. trimLeadingSpace (cmd1, cmd2);
  134. trimTrailingSpace (cmd2);
  135. }//end while
  136.  
  137. while (stricmp(strstr(quitCommand, cmd2), quitCommand) == 0)
  138. {
  139.     doQuit();
  140.     return 0;
  141. }//end while
  142. }//end main
  143. //--------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment