Thread: Puzzled

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Puzzled

    Hi again, I made a program that uses parallel arrays to print out flight numbers and transactions.
    My problem is that when i compile it, it shows no errors, when i run it, the output window crashes.
    here is my program:
    Code:
    #include <iostream>
    #include <cmath>
    #include <fstream>
    using namespace std;
    
    int initialize(int [], int[], int);
    void printBookings(int [], int [], int);
    int findFlight( int [], int, int);
    
    int main()
    {
        ifstream trans;
        trans.open("trans.dat");
        int flightNo[20]; 
        int bookings[20];
        int read, type, flight, seat,number,i;
        read = 0;
        type = 0;
        flight = 0;
        seat = 0;
        initialize(flightNo, bookings, 20);
        printBookings(flightNo, bookings, 20);
        trans >> type >> flight>> seat;
        cout <<"Transactions"<<endl;
        while( type!= -1)
        { 
               if( type == 1)
               {
                   if( findFlight(flightNo, 20, flight) != -1)
                   {
                   bookings[findFlight(flightNo, 20, flight)]+= seat;
                   cout<< seat<< " "<< "seats booked on flight"<<" "<< flight<<endl;
                   }
                   else
                   if( findFlight(flightNo, 20, flight) == -1)
                   cout<<"Invalid Flight Number"<<endl;
               }
               else
               {
                   if(type == 2)
                   {
                           if(seat < bookings[findFlight(flightNo, 20, flight)])
                           {
                                   bookings[findFlight(flightNo, 20, flight)]-= seat;
                                   cout<< seat<<" "<<"seats cancelled from flight"<<" "<<flight<<endl;
                           }
                           else
                           if(seat > bookings[findFlight(flightNo, 20, flight)])
                           {
                                   seat-= bookings[findFlight(flightNo, 20, flight)];
                                   cout<<seat<< " "<<"seats were not cancelled"<<endl;
                           }
                   }
                   else
                   cout<<"Invalid Transaction Type"<<endl;
                   }
                   trans >> type >> flight>> seat;
        }
        printBookings(flightNo, bookings, 20);
        system("pause");
        return 0;
    }
    
    int initialize(int flightNo[], int book[], int count)
    {
        int read= 0;
        int i,number;
        ifstream flightnum;
        flightnum.open("flightnum.dat");
        flightnum>>number;
        while(number != -1)
        {
              flightNo[i] = number;
              flightnum>>number;
              read++;
        }
         for( i=0; i<count; i++)
         {
              book[i] = 0;
              read++;
         }
         return read;
     
    }    
    void printBookings(int flightNo[], int bookings[], int count)
    {
         int i;
         cout << "Flight Numbers"<< "  "<<"Current Bookings"<< endl;
         for( i =1; i<count; i++)
              cout << flightNo[i] << "    "<< bookings[i]<<endl;
    }
    int findFlight( int flightNo[], int count, int num)
    {
        int i, index=0;
        for(i = 0; i<count; i++)
        {
              if( flightNo[i] == num)
              return index;
              index++;
        }
        return -1;
    }

    it reads from 2 files:
    1 157 200
    2 302 150
    1 673 300
    1 211 50
    1 358 100
    3 673 10
    2 358 400
    2 673 250
    5 412 150
    1 266 40
    3 409 0
    1 673 75
    1 358 500
    3 412 0
    2 157 400
    1 409 300
    2 358 200
    -1 -1 -1

    and

    511
    358
    302
    412
    208
    673
    157
    129
    266
    409
    -1

    i dont know why my screen keeps crashing

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int initialize(int flightNo[], int book[], int count)
    {
        int read= 0;
        int i,number;
        ifstream flightnum;
        flightnum.open("flightnum.dat");
        flightnum>>number;
        while(number != -1)
        {
              flightNo[i] = number;
              flightnum>>number;
              read++;
        }
         for( i=0; i<count; i++)
         {
              book[i] = 0;
              read++;
         }
         return read;
     
    }
    Variable i is never initialized but is used in your loop anyway. Pretty good way to (usually) get a crash.
    Aside from that, this is very much C and your program contains more subtle bugs that you haven't thought of.
    Perhaps you should use a debugger to get to the bottom of all those bugs.
    Last edited by Elysia; 12-01-2007 at 05:03 PM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Thank You very much. That helped a lot. After the output started functioning properly, I made many modifications.
    The program works just right now.
    Thank You again for your help!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'd like to ask - are you intending to write C or C++?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    its for c++

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then for one thing, get rid of those arrays and use std::vector.
    http://www.cprogramming.com/tutorial/stl/vector.html

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int flightNo[20];
    Have something like
    const int maxFlights = 20;

    then
    int flightNo[maxFlights];

    and then replace all your other usage of 20 through the code.
    Though Elysia's suggestion of using a vector is also a good idea.

    > bookings[findFlight(flightNo, 20, flight)]
    And if findFlight() returns -1, then what?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Puzzled.
    By silhoutte75 in forum C Programming
    Replies: 13
    Last Post: 01-21-2008, 05:17 PM
  2. Replies: 4
    Last Post: 07-15-2005, 04:10 PM
  3. Puzzled
    By fkheng in forum C Programming
    Replies: 5
    Last Post: 06-22-2003, 09:56 PM
  4. Puzzled
    By sketchit in forum C Programming
    Replies: 3
    Last Post: 10-30-2001, 08:11 PM
  5. Still puzzled [I hope this formatted]
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 09-28-2001, 12:26 AM