Converting the binary data read from a file to a char ...

Unhandled Exception Error in code

Hey all, I've been learning to program for a few months now and I've been working on a program to manage student data. Here's the code:
#include  #include  #include  #include  #include  using namespace std; class Student { private: char name[20]; string course; int section; int grade; public: void calcgrade(); void getData(); void showData(); }; void Student::calcgrade() { if (grade >= 90) grade = 'A'; else if (grade >= 80) grade = 'B'; else if (grade >= 70) grade = 'C'; else if (grade >= 60) grade = 'D'; else grade = 'F'; } void Student::getData() { cout << "Enter the name of the student: "; cin.ignore(); getline(cin, name); cout << "Enter the course: "; cin >> course; cout << "Enter the section: "; cin >> section; cout << "Enter the grade received: "; cin >> grade; calcgrade(); } void Student::showData() { cout << ".......Student Information......" << endl; cout << "Student Name: " << name << endl; cout << "Course: " << course << endl; cout << "Section: " << section << endl; cout << "Grade: " << grade << endl; cout << endl; } void addData() { Student st; ofstream fout; fout.open("Student.data", ios::binary | ios::out | ios::app); st.getData(); fout.write((char*)& st, sizeof(st)); fout.close(); cout << "Data Successfully Saved to File." << endl; } void displayData() { Student st; ifstream file; file.open("Student.data", ios::in | ios::binary); if (file.is_open()) { while (file.read((char*)& st, sizeof(st))) { st.showData(); } cout << "Finished Reading Data From File." << endl; } else { cout << "Unable to open file" << endl; } file.close(); } void searchData() { Student st; ifstream file; file.open("Student.data", ios::in | ios::binary); string search; cout << "Please enter the first name of a student to search for: "; cin >> search; bool isFound = 0; while (file.read((char*)& st, sizeof(st))) { string temp = " "; getline(file, temp); for (int i = 0; i < search.size(); i++) { if (temp[i] == search[i]) isFound = 1; else { isFound = 0; break; } } if (isFound) { cout << "The name " << search << " was found in the database." << endl; break; } } if (file.read((char*)& st, sizeof(st)) && (!isFound)) { cout << "Name not found." << endl; } file.close(); } void modifyData() { Student st; string stname; bool isFound = 0; int pos; fstream file; file.open("Student.data", ios::in | ios::out | ios::binary); cout << "Enter the name of a student whose data you want to modify: "; cin >> stname; while (file.read((char*)& st, sizeof(st))) { string temp = " "; getline(file, temp); for (int i = 0; i < stname.size(); i++) { if (temp[i] == stname[i]) isFound = 1; else { isFound = 0; break; } } if (isFound) { pos = file.tellg(); cout << "Current Data" << endl; st.showData(); cout << "Modified Data" << endl; st.getData(); file.seekg(pos - sizeof(st)); file.write((char*)& st, sizeof(st)); } } if (file.read((char*)& st, sizeof(st)) && (!isFound)) { cout << "Name not found." << endl; } file.close(); } void deleteData() { Student st; string stname; bool isFound = 0; ifstream file; ofstream fout; file.open("Student.data", ios::in | ios::binary); fout.open("Temporary.data", ios::out | ios::app | ios::binary); cout << "Enter the name of a student whose data you want to delete: "; cin >> stname; while (file.read((char*)& st, sizeof(st))) { string temp = " "; getline(file, temp); for (int i = 0; i < stname.size(); i++) { if (temp[i] == stname[i]) isFound = 1; else { isFound = 0; break; } } if (isFound) { cout << "Bleh" << endl; fout.write((char*)& st, sizeof(st)); } } if (file.read((char*)& st, sizeof(st)) && (!isFound)) { cout << "Name not found." << endl; } fout.close(); file.close(); remove("Student.data"); rename("Temporary.data", "Student.data"); } void printData() { ifstream file; file.open("Student.data", ios::in | ios::binary); if (file.is_open()) { cout << file.rdbuf(); } file.close(); } int main() { int num; do { cout << "...............STUDENT MANAGEMENT SYSTEM..............\n"; cout << "======================================== ==============\n"; cout << "0. Close Program. " << endl; cout << "1. Add Data. " << endl; cout << "2. List Data. " << endl; cout << "3. Modify Data. " << endl; cout << "4. Search For Data. " << endl; cout << "5. Print Data. " << endl; cout << "6. Delete Data. " << endl; cout << "Choose an option: "; cin >> num; if (num == 1) { addData(); } else if (num == 2) { displayData(); } else if (num == 3) { modifyData(); } else if (num == 4) { searchData(); } else if (num == 5) { printData(); } else if (num == 6) { deleteData(); } } while (num > 0); return 0; } 
Ideally, when the program runs, the user chooses an option, with different function calls depending on the number entered. The Add Data option works fine, but when choosing others, such as List Data or Search Data, I run into an error: Unhandled exception thrown: read access violation. _Pnext was 0x10A6A04.
Code in xmemory:
 inline void _Container_base12::_Orphan_all() noexcept { #if _ITERATOR_DEBUG_LEVEL == 2 if (_Myproxy != nullptr) { // proxy allocated, drain it _Lockit _Lock(_LOCK_DEBUG); for (_Iterator_base12** _Pnext = &_Myproxy->_Myfirstiter; *_Pnext != nullptr; *_Pnext = (*_Pnext)->_Mynextiter) { (*_Pnext)->_Myproxy = nullptr; } _Myproxy->_Myfirstiter = nullptr; } 
The error occurs after != nullptr. Because there are no obvious errors in the code, I'd like a little help in picking out what I've done wrong.
submitted by nottooavidprogrammer to cpp_questions [link] [comments]

My IO library: fast_io library is a C++ io exception-safe RAII library based on Concepts TS and much faster than cstdio than iostream.

https://bitbucket.org/ejsvifq_mabmip/fast_io

This is a new Experimental library to replace iostream and cstdio based on C++ 2a feature concepts. Currently, the only compiler which supports Concepts TS is GCC.
iostream & cstdio are extremely insecure and slow. fprint formatter creates tons of control flow integrity security issues.
iostream has exception safety issue for its internal state.
"This fits very poorly together with exceptions, when a function must interrupt the control flow "
https://en.wikipedia.org/wiki/Criticism_of_C%2B%2B

Header only Currently. Just need #include && add compiler option -fconcepts -std=c++17 for GCC to enable Concepts TS

Windows is now using win32 APIs. Everything is a header only. You don't need to compile this library at all.

int main()
{
println(fast_io::out,"Hello World"); 
}

Since C++ 20 has not been released. No standard supporting libraries for concepts, which means a lot of Concepts Definitions are ugly. It will be changed after C++ 20 is officially published.
Design goal.

  1. Exception Safe & Exception neutral
  2. As fast as possible. As close to system call as possible.
  3. Zero Overhead.
  4. As safe as possible.
  5. Binary serialization for trivially copyable types and C++ standard library containers. Serialization methods like JSON are slow and tedious for machines. read/write
  6. std::mutex mutex stream
  7. Unicode/UTF-8 support
  8. Compatible with C stdio/ C++ iostream
  9. Native Handle Interface
  10. support C style io format (fprint). C++ style io format (<<,>>). Basic/Lua/Python/etc format (print, scan)
  11. Compilation time open mode parse. Supports C style open mode and C++ style open mode.
  12. No internal iomanip states (since it creates security issues)
  13. Extremely easy to support custom devices
  14. No stupid stuff like std::endl
  15. No integrated locale
  16. No std::char_traits and EOF shit. No use of cctype (they are not inlined and extremely slow)
  17. All fast_io devices can be natively put in C++ containers. std::vector is valid
  18. Providing RAII for FILE*&POSIX file id
  19. Dynamic IO concept-based type erasure stream Type Support
Future Plan After C++ 20
  1. Module support
  2. Coroutine support for async IO
  3. if this proposal would be released. Probably remove more stuffs here
Zero-overhead deterministic exceptions: Throwing values
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf
  1. Network handle support with coroutine. (NOT ASIO library)
NETWORK SUPPORT IS BECOMING MORE AND MORE IMPORTANT IN MODERN WORLD

Possible Stuff in the future
  1. Cryptography
  2. Compression/Decompression

I want this to be in the C++ standard library in the future. :)

