cin.getline Clearing Input

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

string response;
bool x = true;
int length;
char *message = new char[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;
			}
			else if (letter == 'z'){
				letter = ((int)letter - 25);
			}
			else if (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;
			}
			else if (letter == 'a'){
				letter = ((int)letter + 25);
			}
			else if (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;
		}
		else if (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;
		}
		else if (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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

string response;
bool x = true;
int length;
char *message = new char[300];
string message2;
char input[300];
int k = 0;
char letter;

int main(){
	cout << "Enter the message to encode." << endl;
	cin.getline(message, 300); 
	cin.ignore();              
	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;
			}
			else if (letter == 'z'){
				letter = ((int)letter - 25);
			}
			else if (letter == 'Z'){
				letter = ((int)letter - 25);
			}
			else{
				letter = ((int)letter + 1);
			}
		}
		message2.push_back(letter);
	}
	cout << message2 << endl;
	delete[] message;
}
Last edited on
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).
Last edited on
closed account (367kGNh0)
Why do you mix code associated with
using namespace std;
Alongside code which is, well, not associated with that line?
Topic archived. No new replies allowed.