Entering a (space) from user input skips the next part of code?

I can not figure out why in the beginning of my program if I type in a space it skips the next part of the program. For example my program asks the user to enter his or her name, if I were to enter John (space) Doe it will skip the next part of the code asking for the users address. If I type just John it is fine and moves on to the next part of code asking for the users address. Then here as well if I were to type 123 (space) Drive (space) DR the program skips the next part of code. If anyone can help me figure this out it would be greatly appreciated. Thank you in advance for any help. My code is below.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 // Use of functions and classes
#include <iostream>
#include <string>
using namespace std;
//funtion headers
int total(int, int, int);
void displayProducts();
// Customer class
class Customer
{
	string name;
	string address;
public:
	void setName(string);
	string getName();
	void setAddress(string);
	string getAddress();
};
// Function definitions of customer class
void Customer::setName(string name1)
{
	name = name1;
}
void Customer::setAddress(string address1)
{
	address = address1;
}
string Customer::getName() 
{ 
	return name; 
}
string Customer::getAddress()
{
	return address;
}

//displays the menu
void displayProducts()
{
	int a = 0, b = 0, c = 0;
	char selection = 'a';
	do
	{

		cout << "Please select a product from the list below. Each item cost $1" << endl;
		//display the menu
		cout << "Menu";
		cout << "======" << endl;
		cout << "1 - Apples" << endl;
		cout << "2 - Bananas" << endl;
		cout << "3 - Oranges" << endl;
		cout << "E - Press E to proceed to order summary." << endl << endl;
		cout << "Enter selection: ";
		// read user selection
		cin >> selection;


		switch (selection)
		{
		case '1':
			cout << "You selected - Apples" << endl;
			cout << "Please enter quantity. ";
			cin >> a;
			break;

		case '2':
			cout << "You selected - Bananas" << endl;
			cout << "Please enter quantity. ";
			cin >> b;
			break;

		case '3':
			cout << "You selected - Oranges" << endl;
			cout << "Please enter quantity. ";
			cin >> c;
			break;

		case 'E':
		case 'e':
			cout << "Thank you." << endl;
			break;

			//if  1, 2, 3 or E is not selected then go to the default case
		default: cout << "Invalid selection. Please try again";
			// no break in the default case
		}
		cout << endl << endl;
	} while (selection != 'E' && selection != 'e');

	cout << "\nYou ordered " << a << " Apples" << endl;
	cout << "You ordered " << b << " Bananas" << endl;
	cout << "You ordered " << c << " Oranges" << endl;

	total(a, b, c);

}
int total(int a, int b, int c)
{

	int sum = a + b + c;
	cout << "Your total is $" << sum << endl;
	cin.get();
	return sum;
}

int main()
{
	Customer a;
	string name;
	string address;
	cout << "Please enter your name." << endl;
	cin >> name;
	cout << "Please enter your address." << endl;
	cin >> address;
	cout << "hello " << name << endl;
	char selection = 'a';
	displayProducts();

	cin.get();
	return 0;
}
Read the complete line with std::getline()
1
2
3
std::getline( std::cin, name ) ;
// ...
std::getline( std::cin, address ) ;

http://www.cplusplus.com/reference/string/string/getline/
if I were to enter John (space) Doe it will skip the next part of the code asking for the users address. If I type just John it is fine and moves on to the next part of code asking for the users address

This happens because cin's >> operator will drop leading whitespace and stop input at the first trailing whitespace i.e. the whitespace b/w "John" and "Doe"
Awesome, I appreciate the help. Thank you!
Topic archived. No new replies allowed.