How to use this library? Please see examples in the examples folder.

compile option:
g++ -o example example.cc -O2 -std=c++17 -fconcepts

Compiler recommendation under windows:

https://gcc-mcf.lhmouse.com/


Benchmark

If you do benchmark by yourself, ensure the optimization is enabled. For cmake, you need
-DCMAKE_BUILD_TYPE=Release

output 10000000 size_t to file
[email protected]:/mnt/d/hg/fast_io/tests# ./i
std::FILE*: 1.03459230s
std::ofstream: 0.59182820s
std::ofstream with tricks: 0.39233580s
obuf: 0.13328110s
obuf_mutex: 0.13685030s
input 10000000 size_t from file

[email protected]:/mnt/d/hg/fast_io/tests# ./j
std::FILE*: 1.04546250s
std::ifstream: 0.47838780s
ibuf: 0.08077780s

Windows:
g++ -o output_10M_size_t output_10M_size_t.cc -O2 -std=c++17 -fconcepts
Process started (PID=1580) >>>
<<< Process finished (PID=1580). (Exit code 0)
output_10M_size_t
Process started (PID=1208) >>>
std::FILE*: 2.26901100s
std::ofstream: 1.03628600s
std::ofstream with tricks: 0.84219500s
obuf: 0.13401100s
dynamic obuf: 0.13586300s
iobuf_dynamic system_file: 0.13000100s
obuf_mutex: 0.15303500s
g++ -o input_10M_size_t input_10M_size_t.cc -O2 -std=c++17 -fconcepts
Process started (PID=11856) >>>
<<< Process finished (PID=11856). (Exit code 0)
input_10M_size_t
Process started (PID=21088) >>>
std::FILE*: 5.53888200s
std::ifstream: 1.27124600s
ibuf: 0.07400200s
dynamic standard input stream ibuf: 0.08899900s
ibuf_dynamic isystem_file: 0.07600900s
submitted by bisixyf to cpp [link] [comments]

