【问题标题】:Array contents changing when passing it to a particular function数组内容在传递给特定函数时发生变化
【发布时间】:2011-03-13 08:40:40
【问题描述】:

我正在将一个填充了一个函数的 2d 传递给我程序中的所有其余函数。出于某种原因,当我在其中一个函数中输出时,内容似乎发生了变化,尽管它在它所在的其他两个函数中输出良好。我相信我传递数组的方式与其他函数完全相同,所以令人费解内容在我身上发生了变化。此函数与成功传递数组的其他函数之间的唯一区别是,此函数是从函数内部调用的,而不是从 main 调用的。是否愿意提供一些关于可能发生的事情的见解?

标题:

#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <string>

using namespace std;

void extern readFile(ifstream&, int&, int&, int&, int&, int[][6]);
void extern userInput(int&, int&, int&, int&, int&, int&, char&, char&, int[][6]);
void extern findSeats(int&, int&, int&, int&, int&, int&, char&, char&, int[][6]);



#endif // HEADER_H_INCLUDED

主要:

#include "header.h"

int main()
{
    ifstream inFile;
    int FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum;
    int airplane[100][6];
    char ticketType, seatType;

    cout << setw(48) << "Thank you for choosing Cheeta Airlines!" << '\n' << '\n' << endl;
    ifstream inData;

    inData.open("Airplane.txt");

    if (!inData)
    {
        cout << "Cannot open the input file."
             << endl;
            return 1;
    }

    readFile(inFile, FC_Row, FC_Col, EconRow, EconCol, airplane);
    userInput(FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum, ticketType, seatType, airplane);
}

readFile:(数组被填充然后在这个函数中打印)

#include "header.h"

void readFile(ifstream& inFile, int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int[][6])
{
    int a, b;
    int airplane[100][6];

    inFile.open("Airplane.txt");

    inFile >> FC_Row >> FC_Col >> EconRow >> EconCol;

    for (a = 0; a < FC_Row; a++)
    for (b = 0; b < FC_Col; b++)
        inFile >> airplane[a][b] ;


    for (a = 0; a < EconRow; a++)
    for (b = 0; b < EconCol; b++)
        inFile >> airplane[a + FC_Row][b] ;


    cout << setw(11)<< "A" << setw(6) << "B"
    << setw(6) << "C" << setw(6) << "D"
    << setw(6) << "E" << setw(6) << "F" << endl;
    cout << " " << endl;

    cout << setw(30) << "First Class: $2,000" << endl;
    cout << '\n';
    for (a = 0; a < FC_Row; a++)
    {
        cout << "Row " << setw(2) << a + 1 << ":";
        for (b = 0; b < FC_Col; b++)
        cout << setw(5) << airplane[a][b] << " ";

        cout << endl;
    }

    cout << '\n';
    cout << setw(30) << "Economy Class: $750" << endl;
    cout << '\n';
    for (a = FC_Row; a < (EconRow + FC_Row); a++)
    {
        cout <<"Row " << setw(2)<< a + 1 << ":";
        for (b = 0; b < EconCol; b++)
        cout << setw(5) << airplane[a][b] << " ";

        cout << endl;
    }


}

userInput: (数组被传递给这个函数是因为在这个函数中调用的函数使用了这个数组。这个数组在这个函数中打印,其中的内容与它填充的函数中的内容相同在,读取文件)

#include "header.h"

