C++ saving records to file - c++

i've been thinking how to do this "save to file" thing all night, but it seems that luck wasnt on my side.. .
I am new to this, and i want to get learned, thats all.
I am from Bulgaria, and for those who wonder what is "edinen", this is your citizen number as a member of that country. (I still dont know how to explain this...)
Here is what I've got
(I've already included the "fstream", but i still dont know how to use it!
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define n 30
int num=0;
struct uslugi
{
char name[30];
char surname[30];
char lastname[30];
char illness[30];
long int edinen;
}grupa[n];
void add_record();
void show_record();
void search_record();
void remove_record();
void add_record() // FUNCTION - ADD RECORD(S)
{
system("title Add Record");
int br;
cout<<"\n How many pacients do you want to enter?";
cout<<"\n >> ";
cin>>br;
for(int i=num;i<num+br;i++)
{
cout<<"\n \t\t ENTERING DATA FOR PACIENT NUMBER - "<<i+1<<endl;
cout<<"\n Name:";
cout<<"\n >> ";
cin>>grupa[i].Name;
cout<<"\n Surname:";
cout<<"\n >> ";
cin>>grupa[i].surname;
cout<<"\n Lastname:";
cout<<"\n >> ";
cin>>grupa[i].lastname;
cout<<"\n edinen:";
cout<<"\n >> ";
cin>>grupa[i].edinen;
cout<<"\n Ill from:";
cout<<"\n >> ";
cin>>grupa[i].illness;
}
num=num+br;
}
void show_record() // FUNCTION - SHOW RECORD(S)
{
if (num==0)
{
cout<<"\t\t by far there are no pacient at all \n";
}
cout<<"\n \t\t\t list with all pacients"<<num<<endl;
for(int i=0;i<num;i++)
{
cout<<"\n Name:";
cout<<"\n >> "<<grupa[i].name;
cout<<"\n Surname:";
cout<<"\n >> "<<grupa[i].surname;
cout<<"\n Lastname:";
cout<<"\n >> "<<grupa[i].lastname;
cout<<"\n edinen:";
cout<<"\n >> "<<grupa[i].edinen;
cout<<"\n Ill from:";
cout<<"\n >> "<<grupa[i].illness;
cout<<"\n\n";
}
}
void remove_record() // FUNCTION - DELETE RECORD(S)
{
int k,index;
for(int i=0;i<num;i++)
{
cout<<"\n("<<i+1<<") "<<endl;
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
}
cout<<"Who do you want to delete? \n >> ";
cin>>index;
for(k=0;k<num;k++)
{
if(k>=index)
{
grupa[k].edinen=grupa[k+1].edinen;
}
if(k==num-1)
break;
else
cout<<"BY FAR THERE IS NO DATA HERE"<<endl;
}
num--;
for (i=0;i<num;i++)
{
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
}
}
void print_count() // FUNCTION - PRINTING
{
cout<<"\n \t\t\t NUMBER OF PACIENTS - "<<num<<endl;
cout<<""<<endl;
}
void search_record() // FUNCTION - SEARCHING
{
int flag=0;
long int tempegn;
cout<<"\n Type the edinen for the pacient you search: ";
cout<<"\n >> ";
cin>>tempegn;
for (int i=0;i<n;i++)
if(tempegn==grupa[i].edinen)
{
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
flag++;
}
if (!flag)
{
cout<<"\n\t PACIENT WITH THAT NAME DOESNT EXCIST \n\n";
}
}
void main() // MAIN FUNCTION (MENU)
{
int choice;
do
{
cout<<"\n\t\t******************* Menu *******************"<<endl;
cout<<"\t\t* *";
cout<<"\n\t\t* 1.Add new pacient *";
cout<<"\n\t\t* 2.Search for a pacient by edinen *";
cout<<"\n\t\t* 3.Delete pacient *";
cout<<"\n\t\t* 4.List with all pacients *";
cout<<"\n\t\t* 5.Exit *";
cout<<"\n\t\t* *\n";
cout<<"\t\t************************************************"<<endl;
cout<<"\n Type your choice! ";
cout<<"\n >> ";
cin>>choice;
switch(choice)
{
case 1:{print_count();add_record();break;}
case 2:{print_count();search_record();break;}
case 3:{print_count();remove_record();break;}
case 4:{print_count();show_record();}
}
}
while(choice!=5);
}

I would suggest you read a book, or at least some documentation about this. Also, to understand how this kind of input works, it is required knowledge of oop concepts. As far as I noticed you do not have such a knowledge, I would suggest you stick for a while with procedural i/o functions from C, like fprintf. A google search will show many resources about them and they are pretty easy to understand, at least compared with those from C++, like the ones you are using.
To answer to your question, I will try to explain how to achive what you want. Note that this is a very simplified explanation and not 100% accurate.
Firstly, you need to open the file and create a i/o stream, using ifstream for input files or ofstrsam for output files:
ifstream f("input_filename");
ofstream g("output_filename);
After you created f and/or g (they are not both requied. Also, f and g are just random names) you can use them in the same way you use cin and cout, in this case, as f is for input, it should be used like cin.

Related

How can I align the output program correctly? Once there was a 2 digit number, the alignment is being disoriented

It is a program that takes information then saves it to a marklist file and then outputs it again and then the output is not properly aligned to the rows above. How can I align the output program correctly? Once there was a 2 digit number, the alignment is being disoriented.Notice that the output is not properly aligned
#include<iostream>
//fstream is needed because we will use file systems.
#include<fstream>
using namespace std;
/*declaring fstream global variable used
to open, write, read and close files in this program.*/
fstream storage;
struct student_profile{
char FName[100];
char LName[100];
char reg_year[100];
char course[100];
char id[100];
int no_courses;
char course_Name[100];
char course_code[100];
int credit_hour;
int quiz_one;
int quiz_two;
int quiz_three;
int mid;
int assignment;
int lab;
int final_exam;
};
/* creating structure variable from student_profile.
the name this structure variable is student.
it is used to hold name, last name, id etc
*/
student_profile student;
/* function used to register student.
it accept student details then it write those data to file.
so student details will be saved in text file for later use.
*/
void function_to_register(){
cout<<" Write First Name of student: ";
cin>>student.FName;
cout<<" Write Last Name of student: ";
cin>>student.LName;
cout<<" Write Registration year: ";
cin>>student.reg_year;
cout<<" Write Course of student: ";
cin>>student.course;
cout<<" Write number of courses that student learns in this semester and register them one by one\n";
cout<<" Number of courses: ";
cin>> student.no_courses;
/* for loop used to register multiple courses for one student
based on the student's number of courses entered.
*/
for(int a=1; a<=student.no_courses; a++) {
cout<<" Write course Name: ";
cin>>student.course_Name;
cout<<" Enter grade for quiz one: ";
cin>>student.quiz_one;
cout<<" Enter grade for quiz two: ";
cin>>student.quiz_two;
cout<<" Enter grade for quiz three: ";
cin>>student.quiz_three;
cout<<" Enter midterm grade: ";
cin>>student.mid;
cout<<" Enter assignment grade: ";
cin>>student.assignment;
cout<<" Enter lab grade: ";
cin>>student.lab;
cout<<" Enter final exam grade: ";
cin>>student.final_exam;
storage.open("marklist.txt",ios::app) ;
storage.write((char*)&student, sizeof(student));
//storage.write(student,sizeof(student));
storage.close();
}
}
/* function used to calculate grade of each course.
we call this function when the program is displaying grade of student.
*/
void grade_calculator(int totalMark) {
if(totalMark>=90 && totalMark<=100)
cout<<"A+";
else if(totalMark>=80 && totalMark<90)
cout<<"A";
else if(totalMark>=70 && totalMark<80)
cout<<"B";
else if(totalMark>=60 && totalMark<70)
cout<<"C+";
else if(totalMark>=50 && totalMark<60)
cout<<"D";
else if(totalMark>=0 && totalMark<50)
cout<<"F";
else
cout<<"NG";
}
/* function used to display registered students on the screen.
this function gets the registered student from that file
we used above to store student details.
*/
void seeRegistered() {
storage.open("marklist.txt",ios::app);
storage.close();
storage.open("marklist.txt",ios::in);
storage.read((char*)&student, sizeof(student));
while (storage.eof()==0) {
cout<<"\n\n\n";
cout<<" Full Name: "<<student.FName<<" "<<student.LName<<endl;
cout<<" Registration Year: "<<student.reg_year<<endl;
cout<<" Faculty: "<<student.course<<endl;
cout<<" *****************************************************************************\n";
cout<<" ******************************STUDENT MARK LIST******************************\n";
cout<<" *****************************************************************************\n";
cout<<" Subject Quiz1 Quiz2 Quiz3 Mid Assignment Final Total Grade\n";
cout<<" _____________________________________________________________________________\n";
/* for loop used to print all courses and their marks
for a single student.
*/
for(int t=1; t<=student.no_courses; t++) {
int totalMark;
totalMark= (student.quiz_one+student.quiz_two+student.quiz_three+student.assignment+student.mid+student.final_exam)/6;
cout<<" "<<student.course_Name<<""<<student.quiz_one<<" "<<student.quiz_two<<" "<<student.quiz_three<<" "<<student.mid<<" "<<student.assignment<<" "<<student.final_exam<<" "<<totalMark<<" ";
grade_calculator(totalMark);
cout<<endl;
storage.read((char*)&student,sizeof(student));
}
}
storage.close();
}
/* This is the main function which will be executed before all functions,
So in this program when the user select one option then by
the help switch statement the user redirects to appropriate function.
*/
int main() {
char select;
/* while loop that help us to excute the program all time.
this means the program will not exit without our interest.*/
while(1) {
cout<<endl; cout<<endl; cout<<endl;
cout<<endl;
cout<<" **********************************************\n";
cout<<" * STUDENT REGISTRATION AND MARK LIST SYSTEM *\n";
cout<<" **********************************************\n";
cout<<" *\n";
cout<<" Enter 1 To Register New student \n";
cout<<" Enter 2 To Show Registerd Students and Their Grade \n";
/*
you can improve this in which the displayed record is sorted (ascending/descending)
*/
cout<<" Enter 3 To Search for a student \n";
cout<<" Enter 4 To Edit a student record\n";
cout<<" Enter 5 To Delete a student record\n";
cout<<" Enter 0 To Exit \n";
cout<<" : ";
cin>>select;
switch (select) {
case '1':
function_to_register();
cout<<" Student Registered succsesfully \n";
break;
case '2':
seeRegistered();
break;
}
}
}
use std::cout<<setw(num_of_spaces)

unable to write to/read from file

The code I wrote seems to work when i write data in the file but when i look into the record.dat file after writing into it, it shows nothing. The segment with "Main Menu" gets repeated from the switch() every time i try to read from the file. I had to write the open commands with the access modes separately inside each case otherwise it wouldn't even open the file.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>
using namespace std;
class employee
{
private:
int empcode;
char empname[30];
char empdesig[15];
float empsalary;
public:
void add_rec()
{
cout<<"\nEmployee Code : ";
cin>>empcode;
cout<<"\nEmployee Name : ";
cin.ignore();
cin.getline(empname,30);
cout<<"\nEmployee Designation : ";
cin.getline(empdesig,15);
cout<<"\nEmployee Salary : ";
cin>>empsalary;
}
void read_rec()
{
cout<<"\nEmployee Code : "<<empcode;
cout<<"\nEmployee Name : "<<empname;
cout<<"\nEmployee Designation : "<<empdesig;
cout<<"\nEmployee Salary : "<<empsalary;
}
};
int main()
{
employee emp;
fstream rfile;
int ch,rec_no=0,pos=0;
char ans='y',opt;
do
{
cout<<"\n";
cout<<"\t\t\t MAIN MENU ";
cout<<"\n1. Add Record ";
cout<<"\n2. Read Record ";
cout<<"\n3. Modify Record ";
cout<<"\n4. Exit "<<endl;
cout<<"\nSelect an option : ";
cin>>ch;
switch (ch)
{
case 1:
{
rfile.open("record.dat", ios::out);
char opt='y';
cout<<"\t\t\tEmployee Data Entry "<<endl;
do
{
emp.add_rec();
rfile.write((char*)&emp,sizeof(emp));
cout<<"\nEnter another record ? {Y/N} ";
cin>>opt;
} while(opt=='y'||opt=='Y');
}
break;
case 2:
{
rfile.open("record.dat", ios::in);
cout<<"\t\t\tEmployee Data Display "<<endl;
rfile.read((char*)&emp,sizeof(emp));
while(rfile)
{
emp.read_rec();
rfile.read((char*)&emp,sizeof(emp));
}
}
break;
case 3:
{
rfile.open("record.dat", ios::out);
cout<<"\t\t\tEmployee Data Modify "<<endl;
cout<<"\nEnter the record no. to modify : ";
cin>>rec_no;
pos=(rec_no-1)*sizeof(emp);
rfile.seekg(pos,ios::beg);
rfile.read((char*)&emp,sizeof(emp));
cout<<"\nModify this record ? {Y/N} "<<endl;
cin>>opt;
if(opt=='y'||opt=='Y')
{
cout<<"\n";
cout<<"\t\t\tEnter New Data "<<endl;
emp.add_rec();
rfile.write((char*)&emp,sizeof(emp));
cout<<"\nRecord Modified"<<endl;
cout<<"\nPress any key to continue...";
getch();
}
}
break;
case 4:
break;
default:
cout<<"\nPlease select a valid option!";
break;
}
} while(ch!=4);
rfile.close();
return(0);
}
Each time you open a file for writing under a case statement, you also need to close the file. When the file is closed, the double buffered values are then written to the physical file.
Under case 1, add a file close after the while statement:
} while(opt=='y'||opt=='Y');
rfile.close();
Make similar updates to case 2 and 3, and both case 1 and 2 will now work. Case 3 has additional issues. For example, when opening the file to modify, you will want to use the "app" bit mask so you do not delete existing contents. See the following for more information:
https://en.cppreference.com/w/cpp/io/basic_fstream/open

Program compiles but I think switch is ignored.

I am currently studying c++ but I fell behind a little bit, so I apologize if my question is obvious.
I have to create a program that asks for a student's name, GPA, Year of admission, and get a random 5 digit number generated for that person. The number of students will not exceed 42.
My program compiled (somehow) and I am able to get the error for invalid menu selection, however, whenever I give a valid selection (currently 1) nothing happens.
Maybe I am missing something, this is why I need help.
Here is my code.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//print all the menu options
void print_menu()
{
cout<<"\nRCNJ Registrar Menu:"<<"\n"
<<"\n"
<<"[1] Add a student"<<"\n"
<<"[2] Display all students"<<"\n"
<<"[3] Display by year"<<"\n"
<<"[4] Display statistics"<<"\n"
<<"[5] Quit"<<"\n";
}
//get and return the student's name
void get_name(string& student_name) //call student_name after that.
{
cout<<"Please enter the sudent's name: ";
cin >> student_name;
cout<<"\n";
}
//validate and return gpa
double get_gpa()
{
double student_gpa = 0;
cout<<"Please enter the GPA: ";
cin >>student_gpa;
cout<<"\n";
while (student_gpa > 4 || student_gpa < 0)
{
cout<<"Please enter a valid GPA for the student (0.00 - 4.00): ";
cin >> student_gpa;
cout<<"\n";
}
return student_gpa;
}
//validateand return year
int get_year()
{
int student_year = 0;
cout<<"Please enter the year: ";
cin >> student_year;
cout<<"\n";
while (student_year >2016 || student_year <1972)
{
cout<<"Please enter a valid year (min 1972, max 2016): ";
cin >> student_year;
cout<<"\n";
}
return student_year;
}
//generate the student's R#
int generate_number()
{
int r_number;
srand (time(NULL));
r_number = rand() % 89999 + 10000;
return r_number;
}
//save info. Include get_name, get_gpa, get_year
void input_new_student()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int s_name, s_gpa, s_year, r_num;
get_name(student_name);
get_gpa();
get_year();
generate_number();
}
//display all students in the proper format
void print_all()
{
}
//get a year as selection and print all students that are the same year
void print_by_year()
{
}
//display statistics based on entered students
void print_statistics()
{
}
//validate and return the menu option selected by the user.
//it should call print_menu defined earlier
int get_selection(int menu_choice)
{
menu_choice = 0;
cout<<"\n"
<<"Selection: ";
cin >> menu_choice;
cout<<"\n";
while (menu_choice > 5 || menu_choice< 1)
{
cout<<" Menu choice is invalid. Please re-enter (1 - 5): ";
cin>> menu_choice;
cout<<"\n";
}
return menu_choice;
}
int main()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int menu_choice;
int s_name=0;
int s_gpa=0;
int s_year=0;
int r_num=0;
string nameArray[42];
s_name++;
double gpaArray[42];
s_gpa++;
int yearArray[42];
s_year++;
int ramapoArray[42];
r_num++;
print_menu();
get_selection(menu_choice);
switch (menu_choice)
{
case 1:
input_new_student();
nameArray[s_name] = student_name;
gpaArray[s_gpa] = student_gpa;
yearArray[s_year] = student_year;
ramapoArray[r_num] = r_number;
break;
}
return 0;
}
I dont have permission to comment, hence adding it here.
In you main(),
get_selection(menu_choice);
switch (menu_choice)
You return menu_choice, but there is none to take the value, you end you using garbage value as it is uninitialized.
So two ways you can do it, either by passing the address/reference of menu_choice or by return value. try either of these it should work, though I have not gone through the rest of your program.
As suggested by others, try a debugger e.g. gdb?

