help with program

I have been working on this for some time. and there is something wrong with the the get_text function. The people who have tried to help me can't figure out what is wrong. All i now is then when i run the program and enter C or P so i can enter a sentence to encrypt or decrypt, i can enter once letter and then the program crashes.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>

using namespace std;

// Functions

char get_menu_choice ();

void get_text (char text [] , int &length);

int get_shift ();

void decrypt_text (char plain_text[], char cipher_text[], int length, int shift, bool &plain);

void encrypt_text (char plain_text[], char cipher_text[], int length, int shift, bool &cipher);

void display_text (char plain_text[], char cipher_text[], int length, bool cipher, bool plain);







int main ()
{

char choice = 'A';
int i = 0;
int length;// this is the length that user inputs
int shift=3; // sets shift to 3
bool plain=false;
bool cipher=false;
bool decrypt=false;
bool encrypt=false;
char text[50]={0};
char plain_text[50];
char cipher_text[50];




cout <<"*** Mines Caesar Cipher *** \n\n" ;
do
{
choice = get_menu_choice ();
switch (choice) // switch statement with the users "choice"
{
case 'C':

get_text(text, length);
for (i=0;i<length;i++)
cipher_text[i]=text[i];
cipher=true;
cout << endl << endl;

break;

case 'D':
if(cipher==false) //this loop makes one enter a text before decrypting
{
cout << "You have to enter a cipher text first.";
cout << endl;
}
else
decrypt_text(plain_text, cipher_text, length, shift, plain);
cout << endl;
decrypt=true;

break;
case 'E':
if(plain==false) //this loop makes one input a text
//before encrypting it
{
cout << "You have to enter a plain text first.";
cout << endl;
}
else
encrypt_text(plain_text, cipher_text, length, shift, cipher);
cout << endl;
encrypt=true;


break;
case 'P': // allows the user to input text.

cout << "Please enter your text. Finish your text"
<< " with <RETURN>" << endl <<
"------------------------------------------------------" << endl;
get_text(plain_text, length);
plain=true;
cout << endl << endl;





break;

case 'S': // case 's' goes to the get shift function

shift = get_shift();




break;
case 'T':
if(plain==true||cipher==true)
{
if(decrypt==true&&cipher==true)
{
cout << endl;
display_text(plain_text, cipher_text, length, cipher, plain);
cout << endl;
}
else if(decrypt==false&&cipher==true)
{
cout << "You must decrypt your text first." << endl << endl;
}
else if(encrypt==true&&plain==true)
{
cout << endl;
display_text(plain_text, cipher_text, length, cipher, plain);
cout << endl;
}
else
cout << "You must encrypt your text first" << endl << endl;

}
else
cout << endl << "You have to enter either a plain or sipher text first."
<< endl << endl;

break;

case 'X': // case 'x' ends the program
cout <<"Program Terminated"<< endl << endl;
break;

default :
cout <<" Please enter a valid entry C, D, E, P, S, T, or X\n\n";
}
}while(choice != 'X');
return 0;
}

char get_menu_choice () // menu displayed to user, also is the functionable window the the user navigates with.
{


char choice; // title choices

cout <<"C: Enter a cipher text \n\n" ; // menu shown to user

cout <<"D: Decrypt a text \n\n" ;
cout <<"E: Encrypt a text \n\n" ;
cout <<"P: Enter a plain text \n\n" ;
cout <<"S: Set the shift \n\n" ;
cout <<"T: Display the clear and encrypted text \n\n" ;
cout <<"X: Exit program \n\n" ;
cout <<"Your choice: " ;
cin >> choice;
cout <<"\n\n";
choice=toupper(choice); //changes users pick to uper case


return choice; // returnes the users pick

}

int get_shift () // gets the amount the text is going to tbe shifted in an encryption.
{
int x=4;

do
{
cout << "Enter your shift <-3...3>: " ;
cin>> x;
cout <<"\n\n";
}while (x<-3||x>3); // if the number input by user is greater then 3 or less then -3 it keeps the user in the loop until a number between -3 and 3 is enterd.

return x;
}


void get_text (char text[], int &length) // gets the text input by the user
{
cout << "Please enter your text. Finish your text"
<< " with <RETURN>" << endl <<
"------------------------------------------------------" << endl;
char letter;
do
{
letter = _getch(); // Taka a single character as input
cout << letter; // Echo the keyboard input
if(letter == '\n' || letter == '\r') // End of line?
return;
text[length] = tolower( letter );
length++; // Increment the length of the text
}
while(length < 50); // If less than 50 characters keep reading


}

void decrypt_text (char plain_text[], char cipher_text[], int length, int shift, bool &plain)
{
int x=0;
while(x<length)
{
if(plain_text[x]==' ')
{
cipher_text[x]=plain_text[x];
}
else if(cipher_text[x] + shift > 122)
{
plain_text[x]=cipher_text[x] - 32;

}
else if (cipher_text[x] + shift < 97)
{
plain_text[x]=cipher_text[x] + 32;

}
else
{
plain_text[x]=cipher_text[x] + shift;

}
x++;
}
}


void encrypt_text (char plain_text[], char cipher_text[], int length, int shift, bool &cipher)
{
int x=0;

while(x<length)

{
cipher_text[x]=plain_text[x] + shift ;
if(cipher_text[x]==' ')
{
plain_text[x]=cipher_text[x];
}
if(int(plain_text[x] - shift) > 122)
{
cipher_text[x]=int(plain_text[x]) + 32;

}
if(plain_text[x] - shift < 97)
{
cipher_text[x]=int(plain_text[x]) - 32;

}


x++;
}
}


void display_text (char plain_text[], char cipher_text[], int length, bool cipher, bool plain)
{
cout << "Plain text: " << plain_text << endl
<< "Cipher text: " << cipher_text << endl;
}

You need to initialise length before you use it as an index for the text array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void get_text (char text[], int &length) // gets the text input by the user
{
cout << "Please enter your text. Finish your text"
<< " with <RETURN>" << endl <<
"------------------------------------------------------" << endl;
char letter;
length = 0; //// initialise length to 0
do
{
letter = _getch();
cout << letter;
if(letter == '\n' || letter == '\r') 
return;
text[length] = tolower( letter ); //// now this won't cause crash
length++;
}
while(length < 50);


}
Last edited on
Topic archived. No new replies allowed.