All pastes #882315 Raw Edit

WORST PROGRAM EVER

public text v1 · immutable
#882315 ·published 2008-01-30 02:10 UTC
rendered paste body
//Brad Waters
//Program 1 CS351
//Jan 29, 2008
#include <iostream>
#include <math.h>

using namespace std;

char storedCodes[1201]; //global variable
int counter=0;

int menu();
void option1();
void option2();
void option3();




int main()
{

 
  int option=0; 
  while(option!=4)
    {
      option=menu();
      if(option==1)
	option1();
      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;
       	}
    }
  


 return 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;
    return option;
}
void option1() //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';
  char code[12]="";
  cout << "Please enter your 12 digit code: \n";
  cin >> code;
  if(code[11]==NULL || code[12]!=NULL) //checks that code is length 12
    {
      cout << "Your code was not 12 characters. ";
      option1();
    }
  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)
	{
	  cout << "Your code did not contain valid characters (A, L, P, R, T, V, W, X, 0-9). ";
	  option1();
	}
      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. ";
      option1();
    }
  if(flag5!='Y')
    {
      cout << "Your code must contain the digit 5 at least once. ";
      option1();
    }
  if(flag89!='Y')
    {
      cout << "Your code must contain either the digit 8 or 9. ";
      option1();
    }
  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. ";
      option1();
    }
  for(int i=0;i<12;i++)
    {
      storedCodes[(counter*12)+i]=code[i];
    }
    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--;
  
}