Dustin Burns
Dustin Burns

Reputation: 307

Passing pointers to structures to functions

I'm having a lot of trouble understanding pointers, and I've hit a point where I need a little guidance. Here is the code I've written so far:

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

//Declare structure
struct Airports{
    string name;
    string airID;
    double elevation;
    double runway;};

void dispdata(Airports *);
void getdata(Airports *);


int main()
{
    Airports *airptr; 
    airptr = new Airports [3];

    getdata(airptr);
    dispdata(airptr);

    system ("PAUSE");
    return 0;

}

void getdata(Airports *p)
{
    for (int i = 0; i < 3; i++)
    {
        cout << "Enter the name of airport " << i+1 << ": ";
        getline(cin, p->name);
        cout << "Enter the airport " << i+1 << " identifier: ";
        getline(cin, p->airID);
        cout << "Enter the elevation for airport " << i+1 << ": ";
        cin >> p->elevation;
        cout << "Enter the runway length for airport " << i+1 << ": ";
        cin >> p->runway;
        cout << endl;

        p++;
    }

    cout << "Thanks for entering your values!";
}

void dispdata(Airports *p)
{
    cout << "\nHere are the data values you entered:" << endl;
    cout << "\n\t\tAirport info" << endl;
    cout << "Airport\tAirID\tElevation\tRunway Length" << endl;
    cout << "----------------------------------------------------------------" << endl;

    cout << fixed << setprecision(2);

    for (int i = 0; i<3; i++)
    {
        cout << p[i].name << "\t" << p[i].airID << "\t" << p[i].elevation << "\t"     << p[i].runway << endl;
        p++;
    }

}

The idea is to create a dynamically allocated array of structures and to pass a pointer that can point to each element of the array to both functions. This compiles successfully, but because I don't quite get the syntax for this it doesn't end well.

The main problem lies in the getdata function I'm sure. Every time I try to correct it to how I think it should be I get a syntax error. How can I properly change the value the pointer points to in each element of the array?

Upvotes: 0

Views: 101

Answers (2)

dreamweiver
dreamweiver

Reputation: 6002

I like the way you framed the program, this is the simplest way one can understand the concept of using pointers with structures.

The only mistake I see in your code is that, although you have created a array of structures in your main function and passing the same to populate, but you made a mistake in retrieving the structure as a "array of structures" in your getdata() and dispdata() functions.

So if you have to make this code snippet a working one, you need to access the array of structures based on indexes. eg Airports *p[1] where 3<i>0.

So there are two ways of fixing your code

  1. either pass a single structure instead of sending array of structures.
  2. pass entire array of structures to getdata and dispdata functions and loop around the set of structures to assign values or to display values for each structure (Airports *p).

Upvotes: 0

Masked Man
Masked Man

Reputation: 11075

In your displaydata() function, you will have to remove the p++, because you are also incrementing the index i, thus per iteration, you are actually reading the 2nd next element (after 0th element, you will read 2nd, and then 4th) from the array, and hence you will go past the array bound.

Also, in your getdata() method, since a getline() follows a cin (from the previous iteration), the newline character left unread by cin will be treated as the next input by getline(). To avoid this problem, put cin.get() at the end of the loop.

Hence, you need to make 2 changes:

void getdata(Airports *p)
{
    for (int i = 0; i < 3; i++)
    {
        cout << "Enter the name of airport " << i+1 << ": ";
        // ... skipping ...
        cin >> p->runway;
        cout << endl;
        cin.get();    // put this line to "absorb" the unwanted newline

        p++;
    }


void dispdata(Airports *p)
{
    // ... skipping ...
    for (int i = 0; i<3; i++)
    {
        cout << p[i].name << "\t" << p[i].airID << "\t" << p[i].elevation << "\t"     << p[i].runway << endl;
//        p++;    // remove this line, for reason described in the answer
    }

}

Also, please avoid using system("PAUSE"); for reasons discussed here: system("pause"); - Why is it wrong? Instead use cin.get() or getchar()

Upvotes: 1

Related Questions