0

ポインターを理解するのに苦労しています。ちょっとしたガイダンスが必要なところまで来ました。これまでに書いたコードは次のとおりです。

#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++;
    }

}

アイデアは、動的に割り当てられた構造体の配列を作成し、配列の各要素を指すことができるポインターを両方の関数に渡すことです。これは正常にコンパイルされますが、構文がよくわからないため、うまく終了しません。

主な問題は getdata 関数にあると確信しています。あるべきだと思うように修正しようとするたびに、構文エラーが発生します。配列の各要素でポインターが指す値を適切に変更するにはどうすればよいですか?

4

2 に答える 2

1

displaydata()関数では、 index もインクリメントしているため、 を削除する必要p++がありますi。したがって、反復ごとに、実際には配列から 2 番目の次の要素 (0 番目の要素の後、2 番目、次に 4 番目を読み取ります) を読み取ります。したがって、配列の境界を超えてしまいます。

また、あなたのgetdata()メソッドでは、 aは (前の繰り返しから) a にgetline()続くためcin、 によって読み取られずに残された改行文字は、cinによる次の入力として扱われgetline()ます。この問題を回避するcin.get()には、ループの最後に置きます。

したがって、次の 2 つの変更を行う必要があります。

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
    }

}

system("PAUSE");また、ここで説明する理由により、使用を避けてください: system("pause"); - なぜ間違っているのですか? 代わりにcin.get()またはを使用しますgetchar()

于 2012-12-12T06:38:56.303 に答える
0

私はあなたがプログラムを組み立てた方法が好きです。これは、構造体でポインターを使用するという概念を理解できる最も簡単な方法です。

あなたのコードに見られる唯一の間違いは、メイン関数で構造体の配列を作成し、それを渡して入力しましたが、構造体を getdata( ) および dispdata() 関数。

したがって、このコード スニペットを機能させる必要がある場合は、インデックスに基づいて構造体の配列にアクセスする必要があります。例えばAirports *p[1] where 3<i>0

したがって、コードを修正するには2つの方法があります

  1. 構造体の配列を送信する代わりに、単一の構造体を渡します。
  2. 構造体の配列全体を getdata および dispdata 関数に渡し、構造体のセットをループして値を割り当てるか、各構造体の値を表示します (Airports *p)。
于 2012-12-12T06:35:00.307 に答える