-2

This program seems to work fine until it goes to display the data after the sort function, it will display by destination but it bypasses displaying by airline. Then it gives the access violation window and I'm not 100% sure on what it even means. If anybody could explain and help that would be much appreciated.

#include <iostream>
#include <string>

using namespace std;

double  flight_number[3], number_passengers[3], max_records, max_pass, min_pass;
string  airline_name[3], destination[3], duplicate[3];
char    reply, swapping;
int     row, index[3];

void setup();
void load_arrays();
void display_airline();
void display_destination();
void sort();

int main()
{ 
    setup();
    load_arrays();
    display_airline();
    display_destination();

system("pause");
return 0;
}

void setup()
{
max_records = 3;
max_pass = 275;
min_pass = 50;
}

void load_arrays()
{
for (row = 0; row < max_records; row++)
{
    cout << "Charter Number " << row << endl << endl;
    cout << "Please enter..." << endl;
    cout << "Airline Name----> ";
    cin >> airline_name[row];
    cout << endl;
    cout << "Flight Number----> ";
    cin >> flight_number[row];
    cout << endl;
    cout << "Destination----> ";
    cin >> destination[row];
    cout << endl;
    cout << "Passenger Load----> ";
    cin >> number_passengers[row];
    cout << endl;

    while (number_passengers[row] < min_pass || number_passengers[row] > max_pass)
    {
        cout << "You have entered an invalid amount of passengers." << endl;
        cout << "There must be between 50 and 275 passengers. > ";
        cin >> number_passengers[row];
        cout << endl;
    }
}
}

void display_airline()
{
system("cls");

for (row = 0; row < max_records; row++)
{
    duplicate[row] = airline_name[row];
}

sort();

cout << "Airline" << "  Flight" << "    Destination" << "   Passenger" << endl;
cout << "Name" << " Number" << "            Load" << endl;

for (row = 0; row < max_records; row++)
{
    cout << airline_name[index[row]] << " " << flight_number[index[row]] << " " << destination[index[row]] << " " << number_passengers[index[row]];
    cout << endl;
}
}

void display_destination()
{
system("cls");

for (row = 0; row < max_records; row++)
{
    duplicate[row] = destination[row];
}

sort();

cout << "Destination" << "  Flight" << "    Flight" << "    Passenger" << endl;
cout << "       Name" << "  Number" << "        Load" << endl;

for (row = 0; row < max_records; row++)
{
    cout << destination[index[row]] << " " << airline_name[index[row]] << " " << flight_number[index[row]] << " " << number_passengers[index[row]];
    cout << endl;
}
}

void sort()
{
for (row = 0; row < max_records; row++)
{
    index[row] = row;
}

swapping = 'Y';

while (swapping == 'Y')
{
    swapping = 'N';

    for (row = 0; row < max_records; row++)
    {
        if (duplicate[row] > duplicate[row + 1])
        {
            swap (duplicate[row], duplicate[row + 1]);
            swap (index[row], index[row + 1]);

            swapping = 'Y';
        }
    }
}
}

also this is the full violation error: Unhandled exception at 0x009984F6 in Program 6.exe: 0xC0000005: Access violation reading location 0x03947188.

  • 1
    Lacks [SSCCE](http://www.sscce.org). And by the way: Those global variables are a *very* bad idea. – Baum mit Augen Mar 13 '15 at 23:38
  • Care to explain? I'm quite new. @BaummitAugen – MasterKindew Mar 13 '15 at 23:44
  • 1
    Please do not give a huge code dump without following the guidelines in the help center. –  Mar 13 '15 at 23:45
  • 1
    @MasterKindew For explanation of SSCCE, click link. For "Why are those global variables bad?": Read a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Baum mit Augen Mar 13 '15 at 23:47
  • When you used the debugger, which lines were causing the issue? – Thomas Matthews Mar 13 '15 at 23:50
  • The line it highlighted was when it is going to display the data Example(cout << airline_name[index[row]]) @ThomasMatthews – MasterKindew Mar 13 '15 at 23:52
  • @MasterKindew: And when you look at `row` in your debugger, what value did that have? – Mooing Duck Mar 14 '15 at 00:11
  • @MooingDuck `row` still has a value of 1 when the error occurs – MasterKindew Mar 14 '15 at 00:15
  • @MasterKindew: I just noticed, you're acting as if all your arrays are one-based. You're completely bypassing the first element of every array. That's a bug. The fact that it only asks for three airlines would be a giveaway though. – Mooing Duck Mar 14 '15 at 00:20
  • @MooingDuck Okay so I went and changed the for statements to set `row` = 0 but now it only displays airlines and does not execute the airline function, but it does do destination then after the error occurs again – MasterKindew Mar 14 '15 at 00:31
  • I meant it displays by destination not airline. @MooingDuck – MasterKindew Mar 14 '15 at 00:42
  • [BOOM](http://coliru.stacked-crooked.com/a/3bdc336049f61a15) – Mooing Duck Mar 14 '15 at 00:46
  • @MooingDuck Not sure if there is a PM feature here but if there is, would you have the time to explain what is happening and what you have done? I would really appreciate that! But hell it works! :) – MasterKindew Mar 14 '15 at 00:50
  • There isn't a PM feature other than chat, and it's time for me to go. But basically it's a few steps closer to how I would have written each part. Though in truth I wouldn't have used a global array. – Mooing Duck Mar 14 '15 at 00:56

1 Answers1

0
for (row = 0; row < max_records; row++) 
    if (duplicate[row] > duplicate[row + 1]). 

What's duplicate[row+1] when row==3? You want the sort to go to max_records-1 since it works on pairs of elements rather than individual elements.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158