Hay Guys,
my question is related to a practice problem in the book "Jumping into C++":
Create an address book program that builds on problem #1—this time, the user should be able to not just fill out a single structure, but should be able to add new entries, each with a separate name and phone number. Let the user add as many entries as he or she wants—is this easy to do? It is even possible? Add the ability to display all, or some of the entries, letting the user browse the list of entries.
Oh yeah.. the problem one was:
Write a program that lets the user fill in a single structure with the name, address, and phone number of a single person
So the first problem I solved quiet good (I hope)
Code:
#include <iostream>
using namespace std;
// define struct
struct personalInformation
{
string firstName; string lastName;
string address;
int phoneNumber;
};
int main()
{
// Initialize struct
personalInformation person;
// Get and Save Information
// Sorry if you don't understand the strings...
// Just asking for Informations
cout << "Eingabe von Informationen" << endl;
cout << "Vorname der Person: "; cin >> person.firstName;
cout << "Nachname der Person: "; cin >> person.lastName; cin.ignore();
cout << "Addresse der Person: "; getline(cin, person.address, '\n');
cout << "Telefonnummer der Person: "; cin >> person.phoneNumber;
cout << "\n\n" << person.firstName << " " << person.lastName << endl;
cout << person.address << endl;
cout << person.phoneNumber << endl;
return 0;
}
My question now is...
What should I do? (The problem i mentioned earlier)
"Let the user add as many entries as he or she wants"
I really have no Idea how I should save all of this data.
(If it would be a lot)
An array won't do it, because I have to set the size of it before the program compiles.
I guess I have to write something similiar to this:
Code:
#include <iostream>
using namespace std;
// define struct
struct personalInformation
{
string firstName; string lastName;
string address;
int phoneNumber;
};
personalInformation getPersonInformation()
{
// Initialize struct
personalInformation person;
// Get and Save Information
// Sorry if you don't understand the strings...
// Just asking for Informations
cout << "Eingabe von Informationen" << endl;
cout << "Vorname der Person: "; cin >> person.firstName;
cout << "Nachname der Person: "; cin >> person.lastName;
cout << "Telefonnummer der Person: "; cin >> person.phoneNumber;
return person;
}
int main()
{
int newEntries;
cout << "How many entries should be add?" << endl;
cin >> newEntries;
for( int i = 0; i < newEntries; i++ )
{
cout << "\n";
somehowSafeInformation = getPersonInformation();
}
return 0;
}
Do you see the bold underlined text?
There is the part where I don't know how to progress...
So I hope u can give me some advices.
Thank you