I am not getting the desired output .Can someone help pls??

//Telephone Directory #include  #include  #include  #include  #include  #include using namespace std; class phoneBook{ char name[20],phno[15]; public: void getdata(); void showdata(); char *getname(){ return name; } char *getphno(){ return phno; } void update(char *nm,char *telno){ strcpy(name,nm); strcpy(phno,telno); } }; void phoneBook :: getdata(){ cout<<"\nEnter Name : "; cin>>name; cout<<"Enter Phone No. : "; cin>>phno; } void phoneBook :: showdata(){ cout<<"\n"; cout<>choice; switch(choice) { case 1 : //New Record rec.getdata(); cin.get(ch); file.write((char *) &rec, sizeof(rec)); break; case 2 : //Display All Records file.seekg(0,ios::beg); cout<<"\n\nRecords in Phone Book\n"; while(file){ file.read((char *) &rec, sizeof(rec)); if(!file.eof()) rec.showdata(); } file.clear(); getch(); break; case 3 : //Search Tel. no. when person name is known. cout<<"\n\nEnter Name : "; cin>>nm; file.seekg(0,ios::beg); found=0; while(file.read((char *) &rec, sizeof(rec))) { if(strcmp(nm,rec.getname())==0) { found=1; rec.showdata(); } } file.clear(); if(found==0) cout<<"\n\n---Record Not found---\n"; getch(); break; case 4 : //Search name on basis of tel. no cout<<"\n\nEnter Telephone No : "; cin>>telno; file.seekg(0,ios::beg); found=0; while(file.read((char *) &rec, sizeof(rec))) { if(strcmp(telno,rec.getphno())==0) { found=1; rec.showdata(); } } file.clear(); if(found==0) cout<<"\n\n---Record Not found---\n"; getch(); break; case 5 : //Update Telephone No. cout<<"\n\nEnter Name : "; cin>>nm; file.seekg(0,ios::beg); found=0; while(file.read((char *) &rec, sizeof(rec))) { cnt++; if(strcmp(nm,rec.getname())==0) { found=1; break; } } file.clear(); if(found==0) cout<<"\n\n---Record Not found---\n"; else { int location = (cnt-1) * sizeof(rec); cin.get(ch); if(file.eof()) file.clear(); cout<<"Enter New Telephone No : "; cin>>telno; file.seekp(location); rec.update(nm,telno); file.write((char *) &rec, sizeof(rec)); file.flush(); } break; case 6 : return 0; default: file.close(); } } } 
During execution it is not able to show any record from its "phone.dat" file i.e while i use option 2 , I always get "no records found " as default output. Can someone tell me what do i need to change??

submitted by WildTrouble to cpp_questions [link] [comments]

What should I add to exit from the code???

