Pointers and Structs Practice Problems - need help

I'm on pointers and structs now and I have these Practice Problems on structures:

Practice problems
1. Write a program that lets the user fill in a single structure with the name, address, and phone
number of a single person
2. Create an array of space ship objects and write a program that continually updates their
positions until they all go off the screen. Assume that the size of the screen
is 1024 pixels by 768
pixels.
3. 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.
4. Write a program that allows a user to enter high scores of a game, keeping tracking of the name
of the user and the score. Add the ability to show the highest score for each user, all scores for a
particular user, all scores from all users, and the list of users.

I've got some code here for the first one, which isn't able to compile.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

struct PersonalInfo
{
    string person_name;
    string person_address;
    int person_phone_number;
};

int main()
{
    cout << "Personal Information Form:\n";
    cout << "\n";
    cout << "Please provide your name: ";
    getline(PersonalInfo->person_name, cin, '\n');
    cout << "Please provide your home address: ";
    getline(PersonalInfo->person_address, cin, '\n');
    cout << "Please provide your phone number: ";
    cin >> PersonalInfo->person_phone_number;

    cout << "Hello, " << PersonalInfo->person_name << " your phone number and address have successfully been noted.";
    cout << " Thank you.\n";
}


I'm getting an error like this for lines 17, 19, 21 and 23:
|17|error: expected primary-expression before '->' token|

Please help me out here. What am I doing wrong?

If possible, please out with the other practice problems, too.
Last edited on
you didn't create any object !
I understood as much after submitting that.

But I'm still have some problems. Namely, with getline().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

struct PersonalInfo
{
    string name;
    string home_address;
    int phone_number;
};

int main()
{
    PersonalInfo person;
    cout << "Personal Information Form:\n";
    cout << "\n";
    cout << "Please provide your name: ";
    getline(person.name, cin, '\n');
    cout << "Please provide your home address: ";
    getline(person.home_address, cin, '\n');
    cout << "Please provide your phone number: ";
    cin >> person.phone_number;

    cout << "Hello, " << person.name << " your phone number and address have successfully been noted.";
    cout << " Thank you.\n";
}


|18|error: no matching function for call to 'getline(std::string&, std::istream&, char)'|

What's the problem here?
wrong format.
Alright, that one's done. Thanks.

Now, I'll go with problem #3 next. If I am to use pointers to store the data for the multiple entries required, what would be the best way to do it?

This is practice problem 3: 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.

How should I do this? Don't just give me an answer, though; help me in a way that'll also make me learn how to do it, please.

As for making that array of spaceships and making them "leave" the screen, what x- and y-coordinates should I use? Like, do I just use numbers that are equal to the number of pixels, or what? If the screen is 1024 x 768 pixels, where would the center be, and where would the edges be, approximately?

And the book says it's possible to allocate data to variables at runtime via pointers. How is that done, exactly?
Last edited on
linked list.
How should I do this? Don't just give me an answer, though; help me in a way that'll also make me learn how to do it, please.


linked list.


You nailed it @anup30 :D
Last edited on
Alright, thanks.

What about the spaceships, though?
Topic archived. No new replies allowed.