data entry program help

Evert time I run this program, when I select the first option to add a user, it skips over one entry and goes to the second, for example:

How many users would you like to enter: 5
Enter User:
Enter User:_

It will always skip over the first. I've tried different data types, but for some reason I just can't get it. Sorta new to this so I would appreciate any help.

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

void menu();
void addUser();

int main()
{
	int choice;
	do
	{
		menu();

		cout << "\nPlease select an option: ";
		cin >> choice;

		switch (choice)
		{
		case 1:
			addUser();
			break;
		case 2:
			return 0;
		default:
			cout << "You have entered an invalid character.  Please user numbers 1-2.\n";
		}

	} while (choice != 2);


	system("pause");
	return 0;
}

void menu()
{
	cout << "\nPlease select an option from below";
	cout << "\n----------------------------------";
	cout << "\n1. Add a user to DB";
	cout << "\n2. Exit";
	cout << "\n----------------------------------";
}

void addUser()
{
	string user;
	int userAmt;
	char choice = 'n';

	cout << "How mant users do you wish to enter: ";
	cin >> userAmt;

	do
	{
		for (int i = 0; i < userAmt; i++)
		{
			cout << "User: ";
			getline(cin, user);
			cout << "\nYou entered" << user;
		}

		cout << "Do you wish to enter more users(Y/N): ";
		cin >> choice;
	} while (choice != 'n');
}
getline on line 61 is causing your issue. You could change it to this.

1
2
3
4
cin.sync();
cout << "User: ";
getline(cin, user);
cout << "\nYou entered" << user;


Last edited on
Thanks it worked!
Topic archived. No new replies allowed.