#include #include #include #include using namespace std; class telephone { char name[20]; char address[20]; int ph_no; public: void get(void); void show(void); int getph_no(); }; void telephone::get(void) { cout << "\nEnter Name: "; cin.ignore(); cin.getline(name, 20); cout << "Enter Address: "; cin.ignore(); cin.getline(address, 20); cout << "\nEnter Phone Number: "; cin >> ph_no; } void telephone::show() { cout << "\nName: " << name; cout << "\nAddress: " << address; cout << "\nPhone Number: " << ph_no; } int telephone::getph_no() { return ph_no; } void write_telephone() { telephone tel; ofstream outFile; outFile.open("telephone.dat", ios::binary | ios::app); tel.get(); outFile.write(reinterpret_cast (&tel), sizeof(telephone)); outFile.close(); cout << "\n\nTelephone Directory Has Been Created..."; cin.ignore(); cin.get(); } void display_all() { telephone tel; ifstream inFile; inFile.open("telephone.dat", ios::binary); if (!inFile) { cout << "File could not be open !! Press any Key..."; cin.ignore(); cin.get(); return; } cout << "\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n"; while (inFile.read(reinterpret_cast (&tel), sizeof(telephone))) { tel.show(); cout << "\n\n====================================\n"; } inFile.close(); cin.ignore(); cin.get(); } void display_sp(int n) { telephone tel; ifstream inFile; inFile.open("telephone.dat", ios::binary); if (!inFile) { cout << "File could not be open !! Press any Key..."; cin.ignore(); cin.get(); return; } bool flag = false; while (inFile.read(reinterpret_cast (&tel), sizeof(telephone))) { if (tel.getph_no() == n) { tel.show(); flag = true; } } inFile.close(); if (flag == false) cout << "\n\nrecord not exist"; cin.ignore(); cin.get(); } void modify_telephone(int n) { bool found = false; telephone tel; fstream File; File.open("telephone.dat", ios::binary | ios::in | ios::out); if (!File) { cout << "File could not be open !! Press any Key..."; cin.ignore(); cin.get(); return; } while (!File.eof() && found == false) { File.read(reinterpret_cast (&tel), sizeof(telephone)); if (tel.getph_no() == n) { tel.show(); cout << "\n\nPlease Enter The New Details" << endl; tel.get(); int pos = (-1)*static_cast(sizeof(tel)); File.seekp(pos, ios::cur); File.write(reinterpret_cast (&tel), sizeof(telephone)); cout << "\n\n\t Record Updated"; found = true; } } File.close(); if (found == false) cout << "\n\n Record Not Found "; cin.ignore(); cin.get(); } void delete_telephone(int n) { telephone tel; ifstream inFile; inFile.open("telephone.dat", ios::binary); if (!inFile) { cout << "File could not be open !! Press any Key..."; cin.ignore(); cin.get(); return; } ofstream outFile; outFile.open("Temp.dat", ios::out); inFile.seekg(0, ios::beg); while (inFile.read(reinterpret_cast (&tel), sizeof(telephone))) { if (tel.getph_no() != n) { outFile.write(reinterpret_cast (&tel), sizeof(telephone)); } } outFile.close(); inFile.close(); remove("telephone.dat"); rename("Temp.dat", "telephone.dat"); cout << "\n\n\tRecord Deleted .."; cin.ignore(); cin.get(); } int main() { char ch; int num; cout.setf(ios::fixed | ios::showpoint); cout << setprecision(2); do { system("cls"); cout << "\t\t\t\t**\t**"; cout << "\t\t\t\t***\t***"; cout << "\t\t\t\t****\t****"; cout << "\t\t\t\t*****\t*****"; cout << "\t\t\t\t******\t******"; cout << "\t\t\t\t*******\t*******"; cout << "\t\t\t\t*******\t*******"; cout << "\t\t\t\t******\t******"; cout << "\t\t\t\t*****\t*****"; cout << "\t\t\t\t****\t****"; cout << "\t\t\t\t***\t***"; cout << "\t\t\t\t**\t**"; cout << "\t\t\t\t*\t*"; cout << "\n\t\[email protected]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"; cout << "\n\t\t1. Add Telephone Record"; cout << "\n\t\t2. Show Records"; cout << "\n\t\t3. Search Telephone Records"; cout << "\n\t\t4. Modify Record"; cout << "\n\t\t5. Delete Record"; cout << "\n\t\t6. EXIT"; cout << "\n\t\[email protected]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"; cout << "\n\t\tPlease Enter Your Choice (1-6): "; cin >> ch; system("cls"); switch (ch) { case '1': write_telephone(); break; case '2': display_all(); break; case '3': cout << "\n\tPlease Enter Phone Number: "; cin >> num; display_sp(num); break; case '4': cout << "\n\tPlease Enter Phone Number: "; cin >> num; modify_telephone(num); break; case '5': cout << "\n\tPlease Enter Phone Number: "; cin >> num; delete_telephone(num); break; case '6': default: cout << "\a"; } } while (ch != '7'); return 0; } 
I am not able to exit from the run window using option 6. What should i add??

