簡體   English   中英

將數組結構、ofstream 和整數傳遞給函數

[英]passing an array structure, ofstream, and interger to function

在我開始之前,如果解決方案很簡單,我想道歉。 在編碼方面,我是初學者。

當我嘗試編譯我的 C++ 代碼時,我得到兩個錯誤代碼:

  1. 對“outputFirstSheet(std::basic_ofstream&, int, Info*)”的未定義引用
  2. 錯誤:1d 返回 1 個退出狀態

這是我的代碼:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;




struct Info
{
    char hurricaneName[11];
    int ID;
    int life;
    int date;
    float avgWindSpeed;
    float avgRainFall;
    int totalTornadoes;
};




struct Tropicals
{
    int ID;
};

void outputFirstSheet(ofstream&, int counter, Info*);

int main()
{

    ifstream storms;
    storms.open("storms.txt");


    //This will count how many lines have information in them or are being used.
    int counter = 0;
    while (storms)
    {
        counter = counter + 1;
        storms.ignore(256,'\n');
    }
    //This is the end of that program

    Info names[counter];

    //Both pieces of code below will allow me to go back to the beginning of the storms.txt file
    storms.clear();
    storms.seekg(0, ios::beg);



    while (storms){
        storms.get(names[0].hurricaneName, 11);
        for(int a = 0; a < counter - 1 ; a++)
        {
            storms >> names[a].ID;
            storms >> names[a].life;
            storms >> names[a].date;
            float wind = 0.0;
            float sum = 0;
            for (int b = 0; b < 5;b++)
            {
                storms >> wind;
                sum = wind + sum;
            }
            names[a].avgWindSpeed = sum / 5;
            float rain = 0, total = 0;
            for (int c=0; c < 2; c++)
            {
                storms >> rain;
                total = rain + total;
            }
            names[a].avgRainFall = total / 2;
            storms >> names[a].totalTornadoes;

            //Making sure that I skip to the next line
            storms.ignore(256, '\n');
            storms.get(names[a+1].hurricaneName, 11);
        }

    }
    ofstream outfile;
    outfile.open("Results.txt");
    outfile << fixed << setprecision(2);


    // Printing out all the info in the STORM SUMMARY SHEET
    outputFirstSheet(outfile, counter, names);

    return 0;
}

void outputFirstSheet(ofstream &outfile, int counter, Info names)
{
    cout << "The program's results can be located in 'Results.txt'" << endl;
    outfile << setw(70) << "STORM SUMMARY SHEET" << endl;
    outfile << endl;
    outfile << "Name" << setw(10) << "ID" << setw(20) << " Life  " << setw(10) << "Date" << setw(20) << " Average  " << setw(20) << " Average " << setw(20) << "Tornadoes" << setw(20) << "Storm Level" << endl;
    outfile << "    " << setw(10) << "  " << setw(20) << "in days" << setw(10) << "Date" << setw(20) << "wind speed" << setw(20) << "rain fall" << setw(20) << " spawned " << setw(20) << "   level   " << endl;

    //Start adding the data to the STORM SUMMARY SHEET
    outfile << endl;
    for(int i = 0; i < counter - 1; i++)
    {
        outfile << names.hurricaneName[i];
    }




    outfile << endl << endl << "This code was created by Guillermo Molina Matus" << endl << endl;
}

有錯誤的行是outputFirstSheet(outfile, counter, names); 這個函數應該能夠使用 ofstream,接收計數器信息和 Struct Info 名稱變量,但由於某種原因,我不斷收到錯誤消息。

有人可以讓我知道我做錯了什么嗎?

在函數定義中使用Info* 名稱

此外,數組是使用指針傳遞的,因此您實際上不必為 Info 獲取指針。 您可以簡單地傳遞 Info 數組並像接收帶值的變量一樣接收它。 它使用一個指針。 不制作數組的新副本。

void outputFirstSheet(ofstream &outfile, int counter, Info *names)
{
    cout << "The program's results can be located in 'Results.txt'" << endl;
    outfile << setw(70) << "STORM SUMMARY SHEET" << endl;
    outfile << endl;
    outfile << "Name" << setw(10) << "ID" << setw(20) << " Life  " << setw(10) << "Date" << setw(20) << " Average  " << setw(20) << " Average " << setw(20) << "Tornadoes" << setw(20) << "Storm Level" << endl;
    outfile << "    " << setw(10) << "  " << setw(20) << "in days" << setw(10) << "Date" << setw(20) << "wind speed" << setw(20) << "rain fall" << setw(20) << " spawned " << setw(20) << "   level   " << endl;

    //Start adding the data to the STORM SUMMARY SHEET
    outfile << endl;
    for(int i = 0; i < counter - 1; i++)
    {
        outfile << names->hurricaneName[i];
    }

    outfile << endl << endl << "This code was created by Guillermo Molina Matus" << endl << endl;
}

您的函數定義應如下所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM