if statement not working

Hi guys. I have basically written an encryption program that works perfectly except when I try to include punctuation. I narrowed down the problem to being something along the lines of this:

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
#include <iostream>
#include <string>
using namespace std;

int main() 
{
	string test;
	
	cin >> test;
	
	int i = 0;
	while (i < test.length()) 
	{
		if (test[i] == '.') 
		{
			cout << "a";
		}
		
		if (test[i] == ',')
		{
			cout << "b";
		}
		
		if (test[i] == ' ')
		{
			cout << "c";
		}
		i++;
	}
	
	return 0;
}


This is the input/output:

1
2
. , .
a


As you can see, if you enter ". , .", the output should be "acbca" but it doesn't work when there are any spaces. Any help would be much appreciated! Thanks in advance.
the input stream >> operator breaks/stops at spaces - so
test string just gets the . character.
Okay thanks a lot... I totally forgot about that >.> I am trying to replicate a problem I am having in a different program thats too long to post here (I used getline() in the other program so I still don't know what the problem is!). Anyway thanks for the help :)
Replace

cin >> test;

with

getline(cin, test);

and watch your code work~
Topic archived. No new replies allowed.