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.
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':
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 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);
}