ポインターを理解するのに苦労しています。ちょっとしたガイダンスが必要なところまで来ました。これまでに書いたコードは次のとおりです。
#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 関数にあると確信しています。あるべきだと思うように修正しようとするたびに、構文エラーが発生します。配列の各要素でポインターが指す値を適切に変更するにはどうすればよいですか?