String values skipped when being entered from keyboard

Hi guys,

I have a program that has a structure with almost all the variables being strings and when I enter them through the keyboard, some of them get skipped.
Here is all the code, so if someone can copy , run it and check if you can find where is the problem, I would really appreciate it. I have commented which string values get skipped on the program :)

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <string> //the header file to use strings
using namespace std;

// required information for one customer
struct customerDetails
{
	string firstName;
	string lastName;
	string address;
	string cityStateZip;
	string telNo;
	double balance;
	string lastPayment;
};

void displayData(customerDetails [], int ); // this function will display the details of a specific customer
int mainMenu(void); // displays the menu and returns a VALID selection to the main program

const int MAX = 20;

int main()
{
	customerDetails customer[MAX];
	int choice;
	string search;
	double outBal = 0;
	
	int i = 1, lastPosition = 0, modified;
	int j = 1 ;
	
	do
	{
	choice = mainMenu();

	switch ( choice )
	{
	case 1:	// stores data for one customer
			i = lastPosition + 1;
			cout<<"First name: ";
			cin >> customer[i].firstName;
			cout<<"Last name: ";
			cin >> customer[i].lastName;
			cout<<"address: ";
			getline(cin, customer[modified].address); //This one gets skipped always
		
			cout<<"Enter City, State and ZIP: ";
			getline(cin, customer[i].cityStateZip);
			cout<<"Telephone number: ";
			getline(cin, customer[i].telNo);
			cout<<"Account Balance: ";
			cin>> customer[i].balance;
			cout<<"Date of last payment: ";
			getline(cin, customer[i].lastPayment); //this one gets skipped always

			lastPosition = i;
			i++;
		break;
	case 2: // asks the user to enter a customer number that they want to modify
			cout<< "Enter the customer # to modify details: ";
			cin >> modified;
			cout<<"First name: ";
			cin >> customer[modified].firstName;
			cout<<"Last name: ";
			cin >> customer[modified].lastName;
			cout<<"address: ";
			getline(cin, customer[modified].address); //skipped
			cout<<"Enter City, State and ZIP: ";
			getline(cin, customer[modified].cityStateZip);
			cout<<"Telephone number: ";
			getline(cin, customer[modified].telNo);
			cout<<"Account Balance: ";
			cin>> customer[modified].balance;
			cout<<"Date of last payment: ";
			getline(cin, customer[i].lastPayment); // skipped
		break;
	case 3: // asks the user to enter a customer number to DISPLAY the data of one customer
			cout << "Enter customer # to display data: ";
			cin >> modified;
			displayData(customer, modified);
		break;
	case 4: // Searches for the entered name or last name | if one of them is found, it will display all the
			// details of that customer, and will ask the user to enter new data for that customer
			cout<<"Enter the first or last name of the person you are searching for: ";
			cin >> search;
			
			while(j <= lastPosition)
			{
				if(search == customer[j].firstName || search == customer[j].lastName)
				{
					displayData(customer, j);
					cout<<"First name: ";
					cin >> customer[j].firstName;
					cout<<"Last name: ";
					cin >> customer[j].lastName;
					cout<<"address: ";
					getline(cin, customer[j].address);
					cout<<"Enter City, State and ZIP: ";
					getline(cin, customer[j].cityStateZip);
					cout<<"Telephone number: ";
					getline(cin, customer[j].telNo);
					cout<<"Account Balance: ";
					cin>> customer[j].balance;
					cout<<"Date of last payment: ";
					getline(cin, customer[j].lastPayment);
					
				}
				else
				{
					cout<< "Customer not found! "<<endl;
				}
				j++;
				
			}
		   
			break;
	case 5: 
			//Add a new costumer at any time
			cout<<"First name: ";
			cin >> customer[lastPosition + 1].firstName;
			cout<<"Last name: ";
			cin >> customer[lastPosition + 1].lastName;
			cout<<"address: ";
			//getline(cin, customer[lastPosition + 1].address);
		
			cout<<"Enter City, State and ZIP: ";
			getline(cin, customer[lastPosition + 1].cityStateZip);
			cout<<"Telephone number: ";
			getline(cin, customer[lastPosition + 1].telNo);
			cout<<"Account Balance: ";
			cin>> customer[lastPosition + 1].balance;
			cout<<"Date of last payment: ";
			getline(cin, customer[lastPosition + 1].lastPayment);

			lastPosition++;
			break;
	case 6: // calculates the outstanding balance
			cout<<"The outstanding balance is: ";
			outBal=0;
			for(i=1; i <= lastPosition; i++)
			{
				outBal += customer[i].balance;
			}
			cout << outBal<< endl;
			break;
	
	case 7: exit(0);
	}

	
	}while(choice != 7 && i <= MAX); //Run the menu until case 7 is selected, and i is less or equal to MAX
	


		return 0;
}

//displays the data of a customer
void displayData(customerDetails con[], int i)
{

		cout << "Customer name: ";
			cout << con[i].firstName << endl;
		cout << "Last name: ";
			cout << con[i].lastName << endl;
		cout << "address: ";
			cout << con[i].address << endl;
		cout << "City, state and ZIP: ";
		cout << con[i].cityStateZip << endl;
		cout << "Telephone #: ";
			cout << con[i].telNo << endl;
		cout << "Account balance: ";
			cout << con[i].balance << endl; 
		cout << "Date of last payment: ";
			cout << con[i].lastPayment << endl;
				
}

//displays the main menu and return a VALID entry
int mainMenu(void)
{
	int choice;
	cout<<endl;
	cout << "1: Enter data " << endl;
	cout << "2: Modify entered data " << endl;
	cout << "3: Display the data " <<endl;
	cout << "4: Search for a customer "<<endl;
	cout << "5: Add a new costumer "<<endl;
	cout << "6: Compute the total outstanding balance " << endl;
	cout << "7: Exit " << endl;

	cout << "Select one of these options from the MENU: ";
	cin >> choice;
	
	while( choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice !=5 && choice != 6 && choice != 7)
	{
		cout << "Select 1 , 2, 3, 4, 5 or 6 from the menu: ";
		cin>> choice;
	}
	
	return choice;

}



I appreciate your time and help!

Thank you
cin >> customer[i].firstName;

These leave junk at the end off the buffer (such as spaces or newlines). I'm guess that when you get to the getline(), your buffer simply contains a n remaining, so it gets "skipped".

Just use getline() for getting all strings and parse the data yourself.
hmmm but why some of them work , example the address does not work, but then, citystatezip works well , getline(cin, customer[i].telNo); works well as well , and then again , it skips getline(cin, customer[i].lastPayment); ... Initially I had all of them on getline() (and still the same thing happened) ...
Well, you currently have getline(cin, customer.address) commented out in your 5th case.

The reason why some of them are skipped is because you are using cin>> somewhere in your program before them (e.g. on line 60). You can either remove the newlines/garbage characters yourself, or you can just getline() anyway into a temporary string and parse the integer yourself using a stringstream.
thanks!!!

I used cin.ignore() right after the statements where I had used cin >> and now its working fine :)
Topic archived. No new replies allowed.