Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:05:40+00:00 2026-06-15T19:05:40+00:00

I’m having a lot of trouble understanding pointers, and I’ve hit a point where

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:05:41+00:00Added an answer on June 15, 2026 at 7:05 pm

    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()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.