(1) You only have one phoneNumber object, despite what the user enters for num. If you want multiple phoneNumber objects, dynamically allocate an array of type phoneNumber and size 'num'. Better yet, use a vector.
(2) Better to change the type of 'number' to a long instead of int, phone numbers are usually 10 digits long.
Vectors have one important advantage with respect to C-style arrays: vectors can be resized during the execution of the program to accommodate any extra elements as needed
#include <iostream>
#include <string>
#include <vector>
struct contact
{
std::string name;
std::string number; // string: allows phone numbers like 1-212-736-5000
};
int main()
{
std::vector<contact> all_contacts ; // vector to hold all the contacts (initially empty)
std::size_t num; // std::size_t: an unsigned integer type suitable for indexing into an array
std::cout << "Contacts\nEnter the number of all_contacts you want: ";
std::cin >> num;
// while the number of contacts in the vector is less than num
while( all_contacts.size() < num )
{
contact this_contact ;
std::cout << "Enter the name: ";
std::cin >> this_contact.name; // single word, no embedded spaces
std::cout << "Enter the number for " << this_contact.name << ": ";
std::cin >> this_contact.number; // we will assume that it is a valid phone number
// push_back: append this contact to the end of the vector
all_contacts.push_back(this_contact) ; // note: the size of the vector increases by one
}
std::cout << "All your contacts:\n";
// range-based loop: http://www.stroustrup.com/C++11FAQ.html#forfor( const contact& ct : all_contacts ) // for each contact ct in the vector all_contacts
{
// print out the name and phone number
std::cout << ct.name << ": " << ct.number << '\n' ;
}
}
But when I enter a name with a space like: Donald Trump ( I don't mean anything if you love Don ), it doesn't appear the "Enter the number for Donald Trump:", it breaks! The enter the number just appear if my names doesn't have a space like Donald
This is because Donald Trump has a space between the first and second name so two strings are read from the >> stream instead of one and getline(etc) overcomes this.
If you use namespace std; you don't have to write std::getline(etc) which is confusing for beginners. Learn that later on :)
So now you need to modify your program with two things - 1. array, 2. getline()
Arrays are probably better for a newbie, it's up to you whether you use vectors because it is not the only STL container you can use and <maps> and others are functionally better anyway but they come a bit later for beginners if you are one.
Note also going from a cin line to a containing a getline may need stream correction lines:
1 2
cin.clear();
cin.ignore(1000, '\n');
// there are better choices than 1000 but it's good enough
Now I change it to getline ( cin, contact[i].name );. But after I entered the number of contacts, it appeared: Enter the name: Enter the number for: . What the heck is going on ?
for (int i = 0; i < num; i++)
{
cin.clear(); // <--
cin.ignore(1000, '\n'); // <--
cout << "Enter the name: ";
getline(cin, contact.name); // <--
cout << "Enter the number for " << contact.name << ": ";
cin >> contact.number; // <-- modify as you did for array index i
}
You need something like this with the modification you have for arrays similar to the advice from gentleguy. You don't need vectors for what you are doing. :)