当我输入sentinel值时,为什么程序会崩溃?

时间:2016-03-18 04:38:20

标签: c++ variable-assignment

我的程序运行得很好,直到我决定输入sentinel值来停止程序,并显示错误 “项目4.exe中0x00B474AD抛出异常:0xC0000005:访问冲突写入位置0x00B52FF9。

如果存在此异常的处理程序,则可以安全地继续该程序。“ 我试图弄清楚导致这个问题的原因是什么,可能是因为行被初始化为0所以它始终是真的吗?

这是代码

#include<iostream>
#include<string>
#include <iomanip>
using namespace std;

#include "C:\Users\barta\OneDrive\Documents\Visual Studio 2015\Projects\Project 4\Project 4\array.h"

void displayPlane(char msg[], char[ROW][COL]);
void getData(int &, char &);

int main()
{
    int row = 0;
    char seat;

    while (row != -1)
    {

        displayPlane("\tChesapeaake Airlines", layout);
        cout << endl;
        getData(row, seat);
        cout << endl;
        int COL = seat - 'A';
        if (layout[row - 1][COL] == 'X')
        {
            cout << "Sorry this seat is take" << endl;
        }
        else
        {
            layout[row - 1][COL] = 'X';
        }

    }

    cout << "Have a nice day! " << endl;

    system("pause");
    return 0;
}
void displayPlane(char msg[] , char[ROW][COL])
{
    cout <<msg << endl;
    for (int r = 0; r < ROW; r++)
    { cout << endl;
    cout << setw(4) << r + 1;
        for (int c = 0; c < COL; c++)
        {
            cout << setw(4) << layout[r][c];
        }
    }
}
void getData(int& row, char& seat)
{

    cout << "Enter row <-1 to stop>  ";
    cin >> row;
    cout << "Enter your prefered seat  ";
    cin >> seat;
    seat = toupper(seat);
}

这是头文件

//arrays for airline problem

const int ROW = 9;
const int COL = 4;
const int CTR = 3;

//initial seats in the plane
    char layout[ROW][COL] = {  { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' }};

    int classCtr[CTR] = {0,0,0};

    string classes[] = {"First Class", "Business Class", "Coach"};

    double fare [] = {500, 300, 100};

2 个答案:

答案 0 :(得分:1)

虽然您已确定-1为您的哨兵价值,但您甚至可以打印此信息:

cout << "Enter row <-1 to stop>  ";
cin >> row;

你没有告诉程序有关于-1的任何特殊内容。

因此,行现在等于-1。该计划继续进行。

cout << "Enter your prefered seat  ";
cin >> seat;
seat = toupper(seat);

然后我们回到主要。同样,你没有告诉程序担心row的值,即-1。所以现在执行:

    cout << endl;
    int COL = seat - 'A';
    if (layout[row - 1][COL] == 'X')

问题:如果row为-1,您访问layout的哪个元素?

如果-1是sentinel值,则需要编写代码来强制执行此操作; while条件仅在每次循环迭代开始时进行测试,它不是监督条件。

首先:

void getData(int& row, char& seat)
{
    cout << "Enter row <-1 to stop>  ";
    cin >> row;
    if (row == -1)
        return;
    cout << "Enter your prefered seat  ";
    cin >> seat;
    seat = toupper(seat);
}

其次是主要的:

cout << endl;
getData(row, seat);
if (row == -1)
    break;
cout << endl;
int COL = seat - 'A';

就个人而言,我会更改“getData”以返回指示数据是否被提取的bool,并假设当它返回false时用户输入了sentinel值 - 这样我就不必导出了解功能之外的内容。

bool getData(int& row, char& seat)
{
    cout << "Enter row <-1 to stop>  ";
    cin >> row;
    if (row == -1)
        return false;
    cout << "Enter your preferred seat  ";
    cin >> seat;
    seat = toupper(seat);
    return true;
}

并在主要:

cout << endl;
if (getData(row, seat) == false) // or just if (!getData(row, seat))
    break;
cout << endl;
int COL = seat - 'A';

并将while循环更改为

for (;;)  // infinite loop
{
    ...

答案 1 :(得分:0)

getData(row, seat);
cout << endl;
int COL = seat - 'A';
if (layout[row - 1][COL] == 'X')

如果输入-1,则表示您正在访问

layout[-2]
豫ICP备18024241号-1