#include <cstdlib> #include <iostream> #include <fstream> #include <string> using namespace std; //function prototype int add_records(); int get_records(); int user_choice(); int search_records(); string buffer; //structure struct tabledata//creating linked list { int stud_id;//will store information char name[100];//will store information float code;//will store information tabledata *next;//the reference to the next node }; typedef struct tabledata node; node *head,*current,*temp; int main(int argc, char *argv[]) { cout<<"\t\t*************************************************"<<endl; cout<<"\t\t\tStructured Programming Assignment"<<endl; cout<<"\t\t*************************************************\n\n\n"<<endl; cout<<"\t\tStudent Name: Ashvind Anand MITTOO\n"; cout<<"\t\tStudent ID: 101384\n"; cout<<"\t\tCohort ID : BCNS/10B/FT"<<endl; cout<<"\t\tProgramme : BSc(Hons.) Computer Science with Network Security"<<endl; cout<<"\n\n\t\tQuestion 2\n"<<endl; system("PAUSE"); system("CLS"); //calling the functions to main user_choice(); add_records(); get_records(); } //Function user_choice int user_choice() { char user; system("CLS"); cout<<"Do you want to add/search/exit : "; cin>>user; if ((user=='A') || (user=='a')) { get_records(); add_records(); } else if ((user=='e') || (user=='E')) { exit(1); } else if ((user=='S') || (user=='s')) { int search_records(); } else { cout<<"Invalid input!\n"; system("PAUSE"); system("CLS"); user_choice(); } } //function to add at the end of list int add_records() { int stud_id; char name[100]; float code; char user; int counter=0; head=NULL; //opening file datatable for writing ofstream fin; fin.open("tabledata.txt",ios::app); while(1) { cout<<"Do you want to enter a Data [Y/N/E]: "; cin>>user; if ((user=='N') || (user=='n')) { //break out of the loop... break; } else if ((user=='E') || (user=='e')) { user_choice(); } else if ((user=='Y') || (user=='y')) { current=new node; cout<<"\n\tEnter ID: "; cin>>stud_id; current->stud_id=stud_id; cout<<"\tName: "; //pause for user to enter Name cin.ignore(1,'\n'); cin.getline(name,100,'\n'); //insert into linked list strcpy(current->name,name); cout<<"\tCode: "; cin>>code; current->code=code; //saving to file tabledata.txt fin<<"\n "<<stud_id<<" "<<name<<"\t"<<code; //incrementing linked list and linking nodes current->next=head; head=current; } else { cout<<"\nInvalid input. Only Y/N/E allowed.Re-enter your choice:\n"; } } fin.close(); get_records(); } //function to get records from tabledata.txt int get_records() { int stud_id;//declaration string name;//declaration float code;//declaration ifstream fin;//open output file tabledata.txt for reading fin.open("tabledata.txt"); //Printing Header for Table cout<<"\nID\tName\t\tCode\n"; cout<<"-------------------------------\n"; if(!fin) { cout<<"Can't open tabledata.txt file"; exit(1); } else { while(!fin.eof()) { fin>>stud_id>>name>>code; cout<<stud_id<<"\t"<<name<<"\t\t"<<code<<"\n\n"; } /*while(head != NULL) { cout<<head->stud_id<<"\t"<<head->name<<"\t\t"<<head->code<<"\n\n"; head=head->next; }*/ } fin.close(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)