-3

私のコンパイラは、私が書いた次のプログラムをコンパイルしません。Main()関数で宣言したにもかかわらず、「識別子が宣言されていません」とマークした行でエラーが発生します。

プログラムは不完全ですが、アクティビティに関する入力を受け取り、出力します。

#include <iostream.h>
#include <conio.h>


void addToLog();
void viewLog();

void what()
{
    cout << "What would you like to do?" << endl
         << "1. View Today's Log" << endl
         << "2. Add to Today's Log" << endl
         << "__________________________" << endl << endl
         << "? -> ";

    int in;

    cin  >> in;

    if ( in == 1 )
    {
        viewLog();
    }

    if ( in == 2 )
    {
        addToLog();
    }

}

void main()
{
    clrscr();


    struct database
    {
        char act[20];
        int time;
    };

    database db[24];



    what();



    getch();
}

void addToLog()
{
    int i=0;
    while (db[i].time == 0) i++;

    cout    << endl
        << "_______________________________"
        << "Enter Activity Name: ";
    cin     >> db[i].act;                           // <-------------
    cout    << "Enter Amount of time: ";
    cin >> db[i].time;
    cout    << "_______________________________";

    what();

}

void viewLog()
{
    int i=0;
    cout    << "_______________________________";
    for (i = 0; i <= 24; i++)
    {
        cout    << "1. " << db[i].act << "   " << db[i].time << endl; // <-------
    }
    cout    << "_______________________________";

    what();
}
4

1 に答える 1

5

;dbでローカル変数として宣言しました。main()他の機能では見ることができません。

少なくとも2つの解決策があります。

  1. グローバルdb(静的)変数を作成します-つまり、その宣言/定義をから移動しmain()ます。 グローバル変数に過度に依存することは通常は不適切な方法であるため、これは一般的には推奨されません。
  2. dbポインタを必要とする関数にポインタを渡します。
于 2012-04-29T13:29:36.673 に答える