Can some one help with this please.

I need to update a Passengers Name, im using Strings not Char.

if (choice == 2) // Updates Record..
            
            
		{
              merge_sort(0,N_PASSENGER-1);
              
              cout << "\n\t Enter the Seat Number:";
              cin >> key
              
           k=binarySearch(passenger, 0, N_PASSENGER, key); // Search Array 
            if(k>=0) 

                
                cout << " Enter Passengers Name:"
                        passenger->name;
                         cin >> stArray[i].name;
                         myFile << stArray[i].name;
          

		}

Just Updated my Code.


this is the Update part..

if (choice == 2) // Updates Record..
            
            
		{
              merge_sort(0,N_PASSENGER-1);
              
              cout << "\n\t Enter the Seat Number:";
              cin >> key
              
           k=binarySearch(passenger, 0, N_PASSENGER, key); // Search Array 
            if(k>=0) 

                
                cout << " Enter Passengers Name:"
                       cin >> stArray[i].Newname;
                         myFile << stArray[i].Newname;
                        
                        
                         for (i=0; i<N_PASSENGER; i++)
                         {
                             if (stArray[i].seat_number == key)
                                 stArray[i].name = stArray[i].Newname;
                         }

		}
#include <cstdlib>

using namespace std;

int binarySearch(struct  passenger s[], int first, int upto, int key);
void merge_sort(int,int,int);
void merge (int low, int mid, int high);
void merge_sort(int low, int high);




struct passenger
{ 
int seat_number;
string name;
string Newname;
string connecting_flight;
};
 
int N_PASSENGER;
struct passenger stArray[30];
//function binary search using the key value to serach 

int binarySearch(passenger  sorted[], int first, int upto, int key) 
{  // Sort Array if not Sorted... 
    
    while (first < upto) {
        int mid = (first + upto) / 2;  // Compute mid point.
        if (key<sorted[mid].seat_number)
        {
            upto = mid;     // repeat search in bottom half.
        } else if (key>sorted[mid].seat_number) 
        {
            first = mid + 1;  // Repeat search in top half.
        } else 
        {
            return mid;     // Found it. return position
        }
    }
    return -(first + 1);    // Failed to find key
}



/*********************************************************************************************************************/

void merge_sort(int low, int high) // Merge Sort Function
{
    int mid;
    if(low!=high)
    {
        mid =(low+high)/2;
        merge_sort(low,mid);
        merge_sort(mid+1,high);
        merge(low,mid,high);
    }
}
void merge (int low, int mid, int high)
{
    int  i,j,k ;
    passenger b[30];
    i=low;
    j=mid+1;
    k=low;
    
    
    while((i<=mid)&&(j<=high))
    {
        if(passenger[i].seat_number>=passenger[j].seat_number)
        {
            b[k++].seat_number=passenger[j++].seat_number;
            
        }
        else 
        {
            b[k++].seat_number=passenger[i++].seat_number;         
        }
        
    }
    while ( i <= mid )
        b[k++].seat_number = passenger[i++].seat_number;         
    
    while ( j <= high )
        b[k++].seat_number = passenger[j++].seat_number;       
    
    for ( i = low;i <= high;i++ )
        passenger[i].seat_number =  b[i].seat_number; 
}

Code is still not Working

Edited 5 Years Ago by jackly94: n/a

OK i trying to add this Update to a Text file but when i enter the persons name the program goes into an never ending loop

{
              fstream myFile("AirLine.text", ios::in | ios::out | ios::app );
           
           
            merge_sort(0,N_PASSENGER-1);
            
            cout << "\n\t Enter the Seat Number:";
            cin >> key;
            
            k=binarySearch(stArray, 0, N_PASSENGER, key);
            if(k>=0) 
                
                
                cout << " Enter Passengers Name:";
            cin >> stArray[i].Newname;
            
            
            
            for (i=0; i<N_PASSENGER; i++)
            {
                if (stArray[i].seat_number == key)
                    
                    stArray[i].name = stArray[i].Newname;
            }
               
            
            myFile.close();
		}

I changed the program around to


but nothing is happening. I wish to Replace the old name in a location to the Newname.

fstream myFile("AirLine.text", ios::in | ios::out | ios::app );
    
   cout << "Enter Passenger Seat Number:";
   cin >> key
          
    int location = 0;
             
   location = (stArray[i].seat_number -1)* sizeof(passenger); 
   myFile.seekp(location);
    
    
 
   cout << " Enter Passengers New Name:";
   cin >> stArray[i].Newname;
   myFile << stArray[i].Newname;
     
     
    for (i=0; i<N_PASSENGER; i++)
    {
        if (stArray[i].seat_number == key)
             
            stArray[i].name = stArray[i].Newname;
    }
    cout << "Passengers name is now " << stArray[i].Newname;
     
 
             
          
    myFile.close();
This article has been dead for over six months. Start a new discussion instead.