Program on cricket scoreboard

Why is the following code not giving results and how to get results?
Whenever I run the code, it first asks for the names of the players of two teams playing the match, then it shows the menu from which if we select any one of the option it again asks for the batsman name which is not according to the program designed. My research on the code and the problem is that I think buffer memory is full but I don't know how to free it, any help would be beneficial. Thank you
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class scorecard{
char batname[11][20];
int runscored[11];
char situation[11][10];
char mode[11][15];
char bowlername[11][20];
float oversplayed[11];
int maiden[11];
int runsgiven[11];
int wicketstaken[11];
public:
void updatebatsman(void);
void updatebowler(void);
void displaybat(void);
void displaybowl(void);
void menu(void);
scorecard()
{for(int n=0;n<12;n++)
{
runscored[n]={0};
oversplayed[n]={0};
maiden[n]={0};
runsgiven[n]={0};
wicketstaken[n]={0};
}
}
};
int main()
{
int jb=0;
scorecard s1;
int kb;
s1.menu();
do
{
cout<< "Enter the option"<<endl;
cout<<"(1) Display batting score"<<endl<<"(2) Display Bowling score"<<endl<<"(3) Update batting score"<<endl;
cout<<"(4) Update Bowling score"<<endl;
cin >>kb;
switch(kb)
{
case 1 : s1.displaybat();
break;
case 2 :s1.displaybowl();break;
case 3:s1.updatebatsman();break;
case 4:s1.updatebowler();break;
default:cout<<"Wrong choice";
}
}while (jb<1);
}
void scorecard::updatebowler(void)
{char bowlname[20];
int str,k,option,overnumbers,maidenumb,uprun,upwicket;
cout<<"Enter Bowler name:";
cin.getline(bowlname,20);
for( k=0;k<11;k++)
{str= strcmp(bowlername[k],bowlname);
if (str== 0)
{
cout<<"Menu for Bowler information update "<<endl;
cout<<"(1) Update Number of overs"<<endl<<"(2) Update maiden overs"<<endl<<"(3) Update runs given"<<endl;
cout<<"(4) Update wickets taken"<<endl;
cin >> option;
switch(option)
{
case 1:{cout<<"Enter Numbers of overs to be updated:";
cin >>overnumbers;
cout<<endl;
oversplayed[k]+=overnumbers;
break;
}
case 2:{cout <<"Enter the number of maiden overs to be updated:";
cin>>maidenumb;
cout<<endl;
maiden[k]+=maidenumb;
break;
}
case 3:{cout <<"Enter the number of runs to be added:";
cin>>uprun;
cout<<endl;
runsgiven[k]+=uprun;
break;
}
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
}
default:cout<<"wroung choice";
}
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::updatebatsman(void)
{char batsmaname[20];
int str,k;
cout<<"Enter Batsman name:";
cin.getline(batsmaname,20);
for( k=0;k<11;k++)
{str= strcmp(batname[k],batsmaname);
if (str== 0)
{
cout<<"enter runs scored:";
cin>>runscored[k];
cout<<endl<<"enter weather out or not out:";
cin>>situation[k];
cout<<endl<<"enter mode(if batsman out) by which batsman was out:";
cin>>mode[k];
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::displaybat(void)
{
cout << "Batsman name"<<'t'<<"Runs scored"<<'t'<<"situation"<<'t'<<"mode"<<endl;
for(int j=0;j++;j<12)
{
cout<<batname[j]<<'t'<<runscored[j]<<'t'<<situation[j]<<'t'<<mode[j]<<endl;
}
}
void scorecard::displaybowl(void)
{
cout << "Bowler name"<<'t'<<"overs played"<<'t'<<"maiden overs"<<'t'<<"wicket taken"<<'t'<<"Runs given"<<endl;
cout<<endl;
for(int j=0;j++;j<12)
{
cout<<bowlername[j]<<'t'<<oversplayed[j]<<'t'<<maiden[j]<<'t'<<wicketstaken[j]<<'t'<<runsgiven[j]<<endl;
}
}
void scorecard::menu(void)
{
cout<<"Enter the name of players of batting team"<<endl;
for (int k=0;k<11;k++)
{
cout <<"Enter name of player "<<k+1<<":";
cin>>batname[k];
}
cout <<"Enter the name of players of bowling team"<<endl;
for (int n=0;n<11;n++)
{
cout <<"Enter name of player "<<n+1<<":";
cin>>bowlername[n];
}
}
This is very wrong:
for(int j=0;j++;j<12)
It should be:
for(int j=0; j < 11; j++)
You are also missing a break in your case 4 statement for the options:
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
break;
}
default:cout<<"wroung choice";
Without the break you will see also the output wrong choice when the user selects option 4.

Data file handling modify function error c++

class flight //main class flight which the user uses to book tickets
{
int booking_id;
int pnr,p_age;
char p_name[25],d_a_name[25],a_a_name[25],gender,departing_date[10],arrival_date[10],b_id;
long double price;
public:
flight()
{
static int id=0;
booking_id=id++;
}
void modfunction(int n);
};
void flight::modfunction(int n) //function for entering new values when the user has chosen to modify
{
getchar();
cout<<"Enter the passenger's name :";
gets(p_name);
cout<<"Enter the passenger age :";
cin>>p_age;
getchar();
cout<<"Enter the passenger's gender :";
cin>>gender;
getchar();
cout<<"Enter the departing date(dd-mm-yyyy) :";
gets(departing_date);
cout<<"Do you want to book return ticket with a 10% discount?(y/n) :";
cin>>return_ticket_input;
if(return_ticket_input=='y')
{
cout<<"Enter the arrival date :";
gets(arrival_date);
}
cout<<"Choose the airline.\n\n";
flights(departing_date,return_ticket_input,arrival_date);
cout<<"\n\nEnter the desired flight number :";
cin>>selected_fno;
}
void modify_ticket()//function for modifying a ticket
{
int n;
system("clear");
cout<<"Enter the booking id for modification ";
cin>>n;
flight f;
fstream fp("flight.dat",ios::binary);
ifstream ifile("flight.dat",ios::binary);
int found=0;
int count_variable = 0;
while(ifile.read((char*)&f,sizeof(f)))
{
if(n==f.retbid())
{
f.output();
f.modfunction(n);
// int s=sizeof(f) * (count_variable + 1);
fp.seekp(ifile.tellg());
fp.write((char*)&f,sizeof(f));
found = 1;
f.output();
getchar();
}
count_variable++;
}
ifile.close();
fp.close();
if(found==0)
{
cout<<"Passenger not founed!";
}
cout<<"Press any key to continue.";
getch();
}
So this is the minimal version of my problem.
The class named flight. a function called modfuction which is used to input data from the user. and another function called modify ticket used by the user to modify the record details.
The problem is even though the user enters the booking id(n) and it outputs the record, when the user tries to update it(modify) it stays the same!
please help me as this is a part of my school project!
thanks in advance!
Instead of fstream fp("flight.dat",ios::binary); write:
fstream fp("flight.dat",ios::binary|ios::in|ios::out);
P.S.: Encountered Same Problem A minute ago..