submitted by nitishkkkk to cpp_questions [link] [comments]

Need help calling function in main.

I had this working code but I had to make a separate function for main menu so I did that and now when I call the function menu in the code it does not work and gives error any way I could fix that?
//*************************************************************** // HEADER FILE USED IN PROJECT //****************************************************************
#include #include #include #include using namespace std; //*************************************************************** // CLASS USED IN PROJECT //**************************************************************** class account { int acno; char name[50]; int deposit; char type; public: void menu(); 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 }; //class ends here void account::create_account() { cout<<"\nEnter The account No. :"; cin>>acno; cout<<"\n\nEnter The Name of The account Holder : "; cin.ignore(); cin.getline(name,50); 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. : "<>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< (&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 (&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 (&ac), sizeof(account)); if(ac.retacno()==n) { ac.show_account(); cout<<"\n\nEnter The New Details of account"<(sizeof(account)); File.seekp(pos,ios::cur); File.write(reinterpret_cast (&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 (&ac), sizeof(account))) { if(ac.retacno()!=n) { outFile.write(reinterpret_cast (&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 no. NAME Type Balance\n"; cout<<"====================================================\n"; while(inFile.read(reinterpret_cast (&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 (&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(sizeof(ac)); File.seekp(pos,ios::cur); File.write(reinterpret_cast (&ac), sizeof(account)); cout<<"\n\n\t Record Updated"; found=true; } } File.close(); if(found==false) cout<<"\n\n Record Not Found "; } void account::menu() { char ch; int num; 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\tThanks for using bank managemnt system"; break; default : cout << "\a"; } cin.ignore(); cin.get(); } while (ch != '8'); 
} P.S. - This is a project assigned last year to my friends I am just trying to solve it for practice and getting a head start.
submitted by Rutgersknight11 to cpp_questions [link] [comments]

Case 2 of the code not getting executed.Can someone tell me the required change...

//Telephone Directory #include  #include  #include  #include  #include  #include using namespace std; class phoneBook{ char name[20],phno[15]; public: void getdata(); void showdata(); char *getname(){ return name; } char *getphno(){ return phno; } void update(char *nm,char *telno){ strcpy(name,nm); strcpy(phno,telno); } }; void phoneBook :: getdata(){ cout<<"\nEnter Name : "; cin>>name; cout<<"Enter Phone No. : "; cin>>phno; } void phoneBook :: showdata(){ cout<<"\n"; cout<>choice; switch(choice) { case 1 : //New Record rec.getdata(); cin.get(ch); file.write((char *)&rec, sizeof(rec)); break; case 2 : //Display All Records file.seekg(0,ios::beg); cout<<"\n\nRecords in Phone Book\n"; while(file) { file.read((char *)&rec, sizeof(rec)); if(!file.eof()) rec.showdata(); } file.clear(); getch(); break; case 3 : //Search Tel. no. when person name is known. cout<<"\n\nEnter Name : "; cin>>nm; file.seekg(0,ios::beg); found=0; while(file.read((char *) &rec, sizeof(rec))) { if(strcmp(nm,rec.getname())==0) { found=1; rec.showdata(); } } file.clear(); if(found==0) cout<<"\n\n---Record Not found---\n"; getch(); break; case 4 : //Search name on basis of tel. no cout<<"\n\nEnter Telephone No : "; cin>>telno; file.seekg(0,ios::beg); found=0; while(file.read((char *) &rec, sizeof(rec))) { if(strcmp(telno,rec.getphno())==0) { found=1; rec.showdata(); } } file.clear(); if(found==0) cout<<"\n\n---Record Not found---\n"; getch(); break; case 5 : //Update Telephone No. cout<<"\n\nEnter Name : "; cin>>nm; file.seekg(0,ios::beg); found=0; while(file.read((char *) &rec, sizeof(rec))) { cnt++; if(strcmp(nm,rec.getname())==0) { found=1; break; } } file.clear(); if(found==0) cout<<"\n\n---Record Not found---\n"; else { int location = (cnt-1) * sizeof(rec); cin.get(ch); if(file.eof()) file.clear(); cout<<"Enter New Telephone No : "; cin>>telno; file.seekp(location); rec.update(nm,telno); file.write((char *) &rec, sizeof(rec)); file.flush(); } break; case 6 : return 0; default: file.close(); } } } 
I am not able to show the data stored in the file using option 2 ??why???