void userInput(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int[][6])
{
    char reply;
    int airplane[100][6];

    cout << '\n' << "* The chart above is a seating diagram for your flight. 1 indicates that the " << '\n' << "  seat is taken, 0 indicates that the seat is free.";
    cout << " Refer to this chart to help   you make your class and seating selection." << endl;
    cout << '\n';

    cout << "- How many tickets will you be purchasing today?" << endl;
    cin >> ticketNum;

    while (!cin)
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "INVALID DATA!!!!!" << endl;
          cout << "Number of tickets must be a NUMBER." << endl;
          cout << "try again:" << endl;
          cin  >> ticketNum;
        }

         while (ticketNum > ((FC_Row * FC_Col) + (EconRow * EconCol)))
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "The number of tickets you need exceeds the amount of seats we have on that plane! please choose a different amount of tickets!"
          <<"[ or a different airline :( ]" << endl;
          cin >> ticketNum;
        }

         while (ticketNum < 1)
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "The number of tickets must be greater than 0." << endl;
          cout << "try again:" << endl;
          cin  >> ticketNum;
        }

    for (int j = 0; j < ticketNum; j++)
    {
    cout << '\n' << "Ticket # " << j +1 << " selection:" << endl;
    cout << "--------------------------------"<< endl;

    cout << "- Ticket type? First class or Economy class (enter F/E)" << endl;
    cin >> ticketType;
    ticketType = toupper(ticketType);

    while (ticketType != 'F' && ticketType != 'E')
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "INVALID DATA!!!!!" << endl;
          cout << "Please indicate your ticket type by entering either F (for First Class) " << '\n' << "or E (for Economy Class)" << endl;
          cout << "try again:" << endl;
          cin  >> ticketType;
        }

    cout << "- preferred seat type? Window, Aisle, or No preference (enter W/A/N)" << endl;
    cin >> seatType;
    seatType = toupper(seatType);

    while (seatType != 'W' && seatType != 'A' && seatType != 'N')
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "INVALID DATA!!!!!" << endl;
          cout << "Please indicate your preferred seat type by entering either W (for Window seat), " << '\n' << "A (for Aisle seat), or N (for No preference)" << endl;
          cout << "try again:" << endl;
          cin  >> seatType;
        }


    cout << "- row number? 1-" << (FC_Row) << " in First Class," << (FC_Row+1) << "-" << (FC_Row + EconRow) << " in Economy Class" << endl;
    cin >> rowNum;

    while (rowNum > (FC_Row + EconRow))
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "Your row number exceeds" << (FC_Row + EconRow) << endl;
          cout << "try again:" << endl;
          cin >> rowNum;
        }

    while (rowNum < 1)
        {
          cin.clear();
          while(cin.get()!='\n');
          cout << "The row number must be greater than 0." << endl;
          cout << "try again:" << endl;
          cin  >> rowNum;
        }

    if (ticketType == 'F')
        {
                while (rowNum > (FC_Row))
                    {
                    cout << "That row is not located in our first class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
                    cin >> reply;
                    reply = toupper(reply);

                        while (reply != 'Y' && reply != 'N')
                            {
                            cin.clear();
                            while(cin.get()!='\n');
                            cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
                            cout << "try again:" << endl;
                            cin >> reply;
                            reply = toupper(reply);
                            }
                         if (reply == 'Y')
                            {
                                ticketType = 'E';
                                break;
                            }
                        else
                            {
                                cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
                                cin >> rowNum;
                            }
                    }
        }

        if (ticketType == 'E')
        {
            while (rowNum <= (FC_Row))
                    {
                    cout << "That row is not located in our economy class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
                    cin >> reply;
                    reply = toupper(reply);

                        while (reply != 'Y' && reply != 'N')
                            {
                            cin.clear();
                            while(cin.get()!='\n');
                            cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
                            cout << "try again:" << endl;
                            cin >> reply;
                            reply = toupper(reply);
                            }
                         if (reply == 'Y')
                            {
                                ticketType = 'F';
                                break;
                            }
                        else
                            {
                                cout << "Then choose a row numbered " << (FC_Row + 1) << "-" << (FC_Row + EconRow) << endl;
                                cin >> rowNum;
                            }
                    }
        }
        findSeats(FC_Row, FC_Col, EconRow, EconCol, ticketNum, rowNum, ticketType, seatType, airplane);
    }
}

findSeats:(这是数组似乎更改其内容的函数。我知道我正在正确打印数组,因为我使用的代码与我在它在其他功能中正确打印。)

#include "header.h"

void findSeats(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int[][6])
{
    int airplane[100][6], a, b;  
}

【问题讨论】:

  • 您真的应该从上一个问题中听取@dribeas 的建议。不要通过头文件中的“使用”来污染 std 命名空间的所有内容。
  • 为什么不呢?我的理解是 using namespace std;使用,所以我不必在所有 cin 和 cout 语句之前输入 std:: 或类似的东西。
  • 我把这些问题弄糊涂了……抱歉。您应该使用“使用命名空间 std”或任何其他命名空间,但在源文件中。将它放在头文件中是不好的做法,因为您将强制包括您的头在内的任何人使用命名空间 std。
  • 好东西我的教授教把它放在标题中,嗯? -_-
  • 不知道你的教授这样做的原因。只需在stackoverflow上询问社区stackoverflow.com/questions/1452721/…stackoverflow.com/questions/1265039/using-std-namespace

标签: c++ arrays


【解决方案1】:

您实际上并没有将数组传递给任何函数。您的声明“int[][6]”声明该函数采用一个具有六列的二维数组,但由于您没有给它命名,因此不能在函数中使用它。您声明的飞机变量是每个函数中完全独立的局部变量。试着把你的函数写成

void findSeats(int& FC_Row, int& FC_Col, int& EconRow, int& EconCol, int& ticketNum, int& rowNum, char& ticketType, char& seatType, int airplane[][6])
{
  int a,b;
}

在每个函数中进行相同的更改。

【讨论】:

  • 约翰,谢谢你的回答。如果我没有在 readFile 函数(以及其他函数)中声明飞机,我会收到一条错误消息,指出飞机未声明
【解决方案2】:

您主要声明了飞机[][]。然后你在 readFile 中重新声明它,它实际上隐藏了你的第一个声明。该数组在堆栈上声明,当您的程序从 readFile 返回时,您刚刚填充的数组将被销毁,您最终将得到包含乱码的原始数组。这就是为什么你打印不同的东西。仅使用一个数组。不要在 readFile 函数中重新声明它。

【讨论】:

  • celavek,感谢您的回答。如果我没有在 readFile 函数(以及其他函数)中声明飞机,我会收到一条错误消息,指出飞机未声明。
  • @Matt 更正函数签名(参见 John Gordon 的回答)。它不一定是飞机,例如 readFile() 中二维数组的函数参数的名称 - 不要将函数参数与您传入的实际参数混淆。
【解决方案3】:

您是在谈论您的airplane 数组吗?

您不是在传递它,而是在每个函数中声明它。

【讨论】:

    【解决方案4】:

    为确保您的函数不会修改通过引用传递的输入参数,请使用const

    例如

    const int&amp; someArray[]

    或类似的应该禁止脚本执行此操作(如果不打算更改,可能会在其被更改的地方引发编译时错误)。

    问候,
    丹尼斯 M.

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 1970-01-01
      • 2017-06-24
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2015-10-16
      相关资源
      最近更新 更多