getline not working

Feb 15, 2013 at 1:40pm
hello i have some simple code but it is just skipping my getline command so i can't input anything. could someone tell me what i am doing wrong ?
1
2
3
string input;
cout<<"encrypt which characters?"<<endl;
getline(cin,input);

tell me if you need more details on anything.
Last edited on Feb 15, 2013 at 1:41pm
Feb 15, 2013 at 1:56pm
I had the same problem before. Show more code in case I am sure. Perhaps I will help.
Feb 15, 2013 at 2:00pm
the problem is near the end of the code. edit there are still more functions outside the main function but i think they are not relevant.

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
int main()
{
	int inputM[8];
	int outM[8];

	filltestchar();

	for (int i=7;i>=0;i--) {
		for (int j=7;j>=0;j--) {
			copytestcharsplit[i][j]=testcharsplit[i][j];
		}
	}

	int ans;
	cout<<"encrypt or decrypt"<<endl;
	cin>>ans;
	if (ans>0) {
		//decrypt
		string out;
		int temp[8];
		cout<<"decrypt what?"<<endl;
		cin>>out;
		out="1100111011001110";//was just for debug..
		for (int i=0;i<out.size();i=i+8) {
			for (int j=0;j<8;j++) {//splits the input into pieces of 8 integers.
				temp[7-j]=out.at(i+j)-48;
				cout<<temp[7-j];
			}
			cout<<endl;
			cout<<invbinary(temp)<<endl;
			binary(invbinary(temp),outM);
			

			//binary(183,outM);//11001110,11001110,11001110 
		
			for (int i=7;i>=0;i--) {
				testcharsplit[i][8]=outM[i];
			}

			for (int i=7;i>=0;i--) {
				for (int j=7;j>=0;j--) {
					testcharsplit[i][j]=copytestcharsplit[i][j];
				}
			}

			//outputM(testcharsplit);
			solveM(testcharsplit);
			for (int i=0;i<8;i++) {
				inputM[i]=int(testcharsplit[i][8]+8192)%2;
			}

			char x=invbinary(inputM);
			cout<<x<<endl;
		}

	} else {
		//encrypt
		string input;
		cout<<"encrypt which characters?"<<endl;
		getline(cin,input);
		cout<<endl;
		for (int i=0;i<input.size();i++) {
			binary(input.at(i),inputM);

			productM(testcharsplit,inputM,outM);
			
			output(outM);

			//cout<<endl<<invbinary(outM)<<endl;
		}
	}
	//}
	system("pause");
	return 0;
}
Last edited on Feb 15, 2013 at 2:04pm
Feb 15, 2013 at 2:08pm
Perhaps I'm not right, but as I understood it is just skipping the "encrypt which characters?" and going further.
You should try:
1
2
3
4
string input;
                cin.get()    //that one should help
		cout<<"encrypt which characters?"<<endl;
		getline(cin,input);
Feb 15, 2013 at 2:14pm
yes it works now. but could you please explain why the cin.get() is needed or what it is doing exactly? so i can learn from my mistakes too
Feb 15, 2013 at 2:22pm
My friend advised that function to me and he expleined that this function in this situation CLEANS BUFFER. It has more possibilities, you should just googled it.
Feb 15, 2013 at 2:47pm
It is the previous input, such as cin>>out; which caused the problem, when followed by getline.

The first input leaves the newline character '\n' in the input buffer. When the getline() command is executed, it reads the newline and considers that to mark the end of the line, so the result is an empty string.

The solution is to clear the buffer, most commonly by using ignore(), such as
cin.ignore();
or
cin.ignore(1000,'\n);
or maybe
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

reference:
http://www.cplusplus.com/reference/istream/istream/ignore/
Last edited on Feb 15, 2013 at 2:47pm
Topic archived. No new replies allowed.