Im building a banking system. in my create_account it asks for account address and phone number as well as other questions. When I go to my Show account info (balance inquiry) I notice its not getting the right address as well as phone number. its showing "garbage". Im not sure whats going on If you can point me in the right direction and help out that would be appreciated.
/********************************************************************
* Vincent Dotts 09/29/2014 ch11.cpp *
* This program serves as a customer banking system *
*****************************HISTORY*********************************
* WHO DATE Discription *
*********************************************************************
* VD 09/30/2013 Created program *
********************************************************************/
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
using namespace std;
class account
{
int accno;
double phone;
char name[30], address[40];
int deposit;
char type;
public:
void create_account(); //function to get data from user
void show_account() const; //function to show data on screen
void modify(); //function to add new data
void dep(int); //function to accept amount and add to balance amount
void draw(int); //function to accept amount and subtract from balance amount
void report() const; //function to show data in tabular format
int retacno() const; //function to return account number
int retdeposit() const; //function to return balance amount
char rettype() const; //function to return type of account
//int validate(int*); //prototype for function for validation
};
void account::create_account()
{
cout << "\nEnter The account # (5 digits): ";
cin >> accno;
while (accno < 10000 || accno > 99999)
{
cout << "Invalid Account # Please reenter";
cin >> accno;
}
cout << "\n\nEnter The Name of The account Holder : ";
cin.ignore();
cin.getline(name, 30);
cout << "\n\nEnter Address of The account Holder: "<<endl;
cout << "Example: 18 First St, Oakland, CA 97836" <<endl;
cin.ignore();
cin.getline(address, 40);
cout << "\nEnter Phone Number: ";
cin >> phone;
while (phone<1000000000 || phone>9999999999)
{
cout << "Invalid Phone # Please reenter";
cin >> phone;
}
cout << "\nEnter Type of The account (C/S) : ";
cin >> type;
type = toupper(type);
cout << "\nEnter The Initial amount(>=500 for Saving and >=1000 for current ) : ";
cin >> deposit;
cout << "\n\n\nAccount Created..";
}
void account::show_account() const
{
cout << "\nAccount No. : " << accno;
cout << "\nAccount Holder Name: " << name;
cout << "\nAccount Holder Address:" << address;
cout << "\nAccount Holder Phone #:" << phone;
cout << "\nType of Account : " << type;
cout << "\nBalance amount : " << deposit;
}
void account::modify()
{
cout << "\nAccount No. : " << accno;
cout << "\nModify Account Holder Name: ";
cin.ignore();
cin.getline(name, 30);
cout << "\nModify Account Holder Address: " << endl;
cout << "Example: 18 First St, Oakland, CA 97836";
cin.ignore();
cin.getline(address, 60);
cout << "\nModify Account Holder Phone Number: ";
cin >> phone;
cout << "\nModify Type of Account: ";
cin >> type;
type = toupper(type);
cout << "\nModify Balance amount: ";
cin >> deposit;
}
void account::dep(int x)
{
deposit += x;
}
void account::draw(int x)
{
deposit -= x;
}
void account::report() const
{
cout << accno <<" " << name << " " << address<<" "<< phone << " " << type << " " << deposit << endl;
}
int account::retacno() const
{
return accno;
}
int account::retdeposit() const
{
return deposit;
}
char account::rettype() const
{
return type;
}
// function declaration
void write_account(); //function to write record in binary file
void display_sp(int); //function to display account details given by user
void modify_account(int); //function to modify record of file
void delete_account(int); //function to delete record of file
void display_all(); //function to display all account details
void deposit_withdraw(int, int); // function to desposit/withdraw amount for given account
void intro(); //introductory screen function
// THE MAIN FUNCTION OF PROGRAM
int main()
{
char ch;
int num;
intro();
do
{
system("cls");
cout << "\n\n\n\tMAIN MENU";
cout << "\n\n\t01. NEW ACCOUNT";
cout << "\n\n\t02. DEPOSIT AMOUNT";
cout << "\n\n\t03. WITHDRAW AMOUNT";
cout << "\n\n\t04. BALANCE ENQUIRY";
cout << "\n\n\t05. ALL ACCOUNT HOLDER LIST";
cout << "\n\n\t06. CLOSE AN ACCOUNT";
cout << "\n\n\t07. MODIFY AN ACCOUNT";
cout << "\n\n\t08. EXIT";
cout << "\n\n\tSelect Your Option (1-8) ";
cin >> ch;
system("cls");
switch (ch)
{
case '1':
write_account();
break;
case '2':
cout << "\n\n\tEnter The account No. : "; cin >> num;
deposit_withdraw(num, 1);
break;
case '3':
cout << "\n\n\tEnter The account No. : "; cin >> num;
deposit_withdraw(num, 2);
break;
case '4':
cout << "\n\n\tEnter The account No. : "; cin >> num;
display_sp(num);
break;
case '5':
display_all();
break;
case '6':
cout << "\n\n\tEnter The account No. : "; cin >> num;
delete_account(num);
break;
case '7':
cout << "\n\n\tEnter The account No. : "; cin >> num;
modify_account(num);
break;
case '8':
cout << "\n\n\tThank you Come Again";
break;
default:cout << "\a";
}
cin.ignore();
cin.get();
} while (ch != '8');
return 0;
}
// function to write in file
void write_account()
{
account ac;
ofstream outFile;
outFile.open("account.dat", ios::binary | ios::app);
ac.create_account();
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
outFile.close();
}
// function to read specific record from file
void display_sp(int n)
{
account ac;
bool flag = false;
ifstream inFile;
inFile.open("account.dat", ios::binary);
if (!inFile)
{
cout << "File could not be open !! Press any Key...";
return;
}
cout << "\nBALANCE DETAILS\n";
while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
if (ac.retacno() == n)
{
ac.show_account();
flag = true;
}
}
inFile.close();
if (flag == false)
cout << "\n\nAccount number does not exist";
}
// function to modify record of file
void modify_account(int n)
{
bool found = false;
account ac;
fstream File;
File.open("account.dat", ios::binary | ios::in | ios::out);
if (!File)
{
cout << "File could not be open !! Press any Key...";
return;
}
while (!File.eof() && found == false)
{
File.read(reinterpret_cast<char *> (&ac), sizeof(account));
if (ac.retacno() == n)
{
ac.show_account();
cout << "\n\nEnter The New Details of account" << endl;
ac.modify();
int pos = (-1)*static_cast<int>(sizeof(account));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char *> (&ac), sizeof(account));
cout << "\n\n\t Record Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}
// function to delete record of file
void delete_account(int n)
{
account ac;
ifstream inFile;
ofstream outFile;
inFile.open("account.dat", ios::binary);
if (!inFile)
{
cout << "File could not be open !! Press any Key...";
return;
}
outFile.open("Temp.dat", ios::binary);
inFile.seekg(0, ios::beg);
while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
if (ac.retacno() != n)
{
outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
}
}
inFile.close();
outFile.close();
remove("account.dat");
rename("Temp.dat", "account.dat");
cout << "\n\n\tRecord Deleted ..";
}
// function to display all accounts deposit list
void display_all()
{
account ac;
ifstream inFile;
inFile.open("account.dat", ios::binary);
if (!inFile)
{
cout << "File could not be open !! Press any Key...";
return;
}
cout << "\n\n\t\tACCOUNT HOLDER LIST\n\n";
cout << "*********************************************************************\n";
cout << "A/c# NAME Address Phone # Type Balance\n";
cout << "*********************************************************************\n";
while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
{
ac.report();
}
inFile.close();
}
// function to deposit and withdraw amounts
void deposit_withdraw(int n, int option)
{
int amt;
bool found = false;
account ac;
fstream File;
File.open("account.dat", ios::binary | ios::in | ios::out);
if (!File)
{
cout << "File could not be open !! Press any Key...";
return;
}
while (!File.eof() && found == false)
{
File.read(reinterpret_cast<char *> (&ac), sizeof(account));
if (ac.retacno() == n)
{
ac.show_account();
if (option == 1)
{
cout << "\n\n\tTO DEPOSITE AMOUNT ";
cout << "\n\nEnter The amount to be deposited";
cin >> amt;
ac.dep(amt);
}
if (option == 2)
{
cout << "\n\n\tTO WITHDRAW AMOUNT ";
cout << "\n\nEnter The amount to be withdraw";
cin >> amt;
int bal = ac.retdeposit() - amt;
if ((bal<500 && ac.rettype() == 'S') || (bal<1000 && ac.rettype() == 'C'))
cout << "Insufficience balance";
else
ac.draw(amt);
}
int pos = (-1)*static_cast<int>(sizeof(ac));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char *> (&ac), sizeof(account));
cout << "\n\n\t Record Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}
// INTRODUCTION FUNCTION
void intro()
{
cout << "\n\n\n\t CUSTOMER";
cout << "\n\n\t ACCOUNT";
cout << "\n\n\t SYSTEM";
cin.get();
}
http://imgur.com/kV8w5KB
a screen shot of my input
Related
The code below was modified based on my original one to fix an error thanks to #RemyLebeau.
It woks great but a new issue have showed up where when I want to delete a student data using option 4 from the menu, after entering their ID, it says their data is deleted but its not. And at the end of the console the message "error reading data from file", under the Standard Error Stream (cerr), keeps showing up. And all my tries to fix it failed.
error_display
//Project on Student Management
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Course
{
string courseName;
string grade;
string status;
};
class Student
{
int id;
int year;
float cgpa;
vector<Course> courses;
string academic_advisor;
public:
Student();
void getData();
void showData() const;
int getID() const { return id; }
friend ostream& operator<<(ostream &out, const Student &s);
friend istream& operator>>(istream &in, Student &s);
};
void writeStr(ostream &out, const string &str)
{
size_t size = str.size();
out.write((char*)&size, sizeof(size));
if (size)
out.write(str.c_str(), size);
}
void readStr(istream &in, string &str)
{
str.clear();
size_t size;
if (in.read((char*)&size, sizeof(size)))
{
if (size > 0)
{
str.resize(size);
in.read(&str[0], size);
}
}
}
ostream& operator<<(ostream &out, const Course &c)
{
writeStr(out, c.courseName);
writeStr(out, c.grade);
writeStr(out, c.status);
return out;
}
istream& operator>>(istream &in, Course &c)
{
readStr(in, c.courseName);
readStr(in, c.grade);
readStr(in, c.status);
return in;
}
ostream& operator<<(ostream &out, const Student &s)
{
out.write((char*)&s.id, sizeof(s.id));
out.write((char*)&s.year, sizeof(s.year));
out.write((char*)&s.cgpa, sizeof(s.cgpa));
writeStr(out, s.academic_advisor);
size_t NumberOfcourses = s.courses.size();
out.write((char*)&NumberOfcourses, sizeof(NumberOfcourses));
for(int i = 0; i < NumberOfcourses; ++i)
{
if (!(out << s.courses[i]))
break;
}
return out;
}
istream& operator>>(istream &in, Student &s)
{
s.courses.clear();
in.read((char*)&s.id, sizeof(s.id));
in.read((char*)&s.year, sizeof(s.year));
in.read((char*)&s.cgpa, sizeof(s.cgpa));
readStr(in, s.academic_advisor);
size_t NumberOfcourses;
if (in.read((char*)&NumberOfcourses, sizeof(NumberOfcourses)))
{
s.courses.reserve(NumberOfcourses);
Course c;
for(size_t i = 0; i < NumberOfcourses; ++i)
{
if (in >> c)
s.courses.push_back(c);
else
break;
}
}
return in;
}
Student::Student()
{
id = 0;
year = 0;
cgpa = 0.0f;
}
void Student::getData()
{
courses.clear();
cout << "\n\nEnter Student Details......\n";
cout << "Enter ID No. : "; cin >> id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
size_t NumberOfcourses;
cin >> NumberOfcourses;
cin.ignore();
courses.reserve(NumberOfcourses);
Course c;
for(int a = 0; a < NumberOfcourses; ++a)
{
cout << "\nEnter Subject Name: ";
getline(cin, c.courseName);
cout << "Enter Grade of Subject: ";
getline(cin, c.grade);
cout << "Enter Subject Status: ";
getline(cin, c.status);
courses.push_back(c);
}
cout << "Enter student CGPA: ";
cin >> cgpa;
cin.ignore();
cout << endl;
cout << "Enter Name of Academic Advisor of Student: ";
getline(cin, academic_advisor);
}
void Student::showData() const
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(size_t t = 0; t < courses.size(); ++t)
{
cout << "\t" << courses[t].courseName << ": " << courses[t].grade << " (" << courses[t].status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
Student s;
s.getData();
ofstream fout("Students.txt", ios::binary|ios::app);
if (fout << s)
cout << "\n\nData Successfully Saved to File....\n";
else
cerr << "\n\nError Saving Data to File!\n";
}
void displayData()
{
ifstream fin("Students.txt", ios::binary);
Student s;
while (fin >> s)
{
s.showData();
}
if (fin)
cout << "\n\nData Reading from File Successfully Done....\n";
else
cerr << "\n\nError Reading Data from File!\n";
}
void searchData()
{
int n;
cout << "Enter ID Number you want to search for : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Details of ID No. " << n << " are: \n";
s.showData();
flag = true;
break;
}
}
if (fin)
{
if (!flag)
cout << "The ID No. " << n << " not found....\n\n";
cout << "\n\nData Reading from File Successfully Done....\n";
}
else
cerr << "\n\nError Reading Data from File!\n";
}
void deleteData()
{
int n;
cout << "Enter ID Number you want to delete : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
ofstream tout("TrashStud.txt", ios::binary|ios::app);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " has been deleted:\n";
s.showData();
tout << s;
flag = true;
}
else
{
fout << s;
}
}
if (fin)
{
if (fout)
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("tempStud.txt");
}
else
{
remove("Students.txt");
rename("tempStud.txt", "Students.txt");
cout << "\n\nData Successfully Deleted from File....\n";
}
}
else
{
cerr << "\n\nError Saving Data to File!\n";
}
}
else
{
cerr << "\n\nError Reading Data from File!\n";
}
}
void modifyData()
{
int n;
cout << "Enter ID Number you want to Modify : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be modified with new data:\n";
s.showData();
cout << "\n\nNow Enter the New Details....\n";
s.getData();
flag = true;
}
fout << s;
}
if (fin)
{
if (fout)
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("TempStud.txt");
}
else
{
remove("Students.txt");
rename("TempStud.txt", "Students.txt");
cout << "\n\nData Successfully Updated in File....\n";
}
}
else
{
cerr << "\n\nError Saving Data to File!\n";
}
}
else
{
cerr << "\n\nError Reading Data from File!\n";
}
}
void project()
{
int ch;
do
{
system("cls");
cout << "...............STUDENT MANAGEMENT SYSTEM..............\n";
cout << "====================SEGI University=================\n";
cout << "0. Exit from Program\n";
cout << "1. Write Data to File\n";
cout << "2. Read Data From File\n";
cout << "3. Search Data From File\n";
cout << "4. Delete Data From File\n";
cout << "5. Modify Data in File\n";
cout << "Enter your choice : ";
cin >> ch;
system("cls");
switch (ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}
while (ch != 0);
}
int main()
{
project();
}
The error is because fin ( in deleteData () function) runs till the end of file , and at the end fin stores nullptr(boolean conversion to false) that is why else statement runs.And its obvious that why the while loop breaks .
for checking if the file is open or not you can use if(fin.is_open()) for fin and similarly for fout like if(fout.is_open())
The following is a code to enter some details of students into a text file and from the main menu, the user can display them using option 2.
My problem is in the section where it should display all the "Taken Courses by student".
When I choose to display all data, the "mentioned section using the for loop only display the last value I enter when writing to the file.
How can I make it display all my entries?
The issue is under the showdata and displaydata functions.
//Project on Student Management
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
class Student
{
int id;
int year;
char grade[50];
float cgpa;
int NumberOfcourses;
char courseName[100];
char academic_advisor[100];
char status[100];
public:
void getData();
void showData();
int getID(){return id;}
}s;
void Student::getData()
{
cout<<"\n\nEnter Student Details......\n";
cout<<"Enter ID No. : "; cin>>id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
cin >> NumberOfcourses;
for(int a=1; a<=s.NumberOfcourses; a++)
{
cout<<"\nEnter Subject Name: ";
cin.ignore();
cin.getline(courseName, 100);
cout << "Enter Grade of Subject: ";
cin >> grade;
cout<<"\nEnter Subject Status: ";
cin.ignore();
cin.getline(status, 100);
}
cout << "Enter student CGPA: ";
cin >> s.cgpa;
cout << endl;
cout << "Enter Name of Academic Advisor of Student: "; cin.ignore();
cin.getline(s.academic_advisor, 100);
}
void Student::showData()
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(int t=1; t<=NumberOfcourses; t++)
{
cout << "\t" << courseName << ": " << grade << " ("<< status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
ofstream fout;
fout.open("Students.txt", ios::out|ios::app);
s.getData();
fout.write((char*)&s,sizeof(s));
fout.close();
cout<<"\n\nData Successfully Saved to File....\n";
}
void displayData()
{
ifstream fin;
fin.open("Students.txt",ios::in);
while(fin.read((char*)&s,sizeof(s)))
{
s.showData();
}
fin.close();
cout<<"\n\nData Reading from File Successfully Done....\n";
}
void searchData()
{
int n, flag=0;
ifstream fin;
fin.open("Students.txt",ios::in);
cout<<"Enter ID Number you want to search for : ";
cin>>n;
while(fin.read((char*)&s,sizeof(s)))
{
if(n==s.getID())
{
cout<<"The Details of ID No. "<<n<<" are: \n";
s.showData();
flag++;
}
}
fin.close();
if(flag==0)
cout<<"The ID No. "<<n<<" not found....\n\n";
cout<<"\n\nData Reading from File Successfully Done....\n";
}
void deleteData()
{
int n, flag=0;
ifstream fin;
ofstream fout,tout;
fin.open("Students.txt",ios::in);
fout.open("TempStud.txt",ios::out|ios::app);
tout.open("TrashStud.txt",ios::out|ios::app);
cout<<"Enter ID Number you want to delete : ";
cin>>n;
while(fin.read((char*)&s,sizeof(s)))
{
if(n==s.getID())
{
cout<<"The Following ID No. "<< n <<" has been deleted:\n";
s.showData();
tout.write((char*)&s,sizeof(s));
flag++;
}
else
{
fout.write((char*)&s,sizeof(s));
}
}
fout.close();
tout.close();
fin.close();
if(flag==0)
cout<<"The ID No. "<< n <<" not found....\n\n";
remove("Students.dat");
rename("tempStud.txt","Students.txt");
}
void modifyData()
{
int n, flag=0, pos;
fstream fio;
fio.open("Students.txt", ios::in|ios::out);
cout<<"Enter ID Number you want to Modify : ";
cin>>n;
while(fio.read((char*)&s,sizeof(s)))
{
pos=fio.tellg();
if(n==s.getID())
{
cout<<"The Following ID No. "<<n<<" will be modified with new data:\n";
s.showData();
cout<<"\n\nNow Enter the New Details....\n";
s.getData();
fio.seekg(pos-sizeof(s));
fio.write((char*)&s,sizeof(s));
flag++;
}
}
fio.close();
if(flag==0)
cout<<"The ID No. "<<n<<" not found....\n\n";
}
void project()
{
int ch;
do
{
system("cls");
cout<<"...............STUDENT MANAGEMENT SYSTEM..............\n";
cout<<"======================================================\n";
cout<<"0. Exit from Program\n";
cout<<"1. Write Data to File\n";
cout<<"2. Read Data From File\n";
cout<<"3. Search Data From File\n";
cout<<"4. Delete Data From File\n";
cout<<"5. Modify Data in File\n";
cout<<"Enter your choice : ";
cin>>ch;
system("cls");
switch(ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}while(ch);
}
int main()
{
project();
}
Console Display with problem
Your Student class can only hold 1 set of course information. Your getData() loop is overwriting the same variables over and over, that is why you only see the last course entered. You need to allocate an array (or better, use a std::vector) to hold multiple courses per Student.
There are other problems with your code as well.
Try something more like this instead:
//Project on Student Management
#include <iostream>
#include <fstream>
using namespace std;
struct Course
{
char courseName[100];
char grade[50];
char status[100];
};
class Student
{
int id;
int year;
float cgpa;
int NumberOfcourses;
Course *courses;
char academic_advisor[100];
public:
Student();
~Student();
void getData();
void showData() const;
int getID() const { return id; }
friend ostream& operator<<(ostream &out, const Student &s);
friend istream& operator>>(istream &in, Student &s);
};
ostream& operator<<(ostream &out, const Course &c)
{
out.write(c.courseName, sizeof(c.courseName));
out.write(c.grade, sizeof(c.grade));
out.write(c.status, sizeof(c.status));
return out;
}
istream& operator>>(istream &in, Course &c)
{
in.read(c.courseName, sizeof(c.courseName));
in.read(c.grade, sizeof(c.grade));
in.read(c.status, sizeof(c.status));
return in;
}
ostream& operator<<(ostream &out, const Student &s)
{
out.write((char*)&s.id, sizeof(s.id));
out.write((char*)&s.year, sizeof(s.year));
out.write((char*)&s.cgpa, sizeof(s.cgpa));
out.write(s.academic_advisor, sizeof(s.academic_advisor));
out.write((char*)&s.NumberOfcourses, sizeof(s.NumberOfcourses));
for(int i = 0; i < s.NumberOfcourses; ++i)
{
out << s.courses[i];
}
return out;
}
istream& operator>>(istream &in, Student &s)
{
delete[] s.courses;
s.courses = NULL;
s.NumberOfcourses = 0;
in.read((char*)&s.id, sizeof(s.id));
in.read((char*)&s.year, sizeof(s.year));
in.read((char*)&s.cgpa, sizeof(s.cgpa));
in.read(s.academic_advisor, sizeof(s.academic_advisor));
int NumberOfcourses;
if (in.read((char*)&NumberOfcourses, sizeof(NumberOfcourses)))
{
s.courses = new Course[NumberOfcourses];
for(int i = 0; i < NumberOfcourses; ++i)
{
if (in >> s.courses[i])
s.NumberOfcourses++;
else
break;
}
}
return in;
}
Student::Student()
{
id = 0;
year = 0;
cgpa = 0.0f;
NumberOfcourses = 0;
courses = NULL;
academic_advisor[0] = '\0';
}
Student::~Student()
{
delete[] courses;
}
void Student::getData()
{
cout << "\n\nEnter Student Details......\n";
cout << "Enter ID No. : "; cin >> id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
cin >> NumberOfcourses;
cin.ignore();
delete[] courses;
courses = new Course[NumberOfcourses];
for(int a = 0; a < NumberOfcourses; ++a)
{
cout << "\nEnter Subject Name: ";
cin.getline(courses[a].courseName, 100);
cout << "Enter Grade of Subject: ";
cin.getline(courses[a].grade, 50);
cout << "\nEnter Subject Status: ";
cin.getline(courses[a].status, 100);
}
cout << "Enter student CGPA: ";
cin >> cgpa;
cin.ignore();
cout << endl;
cout << "Enter Name of Academic Advisor of Student: ";
cin.getline(academic_advisor, 100);
}
void Student::showData() const
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(int t = 0; t < NumberOfcourses; ++t)
{
cout << "\t" << courses[t].courseName << ": " << courses[t].grade << " (" << courses[t].status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
Student s;
s.getData();
ofstream fout("Students.txt", ios::binary|ios::app);
if (fout << s)
cout << "\n\nData Successfully Saved to File....\n";
else
cerr << "\n\nError Saving Data to File!\n";
}
void displayData()
{
ifstream fin("Students.txt", ios::binary);
Student s;
while (fin >> s)
{
s.showData();
}
if (!fin)
cerr << "\n\nError Reading Data from File!\n";
else
cout << "\n\nData Reading from File Successfully Done....\n";
}
void searchData()
{
int n;
cout << "Enter ID Number you want to search for : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Details of ID No. " << n << " are: \n";
s.showData();
flag = true;
break;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else
{
if (!flag)
cout << "The ID No. " << n << " not found....\n\n";
cout << "\n\nData Reading from File Successfully Done....\n";
}
}
void deleteData()
{
int n;
cout << "Enter ID Number you want to delete : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
ofstream tout("TrashStud.txt", ios::binary|ios::app);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be deleted:\n";
s.showData();
tout << s;
flag = true;
}
else
{
fout << s;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("tempStud.txt");
}
else
{
remove("Students.txt");
rename("tempStud.txt", "Students.txt");
cout << "\n\nData Successfully Deleted from File....\n";
}
}
}
void modifyData()
{
int n;
cout << "Enter ID Number you want to Modify : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be modified with new data:\n";
s.showData();
cout << "\n\nNow Enter the New Details....\n";
s.getData();
flag = true;
}
fout << s;
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("TempStud.txt");
}
else
{
remove("Students.txt");
rename("TempStud.txt", "Students.txt");
cout << "\n\nData Successfully Updated in File....\n";
}
}
}
void project()
{
int ch;
do
{
system("cls");
cout << "...............STUDENT MANAGEMENT SYSTEM..............\n";
cout << "======================================================\n";
cout << "0. Exit from Program\n";
cout << "1. Write Data to File\n";
cout << "2. Read Data From File\n";
cout << "3. Search Data From File\n";
cout << "4. Delete Data From File\n";
cout << "5. Modify Data in File\n";
cout << "Enter your choice : ";
cin >> ch;
system("cls");
switch (ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}
while (ch != 0);
}
int main()
{
project();
}
Alternatively:
//Project on Student Management
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Course
{
string courseName;
string grade;
string status;
};
class Student
{
int id;
int year;
float cgpa;
vector<Course> courses;
string academic_advisor;
public:
Student();
void getData();
void showData() const;
int getID() const { return id; }
friend ostream& operator<<(ostream &out, const Student &s);
friend istream& operator>>(istream &in, Student &s);
};
void writeStr(ostream &out, const string &str)
{
size_t size = str.size();
out.write((char*)&size, sizeof(size));
if (size)
out.write(str.c_str(), size);
}
void readStr(istream &in, string &str)
{
str.clear();
size_t size;
if (in.read((char*)&size, sizeof(size)))
{
if (size > 0)
{
str.resize(size);
in.read(&str[0], size);
}
}
}
ostream& operator<<(ostream &out, const Course &c)
{
writeStr(out, c.courseName);
writeStr(out, c.grade);
writeStr(out, c.status);
return out;
}
istream& operator>>(istream &in, Course &c)
{
readStr(in, c.courseName);
readStr(in, c.grade);
readStr(in, c.status);
return in;
}
ostream& operator<<(ostream &out, const Student &s)
{
out.write((char*)&s.id, sizeof(s.id));
out.write((char*)&s.year, sizeof(s.year));
out.write((char*)&s.cgpa, sizeof(s.cgpa));
writeStr(out, s.academic_advisor);
size_t NumberOfcourses = s.courses.size();
out.write((char*)&NumberOfcourses, sizeof(NumberOfcourses));
for(int i = 0; i < NumberOfcourses; ++i)
{
if (!(out << s.courses[i]))
break;
}
return out;
}
istream& operator>>(istream &in, Student &s)
{
s.courses.clear();
in.read((char*)&s.id, sizeof(s.id));
in.read((char*)&s.year, sizeof(s.year));
in.read((char*)&s.cgpa, sizeof(s.cgpa));
readStr(in, s.academic_advisor);
size_t NumberOfcourses;
if (in.read((char*)&NumberOfcourses, sizeof(NumberOfcourses)))
{
s.courses.reserve(NumberOfcourses);
Course c;
for(size_t i = 0; i < NumberOfcourses; ++i)
{
if (in >> c)
s.courses.push_back(c);
else
break;
}
}
return in;
}
Student::Student()
{
id = 0;
year = 0;
cgpa = 0.0f;
}
void Student::getData()
{
courses.clear();
cout << "\n\nEnter Student Details......\n";
cout << "Enter ID No. : "; cin >> id;
cout << "Enter Intake Year of the Student: "; cin >> year;
cout << "Enter number of Taken courses: ";
size_t NumberOfcourses;
cin >> NumberOfcourses;
cin.ignore();
courses.reserve(NumberOfcourses);
Course c;
for(int a = 0; a < NumberOfcourses; ++a)
{
cout << "\nEnter Subject Name: ";
getline(cin, c.courseName);
cout << "Enter Grade of Subject: ";
getline(cin, c.grade);
cout << "\nEnter Subject Status: ";
getline(cin, c.status);
s.courses.push_back(c);
}
cout << "Enter student CGPA: ";
cin >> cgpa;
cin.ignore();
cout << endl;
cout << "Enter Name of Academic Advisor of Student: ";
getline(cin, academic_advisor);
}
void Student::showData() const
{
cout << "\n\n.......Student Details......\n";
cout << "ID No. : " << id << endl;
cout << "Intake Year : " << year << endl;
cout << "Subjects Taken in Previous Semester : " << endl;
for(size_t t = 0; t < courses.size(); ++t)
{
cout << "\t" << courses[t].courseName << ": " << courses[t].grade << " (" << courses[t].status << ") ";
cout << endl;
}
cout << "CGPA : " << cgpa << endl;
cout << "Name of academic advisor of Student : " << academic_advisor << endl;
cout << endl;
}
void addData()
{
Student s;
s.getData();
ofstream fout("Students.txt", ios::binary|ios::app);
if (fout << s)
cout << "\n\nData Successfully Saved to File....\n";
else
cerr << "\n\nError Saving Data to File!\n";
}
void displayData()
{
ifstream fin("Students.txt", ios::binary);
Student s;
while (fin >> s)
{
s.showData();
}
if (!fin)
cerr << "\n\nError Reading Data from File!\n";
else
cout << "\n\nData Reading from File Successfully Done....\n";
}
void searchData()
{
int n;
cout << "Enter ID Number you want to search for : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Details of ID No. " << n << " are: \n";
s.showData();
flag = true;
break;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else
{
if (!flag)
cout << "The ID No. " << n << " not found....\n\n";
cout << "\n\nData Reading from File Successfully Done....\n";
}
}
void deleteData()
{
int n;
cout << "Enter ID Number you want to delete : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
ofstream tout("TrashStud.txt", ios::binary|ios::app);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be deleted:\n";
s.showData();
tout << s;
flag = true;
}
else
{
fout << s;
}
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("tempStud.txt");
}
else
{
remove("Students.txt");
rename("tempStud.txt", "Students.txt");
cout << "\n\nData Successfully Deleted from File....\n";
}
}
}
void modifyData()
{
int n;
cout << "Enter ID Number you want to Modify : ";
cin >> n;
ifstream fin("Students.txt", ios::binary);
ofstream fout("TempStud.txt", ios::binary);
Student s;
bool flag = false;
while (fin >> s)
{
if (n == s.getID())
{
cout << "The Following ID No. " << n << " will be modified with new data:\n";
s.showData();
cout << "\n\nNow Enter the New Details....\n";
s.getData();
flag = true;
}
fout << s;
}
if (!fin)
{
cerr << "\n\nError Reading Data from File!\n";
}
else if (!fout)
{
cerr << "\n\nError Saving Data to File!\n";
}
else
{
fin.close();
fout.close();
if (!flag)
{
cout << "The ID No. " << n << " not found....\n\n";
remove("TempStud.txt");
}
else
{
remove("Students.txt");
rename("TempStud.txt", "Students.txt");
cout << "\n\nData Successfully Updated in File....\n";
}
}
}
void project()
{
int ch;
do
{
system("cls");
cout << "...............STUDENT MANAGEMENT SYSTEM..............\n";
cout << "======================================================\n";
cout << "0. Exit from Program\n";
cout << "1. Write Data to File\n";
cout << "2. Read Data From File\n";
cout << "3. Search Data From File\n";
cout << "4. Delete Data From File\n";
cout << "5. Modify Data in File\n";
cout << "Enter your choice : ";
cin >> ch;
system("cls");
switch (ch)
{
case 1: addData(); break;
case 2: displayData(); break;
case 3: searchData(); break;
case 4: deleteData(); break;
case 5: modifyData(); break;
}
system("pause");
}
while (ch != 0);
}
int main()
{
project();
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
me and my partner are having a tough time trying to find out how to fix the error on our code. Were engineering undergrads and programming isn't exactly our forte so please take it easy on us.
Im sorry if I'm sending the whole code, but Im quite desperate since we need this project to pass the course. Thanks.
Here is my code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <cctype>
#include <iomanip>
using namespace std;
struct baccount {
int pin;
char accholder[50];
int deposit;
void create_account();
void display_account() const;
void modify();
void depmoney(int);
void drawmoney(int);
void report() const;
int retpin() const;
int retdeposit() const;
};
void create_account()
{
struct baccount qeqe;
cout << "\nEnter Pin Number:";
cin >> qeqe.pin;
cout << "\n\nEnter your name : ";
cin.ignore();
cin.getline(qeqe.accholder, 50);
do {
cout << "\nEnter the initial amount to be stored in the account(Minimum P2000) : ";
cin >> qeqe.deposit;
} while (qeqe.deposit < 2000);
cout << "\n\n\nAccount Created..";
};
void display_account()
{
struct baccount qeqe;
cout << "\nPin Number: " << qeqe.pin;
cout << "\nAccount Holder Name : ";
cout << qeqe.accholder;
cout << "\nCurrent Balance : " << qeqe.deposit;
}
void depmoney(int x)
{
struct baccount qeqe;
qeqe.deposit += x;
}
void modify()
{
struct baccount qeqe;
cout << "\nPin Number : " << qeqe.pin;
cout << "\nModify Account Holder Name : ";
cin.ignore();
cin.getline(qeqe.accholder, 50);
cout << "\nModify Balance amount : ";
cin >> qeqe.deposit;
}
void drawmoney(int x)
{
struct baccount qeqe;
qeqe.deposit -= x;
}
void report()
{
struct baccount qeqe;
cout << qeqe.pin << setw(15) << " " << qeqe.accholder << setw(15) << " " << setw(8) << qeqe.deposit << endl;
}
int retpin()
{
struct baccount qeqe;
return qeqe.pin;
}
int retdeposit()
{
struct baccount qeqe;
return qeqe.deposit;
}
void write_account();
void display_sp(int);
void modify_account(int);
void delete_account(int);
void display_all();
void deposit_withdraw(int, int);
int main()
{
char choice;
int pnum;
do {
system("cls");
cout << "Welcome to Cutaran-Eleria Bank" << endl;
cout << "[1] Create new account" << endl;
cout << "[2] Deposit" << endl;
cout << "[3] Withraw" << endl;
cout << "[4] Show current balance" << endl;
cout << "[5] Check list of accounts" << endl;
cout << "[6] Remove an account" << endl;
cout << "[7] Edit an account" << endl;
cout << "[8] Exit Cutaran-Eleria Bank" << endl;
cout << "Please enter the command you want to do: " << endl;
cin >> choice;
system("cls");
switch (choice) {
case '1':
write_account();
break;
case '2':
cout << "\n\n\tEnter Pin Number: ";
cin >> pnum;
deposit_withdraw(pnum, 1);
break;
case '3':
cout << "\n\n\tEnter The account No. : ";
cin >> pnum;
deposit_withdraw(pnum, 2);
break;
case '4':
cout << "\n\n\tEnter The account No. : ";
cin >> pnum;
display_sp(pnum);
break;
case '5':
display_all();
break;
case '6':
cout << "\n\n\tEnter Pin Number : ";
cin >> pnum;
delete_account(pnum);
break;
case '7':
cout << "\n\n\tEnter Pin Number. : ";
cin >> pnum;
modify_account(pnum);
break;
case '8':
cout << "\n\nThank you for using Cutaran-Eleria Bank";
break;
default:
cout << "\a";
}
cin.ignore();
cin.get();
} while (choice != '8');
return 0;
}
void write_account()
{
struct baccount qeqe;
ofstream newrecord;
newrecord.open("accounts.dat", ios::binary | ios::app);
qeqe.create_account();
newrecord.write(reinterpret_cast<char*>(&qeqe), sizeof(baccount));
newrecord.close();
cout << "Your account is now saved into the system.";
}
void display_sp(int n)
{
baccount bankacc;
bool flag = false;
ifstream oldrecords;
oldrecords.open("accounts.dat", ios::binary);
if (!oldrecords) {
cout << "The account is not in the system";
return;
}
cout << "\nCurrent Balance\n";
while (oldrecords.read(reinterpret_cast<char*>(&bankacc), sizeof(baccount))) {
if (bankacc.retpin() == n) {
bankacc.display_account();
flag = true;
}
}
oldrecords.close();
}
void modify_account(int n)
{
bool found = false;
baccount acc;
fstream File;
File.open("accounts.dat", ios::binary | ios::in | ios::out);
if (!File) {
cout << "Unable to open account.";
return;
}
while (!File.eof() && found == false) {
File.read(reinterpret_cast<char*>(&acc), sizeof(baccount));
if (acc.retpin() == n) {
acc.display_account();
cout << "\n\nEnter The New Details of account" << endl;
acc.modify();
int pos = (-1) * static_cast<int>(sizeof(baccount));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
cout << "\n\n\t Record Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}
void delete_account(int n)
{
baccount acc;
ifstream oldrecords;
ofstream newrecord;
oldrecords.open("accounts.dat", ios::binary);
if (!oldrecords) {
cout << "File could not be opened!!! Press any Key...";
return;
}
newrecord.open("Temp.dat", ios::binary);
oldrecords.seekg(0, ios::beg);
while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) {
if (acc.retpin() != n) {
newrecord.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
}
}
oldrecords.close();
newrecord.close();
remove("accounts.dat");
rename("Temp.dat", "accounts.dat");
cout << "\n\nRecord Deleted ..";
}
void display_all()
{
baccount acc;
ifstream oldrecords;
oldrecords.open("accounts.dat", ios::binary);
if (!oldrecords) {
cout << "Unable to show accounts";
return;
}
cout << "\n\n\t\tList of Accounts\n\n";
cout << "Pin Number Name CurrentBalance\n";
while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) {
acc.report();
}
oldrecords.close();
}
void deposit_withdraw(int n, int option)
{
int amt;
bool found = false;
baccount acc;
fstream File;
File.open("accounts.dat", ios::binary | ios::in | ios::out);
if (!File) {
cout << "Unable to access the account.";
return;
}
while (!File.eof() && found == false) {
File.read(reinterpret_cast<char*>(&acc), sizeof(baccount));
if (acc.retpin() == n) {
acc.display_account();
if (option == 1) {
cout << "\n\nEnter The amount to be deposited";
cin >> amt;
acc.depmoney(amt);
}
if (option == 2) {
cout << "\n\nEnter The amount to be withdrawn";
cin >> amt;
int bal = acc.retdeposit() - amt;
if (amt > acc.retdeposit())
cout << "Insufficient balance";
else
acc.drawmoney(amt);
}
int pos = (-1) * static_cast<int>(sizeof(acc));
File.seekp(pos, ios::cur);
File.write(reinterpret_cast<char*>(&acc), sizeof(baccount));
cout << "\n\nRecord Updated";
found = true;
}
}
File.close();
if (found == false)
cout << "\n\n Record Not Found ";
}
You are getting undefined reference to becouse you havent function definition (in this case).
It means that linker cannot find function's (which you are trying to call) definition.
For example, what you have to do, is:
Outside of struct
void baccount::display_account() const
{
std::cout << "Now you can call me!";
}
Or right in the struct
struct baccount
{
int pin;
void display_account() const
{
std::cout << "Now you can call me!";
}
.
.
.
};
"undefined reference to ..." Is a linker error, not a compiler error. It means that you forgot to link something (library, object file) that defines whatever type the error is complaining about.
Add the missing object file or library to your linker commandline and you should be good.
Do note that, depending on toolchain, the order in which you link things may be significant. Always order your link so that objects depending on types are mentioned before the objects that implement them.
I am creating a program that will write and read the content of a binary file. The part I am struggling with is reading/writing pointsEarned and studentGPA as a double. It prints out 6.36599e-314 instead of the number I provided. I looked around the web for a solution but couldn't find one, so here I am.
class student {
int studentID;
char name[30];
char lastName[30];
double pointsEarned;
double studentGPA;
public:
void getData() {
cout << "\nEnter the Student ID: ";
cin >> studentID;
cout << "\n\nEnter the first name of the student: ";
cin >> name;
cout << "\n\nEnter the last name of the student: ";
cin >> lastName;
cout << "\n\nEnter the amount of points earned by the student: ";
cin >> pointsEarned;
cout << "\n\nEnter the Student GPA: ";
cin >> studentGPA;
}
void showData() {
cout << "\nStudent ID: " << studentID;
cout << "\nStudent Name: " << name << " " << lastName;
cout << "\nPoints: " << pointsEarned;
cout << "\nGPA: " << studentGPA << "\n";
}
int studentNumber() {
return studentID;
}
};
//input student info into the dat file
void write() {
student object;
student val;
ofstream file2;
file2.open("student.dat", ios::binary | ios::app);
object.getData();
file2.write((char *)&object, sizeof(object));
file2.close();
}
void display() {
student object;
ifstream file1;
file1.open("student.dat", ios::binary);
if (!file1) {
cout << "File not Found!\n";
}
else {
//read data
while (file1.read((char *)&object, sizeof(object))) {
object.showData();
}
}
file1.close();
}
//display student information through their studentID
void search(int n) {
student object;
ifstream file1;
file1.open("student.dat", ios::binary);
if (!file1) {
cout << "File not Found!\n";
}
else {
while (file1.read((char *)&object, sizeof(object))) {
if (object.studentNumber() == n)
object.showData();
//validation
if (object.studentNumber() != n) {
cout << "StudentID doesn't exist\n";
break;
}
}
}
file1.close();
}
//delete student
void deleteRecord(int n) {
student object;
//open both files
ifstream file1;
file1.open("student.dat", ios::binary);
ofstream file2;
file2.open("Temp.dat", ios::out|ios::binary);
//write contents of f1 to f2
while (file1.read((char*)&object, sizeof(object))) {
if (object.studentNumber() != n)
file2.write((char*)&object, sizeof(object));
}
file1.close();
file2.close();
//delete original file and rename the new one
remove("student.dat");
rename("Temp.dat", "student.dat");
}
void editRecord(int n) {
fstream fp;
student object;
int found = 0;
fp.open("student.dat", ios::in | ios::out);
while (fp.read((char *)&object, sizeof(object)) && found == 0) {
if (object.studentNumber() == n) {
object.showData();
cout << "\nEnter The New Details of student";
object.getData();
int pos = -1 * sizeof(object);
fp.seekp(pos, ios::cur);
fp.write((char*)&object, sizeof(object));
found = 1;
}
//validation
if (object.studentNumber() != n) {
cout << "StudentID doesn't exist\n";
break;
}
}
fp.close();
}
int main() {
int n;
int studentIDNumber;
cout << " Press 1 if you choose to input student information\n Press 2 to display all student's information\n Press 3 to display a specific student's information\n Press 4 to modify student information\n Press 5 to delete a student\n";
cin >> n;
if (n == 1) {
write();
} else if(n == 2){
display();
}
else if (n == 3) {
cout << "Enter the Student ID to get the student information: ";
cin >> studentIDNumber;
search(studentIDNumber);
}
else if (n == 4) {
cout << "Enter the Student ID to modify the student information: ";
cin >> studentIDNumber;
editRecord(studentIDNumber);
}
else if (n == 5) {
deleteRecord(studentIDNumber);
cout << "The record has been deleted.\n";
}
else {
cout << "Error. The number is neither 1 or 2. Please try again. \n";
}
return 0;
}
The output:
Student ID: 55
Student Name: John Doe
Points: 6.36599e-314
GPA: 7.27464e+199
Press any key to continue . . .
I have got an assignment project to make a program using C++ which maintains a list of Students (their name, age, and GPA) in a text file. The program has the following functionality:
Insertion of record
Deletion of record
Searching of record
Updating a record
When deleting a record my code takes a string name as input and removes it from the text file. However the next two lines in the file (age & gpa for that student) are left. How do I remove those too?
Following is the code for my program.
#include <iostream>
#include <fstream>
using namespace std;
void writing();
void deleting();
void searching();
class student {
public:
int age = 0;
string name;
void SetGpa(float x)
{
gpa = x;
}
float GetGpa()
{
return gpa;
}
private:
float gpa = 0.0;
};
int main()
{
int opt;
cout << "Please Enter an option number to continue:\n ";
cout << "\nPress 1 for New Record insertion";
cout << "\nPress 2 for Record Deletion";
cout << "\nPress 3 for Searching a Record";
cout << "\nPress 4 for Updating a Record";
cout << "\nEnter option Number: ";
cin >> opt;
switch (opt)
{
case 1:
{
writing();
break;
}
case 2:
{
deleting();
break;
}
case 3:
{
searching();
break;
}
case 4:
{
deleting();
writing();
cout << "Record has been updated! ";
break;
}
}
}
void writing()
{
float a;
student moiz;
cout << "Please enter name of student: ";
cin >> moiz.name;
cout << "Please enter the age of student: ";
cin >> moiz.age;
cout << "Pleae enter the Gpa of student: ";
cin >> a;
moiz.SetGpa(a);
ofstream myfile;
myfile.open("record.txt", ios::app | ios::out);
myfile << endl << moiz.name << endl;
myfile << moiz.age << endl;
myfile << moiz.GetGpa();
myfile.close();
}
void deleting()
{
string line, name;
cout << "Please Enter the name of record you want to delete: ";
cin >> name;
ifstream myfile;
ofstream temp;
myfile.open("record.txt");
temp.open("temp.txt");
while (getline(myfile, line))
{
if (line != name)
temp << line << endl;
}
cout << "The record with the name " << name << " has been deleted if it exsisted" << endl;
myfile.close();
temp.close();
remove("record.txt");
rename("temp.txt", "record.txt");
}
void searching()
{
ifstream fileInput;
fileInput.open("record.txt");
string line, search;
cout << "Please enter the term to search: ";
cin >> search;
for (unsigned int curLine = 0; getline(fileInput, line); curLine++)
{
if (line.find(search) != string::npos)
{
cout << "found: " << search << " on line: " << curLine << endl;
}
else
{
cout << "Error! Not found on Line" << curLine << endl;
}
}
}
You can add an else clause to your statement checking the name, and introduce a counter of how many of the following lines should be skipped after name was found:
int skip = 0;
while (getline(myfile, line)) {
if ((line != name) && !(skip > 0)) {
temp << line << endl;
}
else {
if(skip == 0) {
skip = 2; // Skip the next two lines also
}
else {
--skip;
}
}
}