For loop problem

Nov 22, 2012 at 3:38am
I'm very new to c++. I was just practicing and i decided to make a little secret santa program. Everything is working fine except for the first for loop.(line 22) After i ask how many people are participating, it skips the first person and moves on to the second person. I cant find whats wrong. Thanks.

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

int main(void)
{
    const int array_size=15;
	int n=0;
    string person_name [array_size];
	string person [array_size];
	srand(time(0));
	char move;
	
	
	cout<<"Secret Santa Program"<<endl;
	
	cout<<endl<<"How many people will be participating this year?"<<endl;
	cin>>n;
	
	for (int x=0;x<n;x++)
		
	{	cout<<endl<<"Enter your name: ";
		getline (cin,person_name[x]);
		person[x]=person_name[x];
	}
	
	cout << string(50, '\n');
		
	for (int i=0; i<(n-1);i++)
	{		int r=0;
			do
			 r= i + (rand()% (n-i));
			while (r==i);
			string temp= person[i];person[i]=person[r];person[r]=temp;
		}
	
	for (int c=0;c<n;c++)
		{
			cout<<person_name[c]<<"'s Turn:"<<endl;
			cout<<endl<<"Enter any key to pick your person..."<<endl;
			cin>>move;
			cout << string(50, '\n');
			cout<<endl<<"You picked:\t"<<person[c]<<endl;
			cout<<endl<<"Enter any key to continue..."<<endl;
			cin>>move;
			cout << string(50, '\n');
		}
		cout<<"Press any key to quit..."<<endl;
		cin>>move;
	

return 0;
}
Nov 22, 2012 at 7:53am
Im a noob myself soo I hope this helps. At line 20 you are using cin which puts '\n' in the buffer stream, thats why it skips the getline at line 25.
Try using cin.ignore() after the line 20.. Like
1
2
cin>>n;
cin.ignore();
Nov 22, 2012 at 10:59pm
Nice it works! Thanks! i got to remember that.
Nov 23, 2012 at 4:40pm
Glad I could help out!
Topic archived. No new replies allowed.