So I've been assigned for a c++ class to put together a program that will encode or decode a caeser cipher in key 13. It's required to have four different functions including main and to dynamically allocate the message entered. I managed to create a program that does this exactly, until exactly one change is made. When I replace cin >> message; with cin.getline(message, 300); the program begins to only output blank responses when run, specifically messages that read "Your message is now ." It seems to be clearing the message entered for some reason. When I copy and paste the chunk of code that takes input and changes the message and put it all in one function, it works just fine even with getline being used, but unfortunately the assignment rules restrict me from turning it in this way. Is this by chance just a bug with getline specifically, or is there a glaring error in the code that the compiler and I are both missing? Any help would be greatly appreciated, since I can't seem to find anywhere online where others had this same issue.
The code is pasted below, the chunk that encodes the message begins on line 18 and the line that takes the message input to be encoded is on line 95.
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <cstring>
usingnamespace std;
string response;
bool x = true;
int length;
char *message = newchar[300];
string message2;
char input[300];
int k = 0;
char letter;
int encode(){
length = strlen(message);
for (int i = 0;i < length;i++){
letter = message[i];
for (int k = 0;k < 13;k++){
if ((int)letter == 32){
break;
}
elseif (letter == 'z'){
letter = ((int)letter - 25);
}
elseif (letter == 'Z'){
letter = ((int)letter - 25);
}
else{
letter = ((int)letter + 1);
}
}
message2.push_back(letter);
}
strcpy(message, message2.c_str());
}
int decode(){
length = (strlen(message));
for (int i = 0;i < length;i++){
letter = message[i];
for (int k = 0;k < 13; k++){
if ((int)letter == 32){
break;
}
elseif (letter == 'a'){
letter = ((int)letter + 25);
}
elseif (letter == 'A'){
letter = ((int)letter + 25);
}
else{
letter = ((int)letter - 1);
}
}
message2.push_back(letter);
}
strcpy(message, message2.c_str());
}
int reply(){
while (x == true){
cout << "Would you like to encode or decode a message? Encode/Decode/No" << endl;
cin >> response;
if (response == "Encode" || response == "encode"){
x = false;
break;
}
elseif (response == "Decode" || response == "decode"){
cout << "Enter the message to decode." << endl;
cin.getline(message, 300);
cin.ignore();
decode();
cout << "Your message is " << message << "." << endl;
delete[] message;
exit(0);
x = false;
}
elseif (response == "No" || response == "no"){
x = false;
exit(0);
}
else{
cout << "Invalid response. Please enter Encode, Decode, or No." << endl;
}
}
}
int main(){
reply();
cout << "Enter the message to encode." << endl;
cin.getline(message, 300);
cin.ignore();
encode();
cout << "Your message is now " << message << "." << endl;
delete[] message;
}
This is also the copy/pasted chunk that correctly takes a message input and encodes it despite there being no significant changes. When run with the input "Hello world" it will display the output "Uryyb jbeyq" as it's supposed to.
In the code that doesn't work you have the cin.ignore() in the wrong place. It needs to go before the getline since it's eating the newline that reply() left (reply() should probably handle cleaning up after itself, actually).