Please show your input file "contacts.txt".

Do you realize that your run line ("./project1 <contacts.txt") is using input redirection? Which means that the above file should contain all your input in the order required.