Problems with cin and spaces

I'm having trouble with inputting lines. cin only gets me everything before the space. I've seen a few topics about this, and found getline(cin,var), and cin.ignore(), but nothing I've found works.
Here's my entire code (just in case something there was the real problem); the problem is in the main at the bottom:

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
/*
This program encodes and decodes a string,
using the substitutions given in the file code.dat
(however, the cin function will ignore space characters).
*/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class CodeGenerator {
	char substitutions[96]; // array to be used as a reference when encoding and decoding
	public:
	CodeGenerator(string line) { // constructor
		char temp; // holds an individual character; line is read char by char
		for(int i = 0; i < line.length(); i++) {
			temp = line[i];
			substitutions[i] = temp; // reads in and stores the substitutions
		}
	}
	string encode(string in) {
		string out = string(); // output, empty string
		int temp;
		for(int i = 0; i < in.length(); i++) {
			int temp = in[i]; //uses ASCII code of char as index for replacement in the replacement array
			out += substitutions[temp - 32]; // encryption method is based on -32 system on the replacements
		}
		return out;
	}
	int search (char temp) { // finds index of given character
		int index = 0;
		while (substitutions[index] != temp) // loops until char is found
			index++;
		return index;
	}
	string decode(string in) {
		string out = string(); // output string; initialized as empty
		char temp;
		for(int i = 0; i < in.length(); i++) {
			temp = in[i];
			out += (search(temp) + 32); // searches for index, and uses that to find ASCII value of original character
		}
		return out;
	}
} ;

int main() {
	string line, in, out;
	ifstream file ("./code.dat");
	if (file.is_open()) { // error handling
		getline(file,line);
		file.close();
	}
	else {
		cout << "Could not open file\n";
		return 0;
	}
	CodeGenerator codes = CodeGenerator(line);
	cout << "Enter the message to be encoded.\n";
	//cin >> in; this didn't work either
	getline(cin, in);
	out = codes.encode(in);
	cout << "Here is the encoded message\n" << out << endl;
	
	cout << "Here is the decoded message\n" << codes.decode(out) << endl;
	
	return 0;
}

*code.dat just has 96 characters, ASCII 128-32, in a random order.

When I input anything with a space, it messes up, i.e.
"word1 word2 word3"
will be interpreted as
"word1"
The problem, I think is in
getline(cin,in)
I'm using g++ and mingw compilers.
Help?
Last edited on
It should work:
http://ideone.com/Yy5ZMv
^that code worked, but mine... doesn't, even though they're basically identical. Does it have anything to do with my getting input from a file before getting that line?
closed account (j3Rz8vqX)
What is line 64 doing?

---Go and fetch one string(delimited by default stuff) from the file.

------POOF: we get your first string segment before a space =D

Printing in, will prove that you've acquired the predicted string from the getline prompt.

Example:cout << "Here is the non encoded message\n" << in << endl;

Have fun.
It worked!
Not sure what did the trick, but it works now.
Just to be clear,
cin >> in;
will still only read what comes before the space, while
getline(cin, in);
will read all of it?

(Line 64 was to prove that the encryption worked)

Thank you for the help, everyone.
Topic archived. No new replies allowed.