i am new to c++ and have been asked to create an encryption programme using the zig zag (rail fence) cipher. this will be saved to a file, and when it is opened the file will be decrypted.
a user will type in a message. The message is then read off in rows. For example, i am using 3 tiers and a message of 'WE ARE DISCOVERED. FLEE AT ONCE', the cipherer writes out:

W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
Then reads off:

WECRL TEERD SOEEF EAOCA IVDEN

i know that one way of doing this is to have a 2D array and toggle it's rows from 0 to 2 and then 2 to 0 one after the other until you finish the string. But since i am new at c++ i have not learnt how to dothis yet.

/*Header Files*/
#include <stdio.h>
#include <conio.h>
#include "bcio2.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*Function Prototype*/
void sleep(void);
void sleep( int nbr_seconds );
void header(void);
void filewrite(void); 
void menu(void);
int valid(char s[]);
void fileread(void);

/*Validatoion Error Message*/
char msg0[40]="\t\t\tTry Again!\a";
char msg1[60]="YOU HAVE ENTERED MORE THAN ONE CHARACTER!\a";
char msg2[40]="YOU HAVE ENTERED BETWEEN 5 AND 9!\a";
char msg3[60]="YOU HAVE FORGOTTEN TO ENTER A VALID OPTION!!\a";
 
/*Variables & Constants*/
char input[8];
int multi;
int flag=0;
char x;
int conv;
/*START OF MAIN FUNCTION*/
void main(void)
{
 
 header();
 sleep();
 menu();         
   
}  
 
/*HEADER SCREEN TO DISPLAY  GRAPHICS*/
void header(void)
{ int x;//inner loop counter// 
 int y;//outer loop counter// 
   char d = ' ';
   settextcolour(magenta, BRIGHT);
/*print top line and ends*/ 
        printf("\n\t%c",201); 
        for(x=1;x<15;x++)
   printf("%c",205);
  printf("%c",203);
  for(x=1;x<15;x++)
                printf("%c",205); 
        printf("%c\n",187); 
/*print bottom right and left sides*/  
  for(y=1;y<8;y++){ 
                printf("\t%c",186);
                for(x=1;x<15;x++) 
                        printf("%c",d); 
                printf("%c",186);
    for(x=1;x<15;x++) 
                        printf("%c",d); 
                printf("%c\n",186);
  }
/*print middle line and edges*/ 
        printf("\t%c",200); 
        for(x=1;x<15;x++) 
                printf("%c",205); 
  printf("%c",202);
  for(x=1;x<15;x++)
    printf("%c",205);
        printf("%c",206);
   for(x=1;x<30;x++) 
                printf("%c",205);
  printf("%c\n",187);
/*print bottom right and left sides*/  
  for(y=1;y<8;y++){ 
                printf("\t\t\t\t      %c",186);
                for(x=1;x<30;x++) 
                        printf("%c",d); 
                printf("%c\n",186);
  }
  
/*print bottom line and edges*/ 
        printf("\t\t\t\t      %c",200); 
        for(x=1;x<30;x++) 
                printf("%c",205); 
        printf("%c\n",188);
}
 
/*SPLASH SCREEN*/
 void sleep(void)
{
 int ctr;
 int wait =1;// assigned for three seconds
 int x;//inner loop counter// 
 int y;//outer loop counter// 
   char d = ' ';
 clrscr(); 
    for (ctr=1; ctr <= wait; ctr++){
 settextcolour(magenta, BRIGHT);
 printf("\n\n\t\t\tWELCOME TO THE CANDID ECHO PRESS\n");
 puts("\t\t\tPLEASE WAIT WHILE THE PROGRAMME LOADS\n");
 header();
 printf("\n\n\t\t\tPRODUCED BY: Richard Lasowski");
       sleep( (int) 2 );  /* pause for 2 seconds */
 }
   
}
void sleep( int nbr_seconds )
{
    clock_t goal;
    goal = ( nbr_seconds * CLOCKS_PER_SEC ) + clock();
    while( goal > clock() )
    {
       ; /* loop */
    }
}
 
 
/*TO DISPLAY MENU SCREEN*/
void menu(void)
{
 
 clrscr();
 header();
 gotoxy(17,20);
 puts("WECLOME TO MAIN MENU SCREEN!  FILE PROCCESSING:");
 gotoxy(25,30);
 settextcolour(magenta, BRIGHT);
 puts("* CREATE A FILE .......a");
 gotoxy(25,32);
 puts("* OPEN A FILE .........b");
    gotoxy(25,34);
 puts("* EXIT THE PROGRAMME...c");
 gotoxy(25,26);
 puts("CHOOSE A VALID OPTION NUMBER:");
 
 gotoxy(30,36);
 puts("CHOOSE AN OPTION:");
 gets(input);
 flag=valid(input);
 if(!flag)
 {
  gotoxy(32,46);
  flushall();
 }
 
}

