1
string firstname, lastname;
string phonenumber, email;
cout << "What is the first of the person that you would like to add? : ";
cin >> firstname;


cout << "What is the last of the person that you would like to add? : ";
cin >> lastname;


cout << firstname << " " << lastname << endl;
cout << "What is the phone number of that person? : ";
cin >> phonenumber;

I need help taking this user input and plugging it into an array. I honestly do not know how to do that, if I could get some help on that it would be great!

C.L.U
  • 11
  • 3
  • `std::vector` will help – ForceBru Dec 08 '16 at 16:07
  • [Find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start reading about *structures* before you start wondering about "dynamic arrays". And if you have a good book, then it will explain dynamic memory allocation as well. – Some programmer dude Dec 08 '16 at 16:07
  • I understand how to use arrays, that have a fixed value, but my professor told me that we had to do this( for a programming assignment), also using structs, should i read up on those as well? – C.L.U Dec 08 '16 at 16:12

1 Answers1

3

Create a struct named Record as follows

struct Record
{
   string firstName, lastName,phone; //etc 
}; 

If you know how many records you want to enter then you have to create an array of Record as follows

Record PersonInfo[5];

Now each index of PersonInfo let say PersonInfo[2] is a complete Record and you can access there fields like

PersonInfo[2].phone = "5655567" //etc

Now if you want to create array of Record but don't know the size then your best bet for now is to use a vector like follows. A vector is an array of changable size. You need to include the following header

#include<vector>

After wards you can do the following

vector<Record> PersonInfo //thats how vectors are declared

The name between <> bractes tell of what type you want a vector of ,you can write int as well. Following is how you add items to a vector

Record r1,r2; // just for example
PersonInfo.push_back(r1);
PersonInfo. push_back(r2);

you may add any number of items in it and you can access them just like array as follows

PersonInfo[0] .lastName // its actually r1.lastName and so on

This may seem difficult for now you may want to learn vectors and their operations before going for dynamic memory allocation which requires that you understand what pointers are. I don't know that you know about pointers and how to use them that is why I reffered you vectors

sasha199568
  • 1,143
  • 1
  • 11
  • 33
Syed Ahmed Jamil
  • 1,881
  • 3
  • 18
  • 34