submitted by WildTrouble to cpp_questions [link] [comments]

I need help calling the function in main

I had this working code but I had to make a separate function for main menu so I did that and now when I call the function menu in the code it does not work and gives error any way I could fix that? //*************************************************************** // HEADER FILE USED IN PROJECT //****************************************************************
#include #include #include #include using namespace std; //*************************************************************** // CLASS USED IN PROJECT //**************************************************************** class account { int acno; char name[50]; int deposit; char type; public: void menu(); 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 }; //class ends here void account::create_account() { cout<<"\nEnter The account No. :"; cin>>acno; cout<<"\n\nEnter The Name of The account Holder : "; cin.ignore(); cin.getline(name,50); 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. : "<>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< (&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 (&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 (&ac), sizeof(account)); if(ac.retacno()==n) { ac.show_account(); cout<<"\n\nEnter The New Details of account"<(sizeof(account)); File.seekp(pos,ios::cur); File.write(reinterpret_cast (&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 (&ac), sizeof(account))) { if(ac.retacno()!=n) { outFile.write(reinterpret_cast (&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 no. NAME Type Balance\n"; cout<<"====================================================\n"; while(inFile.read(reinterpret_cast (&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 (&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(sizeof(ac)); File.seekp(pos,ios::cur); File.write(reinterpret_cast (&ac), sizeof(account)); cout<<"\n\n\t Record Updated"; found=true; } } File.close(); if(found==false) cout<<"\n\n Record Not Found "; } void account::menu() { char ch; int num; 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\tThanks for using bank managemnt system"; break; default : cout << "\a"; } cin.ignore(); cin.get(); } while (ch != '8'); 
} P.S. - This is a project assigned last year to my friends I am just trying to solve it for practice and getting a head start.
submitted by Rutgersknight11 to Cplusplus [link] [comments]

Get code examples like "iomanip functions c++" instantly right from your google search results with the Grepper Chrome Extension. Sunday, 5 March 2017. Iomanip Binary Options Sets the format flags specified by parameter mask. Behaves as if member setf were called with mask as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). See ios_base::fmtflags for more information on the particular flags that can be modified with this manipulator function. I am trying to achieve converting a binary content read from a file to a byte continuously . My file samplefile.bin contains 16bytes and this is in the form of binary . In the Hexed.it it will show like this .. What I needed in this case is I need to extract these binary values in Hex format and store it to a char *. I will post my sample code over here. Aligning text with iomanip It's possible to specify whether output is left or right aligned by using the manipulator flags that are part of ios_bas. In particular, it is possible to specify that output should be either left or right aligned by passing in the stream manipulators std::left and std::right. Putting Your Knowledge of iomanip Together Now that we know how to space and align text, we ... ShreevatsaR Ein Problem bei der Wiederherstellung von dec über hex ist, dass dec möglicherweise nicht der zuvor eingestellte Wert gewesen i... Shane http://www.blogger.com/profile/18122882257580861903 [email protected] Blogger 120 1 25 tag:blogger.com,1999:blog-7199211001067866733.post-8181513005308943284 ... Header der Standardbibliothek <iomanip> Dieser Header ist Teil der Input / Output-Manipulatoren-Bibliothek. Definitionen . resetiosflags. löscht die angegebenen ios_base-Flags (Funktion) setiosflags . setzt die angegebenen ios_base Flags (Funktion) setbase. Ändert die für Integer-E / A verwendete Basis (Funktion) setfill. ändert das Füllzeichen (Funktionsvorlage) setprecision. ändert die ... <iomanip> IO Manipulators. Header providing parametric manipulators: Parametric manipulators setiosflags Set format flags (function ) resetiosflags Reset format flags (function ) setbase Set basefield flag (function ) setfill Set fill character (function ) setprecision Set decimal precision (function ) setw Set field width (function ) get_money Get monetary value (function ) put_money Put ... Wednesday, October 19, 2016. Iomanip Binary Options

[index] [17399] [10929] [14881] [21995] [8463] [22349] [2905] [29652] [13649] [25717]

http://binary-optiontrade.giucudidpostprimen.tk