/*FILE WRITNG*/
void filewrite(void)
{
 flushall();
 clrscr();
 header();
FILE *fp;
char str[165][165];
char str1[330]
char filename[10];
 
 gotoxy(24,20);
 puts("FILE PROCCESSING: FILE WRITING\n\n");
 puts("Enter up to 333 characters (no spaces):");
 scanf("%s",&str);
 
 puts("\n\nEnter a file name (8 characters max):");
 scanf("%s",&filename);
 fp=fopen(filename,"w");
 fprintf(fp,"%s",str);
  fclose(fp);
  flushall();
  header();
  menu();
}
/*FILE READING*/
void fileread(void)
{
 flushall();
 clrscr();
 header();
 char create;
 char filename[8];
 FILE *file1;
    gotoxy(24,20);
 puts("FILE PROCCESSING: FILE READING");

    gotoxy(25,25);
 puts ("THIS PROGRAM READS (OPENS) FILE");
    gotoxy(13,28);
 fputs("Enter file name: ",stdout);
 gets(filename);
 gotoxy(13,29);
 file1 = fopen(filename ,"r");
 if (file1 == NULL)
 {
  gotoxy(13,32);
  puts("FILE MISSING");
  gotoxy(23,42);
     puts("Press any key to continue");
     gotoxy(49,42);
  getch();
     flushall();
 

 }
 gotoxy(13,31);
 printf("Reading charcters from the file: %s\n\n",filename);
  gotoxy(13,33);
 while((create=fgetc(file1)) !=EOF)
 {
  //delay(100);
  putchar(create);
 }
 fclose(file1);
    gotoxy(23,42);
 puts("Press any key to continue");
 gotoxy(49,42);
 getch();
     flushall();
}

/*VALIDATE INPUT (VALID)*/
int valid(char input[])
{
 int count=0;
 int test=0;
 flag++;
 conv = atof(input);
 

/*test option 'a' file write*/
 {
  int x;
    for(x=0;x<strlen(input);x++)
     {
    if(input[x]=='a')
    filewrite();
   
 }
 }
 
/*test option 'b' file read*/
 {
  int x;
    for(x=0;x<strlen(input);x++)
     {
    if(input[x]=='b')
    fileread();
  }
 }
 
/* test option 'c' exit*/
 {
  int x;
    for(x=0;x<strlen(input);x++)
     {
    if(input[x]=='c')
    exit(0);
     
 
  }
 }

/* test for range 1 to 4*/
 if(conv<1 || conv>4)
 {
  
 flushall();
 clrscr();
 header();
 
 gotoxy(27,20);
 puts(" MENU (OPTION) VALIDATION ");
 
    gotoxy(15,22);
 puts (" PLEASE CHOOSE THE VALID OPTIONS AVAILABLE!");
 gotoxy(15,24);
  puts(msg0);
   flushall();
 }
 
 
/*test all input in range 0 to 4*/
 
if(count==1)
 {
  int x;
  for(x=0;x<strlen(input);x++)
  {
   //if((input[x]>='1'||input[x]=='a')&&(input[x]<='4'||input[x]=='a'));
   if(input[x]>='1' && input[x]<='4');
 }
}
else
{
 settextcolour(magenta, BRIGHT);
 gotoxy(15,28);
 puts(msg3);
 test++;
 flushall(); 
}
 
 

/*if input errors set flags*/
  if(test>0)
  {
 flag--;
 
     gotoxy(23,30);
     puts(" - Press any key to continue -");
     gotoxy(54,30);
  getch();
     flushall();
  header();
  menu();
  }
else
 ;
return flag;
}

if someone could help me out that would be great, cheers

Member Avatar for iamthwee

I would recommend getting rid of all the non-standard graphic libraries. You don't need it.

And then learn to properly indent your code for starters.

Also, 90% of your code has nothing to do with the problem at hand, it's just user interface fluff.

void encryptIt( const char *clear, char *cypher );
int main ( ) {
  char cleartext[] = "WE ARE DISCOVERED. FLEE AT ONCE";
  char cyphertext[100];
  encryptIt( cleartext, cyphertext );
  printf("%s\n", cyphertext );
  return 0;
}

void encryptIt( const char *clear, char *cypher ) {
  // your code here
}

It's short, simple, and focusses on the specific problem at hand.

When you've got it to work, you can simply copy/paste the working function into your main code, complete with all the colour and noise which you like.

i was set a task to encrypt to a file then decrypt that file once it was opened.

thankyou for the help, cheers

Does that mean you've solved it, or are you just restating your homework?

Post some attempt at the actual encrypt() / decrypt() function (as I outlined), then we can help you (if you're still stuck that is).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.