All pastes #885892 Raw Edit

Something

public text v1 · immutable
#885892 ·published 2008-01-31 06:10 UTC
rendered paste body
#include <iostream>

using namespace std;



class CodeStoreMenu
{
public:
  int counter;
  string *storedCodes[100];
  //displays the menu and reads in user option
  CodeStoreMenu()
  {
    counter=0;
  }
  int menu() 
  {
    int option=0;

    cout << "1) Enter a new code \n";
    cout << "2) Print entire list of codes\n";
    cout << "3) Display last code and delete it from the list.\n";
    cout << "4) Exit.\n";
    cout << "Enter a choice(1-4): ";
    cin >> option; 
    while(option!=4)
      {
	if(option==1)
	  readAndValidate();
	else if(option==2)
	  option2();
	else if(option==3)
	  option3();
	else if(option==4)
	  break;
	else
	  {
	    cout << "Your entry was invalid. Please enter your option.\n";
	    option=0;
	  }
      }
  }
  void readAndValidate() //reads in codes and checks them for problems
  {
    char flag4='N', flag5='N', flag89='N', seg1digit='N', seg1char='N', seg2digit='N', seg2char='N', seg3digit='N', seg3char='N', validlength='N', validchars='N';
    char code[1024]="";
    cout << "Please enter your 12 digit code: \n";
    cin >> code;
    if(strlen(code)==12) //checks that code is length 12
      {
	validlength='Y';
      }
    for(int i=0; i<12; i++) //i apologize for the sloppy code, but couldn't think
                            //of a better way to check valid characters
      {
	if (code[i]=='A' || code[i]=='L' || code[i]=='P' || code[i]=='R' || code[i]!= 'T' || code[i]=='V' || code[i]=='W' || code[i]=='X' || isdigit(code[i])==1)
	  {
	    validchars='Y';
	  }
	if (code[i]=='4') //sets flag if digit 4 is found
	  flag4='Y';
	if (code[i]=='5') //sets flag if digit 5 is found
	  flag5='Y';
	if (code[i]=='8' || code[i]=='9') //sets flag if digit 8 or 9 is found
	  flag89='Y';
      }
    for(int i=0; i<4; i++)
      {
	if (isdigit(code[i])==1) //checks that segment 1 has a digit
	  seg1digit='Y';
	if (isdigit(code[i])!=1) //checks that segment 1 has a char
	  seg1char='Y';          //already would have thrown out invalid char
      }
    for(int i=4; i<8; i++)
      {
	if (isdigit(code[i])==1) //checks that segment 2 has a digit
	  seg2digit='Y';
	if (isdigit(code[i])!=1) //checks that segment 2 has a char
	  seg2char='Y';          
      }
    for(int i=8; i<12; i++)
      {
	if (isdigit(code[i])==1) //checks that segment 3 has a digit
	  seg3digit='Y';
	if (isdigit(code[i])!=1) //checks that segment 3 has a char
	  seg3char='Y';         
      }
    if(flag4!='Y')
      {
	cout << "Your code must contain the digit 4 at least once.\n";
      }
    if(flag5!='Y')
      {
	cout << "Your code must contain the digit 5 at least once.\n";
      }
    if(flag89!='Y')
      {
	cout << "Your code must contain either the digit 8 or 9.\n";
      }
    if(seg1digit!='Y' || seg1char!='Y' || seg2digit!='Y' || seg2char!='Y' || seg3digit!='Y' || seg3char!='Y')
      {
	cout << "Each 4 character segment must contain at least one letter and one digit.\n";
      }
    if(validlength!='Y')
      cout << "The code you entered is not 12 characters long.\n";
    if(validchars!='Y')
      cout << "The code you entered does not contain all valid characters (A, L, P, R, T, V, W, X, 0-9.)\n";
    if(seg1digit=='Y' && seg1char=='Y' && seg2digit=='Y' && seg2char=='Y' && seg3digit=='Y' && seg3char=='Y' && validchars=='Y' && validlength=='Y' && flag4=='Y' && flag5=='Y' && flag89=='Y')
      {
	storedCodes[counter]=new string(code);
	counter++;
      }
  }
  void option2() //prints array of valid codes
  {
    for(int i=0; i<(counter*12);i++)
      {
	cout << storedCodes[i];
	if((i+1)%12==0 && i!=0) 
	  cout << "\n"; //does linebreak every 12 chars
      }
    
  }

  void option3()
  {
    for(int i=((counter-1)*12); i<((counter)*12);i++)
      {
	cout << storedCodes[i];
	if((i+1)%12==0&&i!=0)
	  cout << "\n";
      }
    counter--;
    
  }
};

int main()
{
  CodeStoreMenu *myInstance=new CodeStoreMenu();
  myInstance->menu();
  delete myInstance;
  return 0;
}