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.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main(void)
{
constint 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;
}
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