Challenge 77

Description

The purpose of this challenge is to use arrays , functions and files. This challenge also uses a menu.

Requirements

    1. The goal of this program is to display words using a “pager” mechanism. Let’s assume we’re displaying the letters of the alphabet one per line, using a page size of 5.
      1. The first page of output would display A B C D E.
      2. If the user selects Next, then F G H I J would be displayed.
      3. If the user selects Next again, then K L M N O would be displayed
      4. If the user selects Previous, then F G H I J would be displayed
      5. If the user selects Next again, then K L M N O would be displayed
      6. If the user selects First, then A B C D E would be displayed
      7. If the user selects Last, then only Z would be displayed. This is because there are 26 letters in alphabet and a page size of 5 would make the last page only contain 1 letter.
    2. Start by writing the main() function as given below
      int main()
      {
          CommonFive fiveletterwords;
      
          // use the line below if you want to test with all words
          // const int WORDCOUNT = fiveletterwords.size();
      
          // use the line below if you want to test with fewer words
          // only use the line below or the one above, one or the other
          const int WORDCOUNT = 100;
      
          string words[WORDCOUNT];
          fiveletterwords.copy(words, WORDCOUNT);
      
          char input;
          int start = 0, finish = 9, pagesize = 10;
      
          do
          {
              system("clear");
              show_range(words, start, finish);
      
              // comment out the line below when you are done
              cout << "DEBUG ---- start: " << start << " finish: " << finish << endl << endl;
      
              cout << "f - Show first page" << endl;
              cout << "p - Show previous page" << endl;
              cout << "n - Show next page" << endl;
              cout << "l - Show last page" << endl;
              cout << "c - Change page size" << endl;
              cout << "s - Save current page to file" << endl;
              cout << "q - Quit" << endl;
              cout << "Command: "; 
              cin >> input;
      
              if (input == 'c')
              {   
                  // TODO: only allow pagesize between 4 and 15
                  cout << "Enter new page size: "; 
                  cin >> pagesize;
              }
      
              if (input == 's')
              {
                  save_range(words, start, finish);
                  cout << "File saved." << endl;
              }
      
              calc_range(input, start, finish, pagesize, WORDCOUNT);
      
          } while (input != 'q');
      
          return 0;
      }
      
    3. Write a function void show_range(string array[], int start, int finish). This function will display one string per line from array. start and finish represent which indexes will be displayed from the array. For example, if start is 5 and finish is 9, then only indexes 5-9 will be displayed from array. Also display the index of the array on the same line (See Sample Interaction) — this will be helpful in debugging.
    4. Write a function void save_range(string array[], int start, int finish). This function will save the array contents to a file, but only the array contents from indexes start to finish. If start = 5 and finish = 9, then only the words at indexes 5-9 will be saved to the file. The name of the file would be words5-9.txt.
    5. Write a function void calc_range(char choice, int & start, int & finish, int pagesize, int wordcount). This function is used to calculate values for start and finish — notice that these two parameters are passed by reference, their changed values will be used in main(). The other parameters choice, pagesize and wordcount are used to help calculate start and finish. Use the code below as your starting point. Use similar logic as described in Step #1, the wordcount parameter used in the function below represents how many total words are being paged (Using the example from Step 1, wordcount in that scenario would be 26).
      void calc_range(char choice, int & start, int & finish, int pagesize, int wordcount)
      {
         // the goal of this function is to set new values
         // for start and finish
          if (choice == 'c') // nothing to do here
          {
              finish = start + pagesize - 1;
          }
      
          // Choice 'f' means set start and finish 
          // to the first 'page' 
          // Nothing more to do on this, use as a guide
          if (choice == 'f')
          {
              // jump to the first page
              start = 0;
              finish = start + pagesize - 1;
          }
      
          // go to previous page
          if (choice == 'p')
          {
              // make sure using previous does not go beyond the first page
          }
      
          // go to next page
          if (choice == 'n')
          {
              // make sure using next does not go beyond the last page
          }
      
          // go to last page
          if (choice == 'l')
          {
              // jump to the last page 
          }
      }
      
    6. In the section of code in main that asks for pagesize, modify it so that it only allows pagesize to be between 4 and 15.
    7. When testing, make sure to try various combinations of navigation, choosing random selections of first, next, previous, last. Also, test different page sizes, especially odd numbered page sizes – this ensures that the last page contains items fewer than the actual page size.
    8. Read the comments in main(). You can test with a few words or a lot of words. Use the correct line of code that makes that possible. A
    9. Also, the DEBUG line should be commented out when you are done testing. It’s there to help see the values of start and finish.

Sample Interaction

0: aback
1: abaft
2: abase
3: abate
4: abbey
5: abbot
6: abhor
7: abide
8: abler
9: abode

DEBUG ---- start: 0 finish: 9

f - Show first page
p - Show previous page
n - Show next page
l - Show last page
c - Change page size
s - Save current page to file
q - Quit
Command: n
10: about
11: above
12: abuse
13: abyss
14: ached
15: aches
16: acids
17: acorn
18: acres
19: acrid

DEBUG ---- start: 10 finish: 19

f - Show first page
p - Show previous page
n - Show next page
l - Show last page
c - Change page size
s - Save current page to file
q - Quit
Command: c
Enter new page size: 7
10: about
11: above
12: abuse
13: abyss
14: ached
15: aches
16: acids

DEBUG ---- start: 10 finish: 16

f - Show first page
p - Show previous page
n - Show next page
l - Show last page
c - Change page size
s - Save current page to file
q - Quit
Command:

LEGEND

PROGRAM OUTPUT
USER INPUT
FROM INPUT
META/HELPER TEXT

CATALOG ID: CPP-CHAL00077