cin >> string space problem.

Alright. I am teaching myself how to program C++ and I am writing a program that takes input from the user and puts it into a string from which I manipulate it to put it into a file on the same line. I separate information using special character £ so I can recall that information later without any problems. I am having a problem where I take input from the user. Say I wanna put "William V." into a string. When it enters it goes to the next cin. Below is an extract of the code I am referring 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
44
45
46
47
48
49
50
int main(){
	while(true){
		fstream myFile;
		int switchInt;
		file *a = new file();
		cout << "Enter '1' to create new account, '2' to read all accounts, '3' to find an\naccount using ID, '4' to edit an account: ";
		cin >> switchInt;
		system("cls");
		switch(switchInt){
			case 1:
				cout << "Enter first name: ";
				cin >> firstName;
				cout << "Enter last name: ";
				cin >> lastName;
				system("cls");
				a->write();
				break;
			case 2:
				system("cls");
				a->read();
				break;
			case 3:
				cout << "Enter ID: ";
				cin >> IDFind;
				system("cls");
				a->find();
				break;
			case 4:
				cout << "Enter ID: ";
				cin >> IDFind;
				system("cls");
				a->edit();
				break;
			case 666:
				myFile.open("IDStorage.txt", ios::out);
				myFile << flush;
				myFile.close();
				myFile.open("NextID.txt", ios::out);
				myFile << "000000\n";
				myFile.close();
				cout << "Flushing the data out of 'NextID.txt' and 'IDStorage.txt'.\n";
				system("pause");
				system("cls");
				break;
			default:
				cout << "Number entered is out of bounds, enter new number.\n";
				system("pause");
				system("cls");
				break;
}}}


Extra information: file is a custom class that manipulates the file "IDStorage.txt" by either creating (in the case that it doesn't already exist), reading, writing to, or editing it.
firstName and lastName are strings.
Yes, 666 does clear the file. I needed something to flush the file if needed without actually opening the file outside of the program.

Edit: I didn't explain this very well. When I have a space, it doesn't put all of the input into the string. It enters everything before the space into the string, then it goes to the next cin and enters the rest of the input until the next space in that string. I do not want this to happen. How do I fix this?
Last edited on
Maybe this is just me, but when I hit enter it does two cin.getline();s It's like I hit enter twice, but i don't. I'm switching machines really fast to see if it does the same

Edit: My bad. I typed it wrong. Thanks!
However sometimes it does this: http://imgbin.org/index.php?page=image&id=8014
Last edited on
If you are mixing unformatted input ( std::getline() ) with formatted input ( operator>> ), you might want to have a look at http://cplusplus.com/forum/general/69685/#msg372532
Topic archived. No new replies allowed.