Question

Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able...

Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well.

you will also need to able to modify the code so it will accept name such as "john kenny " and the progrom should also ask for the student id number.

#include<iostream>
#include<stdlib.h>

using namespace std;

struct st

{

int roll;

char name[50];

char grade;

struct st *next;

};

struct st *hptr=0; // declare null header of linked list

int addStudent();

int delete_student();

void display();

int update();

int search();

int main()

{

char ch;

int opt;

do

{

cout<<"Choose option:\n\t1. Add Student\n"

"\t2. Remove Student\n"

"\t3. Update grade\n"

"\t4. Search Student\n"

"\t5. Display Records\n";

cin>>opt;

switch(opt)

{

case 1: addStudent();

break;

case 2: delete_student();

break;

case 3: update();

break;

case 4: search();

break;

case 5: display();

break;

default:

cout<<"Invaild Option, Try again...!\n";

break;

}

cout<<"Do you wants another operation(y/n)? : ";

cin>>ch;

}while(ch=='Y'||ch=='y');

}

/* Add Student */

int addStudent()

{

st *temp = new st;

st *temp1 = new st;

cout<<"Enter roll no:";

cin>>temp->roll;

if(cin.fail()){

cout<<"Not numeric\n";

return 0;

}

cout<<"Enter name:";

cin>>temp->name;

cout<<"Enter grade:";

cin>>temp->grade;

// Head of linked list is null

if(hptr==0)

{

temp->next=hptr;

hptr=temp;

}

else

{

temp1=hptr;

while(temp1->next)

temp1=temp1->next;

temp->next=temp1->next;//NULL

temp1->next=temp;

}

}

/* Delete Student */

int delete_student()

{

int rollno;

st *temp = new st;

st *temp1 = new st;

cout<<"Do U wants to delete record ,then enter Roll No.:";

cin>>rollno;

//Rollno is not numeric

if(cin.fail()){

cout<<"Not numeric\n";

return 0;

}

temp=hptr;

while(temp)

{

if(temp->roll==rollno)

{

// searching record is head

if(temp==hptr)

{

hptr=temp->next;

free(temp);

return 0;

}

else

{

temp1->next=temp->next;

free(temp);

return 0;

}

}

temp1=temp;

temp=temp->next;

}

cout<<"Record is not found..!\n";

}

/* Update the grade of the student */

int update()

{

int rollno;

st *temp = new st;

st *temp1 = new st;

cout<<"Enter roll number.:";

cin>>rollno;

if(cin.fail()){

cout<<"Not numeric\n";

return 0;

}

temp=hptr;

while(temp)

{

if(temp->roll==rollno)

{

cout<<"Enter grade to update:";

cin>>temp->grade;

return 0;

}

temp1=temp;

temp=temp->next;

}

cout<<"Record not found..!\n";

}

/* search the student */

int search()

{

int rollno;

st *temp = new st;

st *temp1 = new st;

cout<<"Enter roll number:";

cin>>rollno;

if(cin.fail()){

cout<<"Not numeric\n";

return 0;

}

temp=hptr;

cout<<"RollNo.\t\t|Name\t\t|Grade\n";

cout<<"-------------------------------------------\n";

while(temp)

{

if(temp->roll==rollno)

{

cout<<temp->roll<<"\t\t|"<<temp->name<<"\t\t|"<<temp->grade<<"\n";

return 0;

}

temp1=temp;

temp=temp->next;

}

cout<<"Record is not present..!\n";

}

/* Display the record */

void display()

{

cout<<"RollNo.\t\t|Name\t\t|Grade\n";

cout<<"-------------------------------------------\n";

struct st *ptr;

ptr=hptr;

while(ptr)

{

cout<<ptr->roll<<"\t\t|"<<ptr->name<<"\t\t|"<<ptr->grade<<"\n";

ptr=ptr->next;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string>

using namespace std;

struct st

{

   int roll;

   char name[50];

   char grade;

   struct st* next;

};

struct st* hptr = 0; // declare null header of linked list

int addStudent();

int delete_student();

void display();

int update();

int search();

void save();

void read();

int main()

{

   char ch;

   int opt;

   do

   {

       cout << "Choose option:\n\t1. Add Student\n"

           "\t2. Remove Student\n"

           "\t3. Update grade\n"

           "\t4. Search Student\n"

           "\t5. Display Records\n"

           "\t6. Save To Database\n"

           "\t7. Read From Database\n";

       cin >> opt;

       switch (opt)

       {

       case 1: addStudent();

           break;

       case 2: delete_student();

           break;

       case 3: update();

           break;

       case 4: search();

           break;

       case 5: display();

           break;

       case 6: save();

           break;

       case 7: read();

           break;

       default:

           cout << "Invaild Option, Try again...!\n";

           break;

       }

       cout << "Do you wants another operation(y/n)? : ";

       cin >> ch;

   } while (ch == 'Y' || ch == 'y');

}

/* Add Student */

int addStudent()

{

   st* temp = new st;

   st* temp1 = new st;

   cout << "Enter roll no:";

   cin >> temp->roll;

   if (cin.fail()) {

       cout << "Not numeric\n";

       return 0;

   }

   cout << "Enter name:";
   cin.ignore();
   cin.getline(temp->name, 49, '\n');

   cout << "Enter grade:";

   cin >> temp->grade;

   // Head of linked list is null

   if (hptr == 0)

   {

       temp->next = hptr;

       hptr = temp;

   }

   else

   {

       temp1 = hptr;

       while (temp1->next)

           temp1 = temp1->next;

       temp->next = temp1->next;//NULL

       temp1->next = temp;

   }

}

/* Delete Student */

int delete_student()

{

   int rollno;

   st* temp = new st;

   st* temp1 = new st;

   cout << "Do U wants to delete record ,then enter Roll No.:";

   cin >> rollno;

   //Rollno is not numeric

   if (cin.fail()) {

       cout << "Not numeric\n";

       return 0;

   }

   temp = hptr;

   while (temp)

   {

       if (temp->roll == rollno)

       {

           // searching record is head

           if (temp == hptr)

           {

               hptr = temp->next;

               free(temp);

               return 0;

           }

           else

           {

               temp1->next = temp->next;

               free(temp);

               return 0;

           }

       }

       temp1 = temp;

       temp = temp->next;

   }

   cout << "Record is not found..!\n";

}

/* Update the grade of the student */

int update()

{

   int rollno;

   st* temp = new st;

   st* temp1 = new st;

   cout << "Enter roll number.:";

   cin >> rollno;

   if (cin.fail()) {

       cout << "Not numeric\n";

       return 0;

   }

   temp = hptr;

   while (temp)

   {

       if (temp->roll == rollno)

       {

           cout << "Enter grade to update:";

           cin >> temp->grade;

           return 0;

       }

       temp1 = temp;

       temp = temp->next;

   }

   cout << "Record not found..!\n";

}

/* search the student */

int search()

{

   int rollno;

   st* temp = new st;

   st* temp1 = new st;

   cout << "Enter roll number:";

   cin >> rollno;

   if (cin.fail()) {

       cout << "Not numeric\n";

       return 0;

   }

   temp = hptr;

   cout << "RollNo.\t\t|Name\t\t|Grade\n";

   cout << "-------------------------------------------\n";

   while (temp)

   {

       if (temp->roll == rollno)

       {

           cout << temp->roll << "\t\t|" << temp->name << "\t\t|" << temp->grade << "\n";

           return 0;

       }

       temp1 = temp;

       temp = temp->next;

   }

   cout << "Record is not present..!\n";

}

/* Display the record */

void display()

{

   cout << "RollNo.\t\t|Name\t\t|Grade\n";

   cout << "-------------------------------------------\n";

   struct st* ptr;

   ptr = hptr;

   while (ptr)

   {

       cout << ptr->roll << "\t\t|" << ptr->name << "\t\t|" << ptr->grade << "\n";

       ptr = ptr->next;

   }

}

void save()
{
   // stores the current data of linked list in database.txt
   // format of storage is "'roll' 'name' 'grade'"
   ofstream fout("database.txt");

   struct st* ptr;

   ptr = hptr;

   while (ptr)

   {

       fout << ptr->roll << "\n" << ptr->name << "\n" << ptr->grade << "\n";

       ptr = ptr->next;

   }
}

void read()
{
   // reads from file database.txt
   // keeps reading till end of file.
   // stores the data in the currect linked list.
   // you can use display() to view data that is read from database.txt.
   ifstream fin("database.txt");

   while (!fin.eof())
   {
       st* temp = new st;

       st* temp1 = new st;

       fin >> temp->roll;
       if (temp->roll < 0) break;

       fin.ignore();

       fin.getline(temp->name, 49, '\n');

       fin >> temp->grade;

       // Head of linked list is null

       if (hptr == 0)

       {

           temp->next = hptr;

           hptr = temp;

       }

       else

       {

           temp1 = hptr;

           while (temp1->next)

               temp1 = temp1->next;

           temp->next = temp1->next;//NULL

           temp1->next = temp;

       }
   }
}

Choose option: 1. Add Student 2. Remove Student 3. Update grade 4. Search Student 5. Display Records 6. Save To Database 7. R

Choose option: 1. Add Student 2. Remove Student 3. Update grade 4. Search Student 5. Display Records 6. Save To Database 7. R

it also takes input full names, with spaces in add function, and also reads full names from database.txt in read function.

for any query leave a comment here ill get back to you as soon as possible :)

PLEASE DO NOT FORGET TO LEAVE A THUMBS UP! THANKS! :)

Add a comment
Know the answer?
Add Answer to:
Based on this program modify the source code so that it will able to save the student record into a database txt and able to display and modify from a database txt as well. you will also need to able...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • 1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum,...

    1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum, courseName, & grade) 2) Change the type for the 'data' member variable in the node struct to CourseInfo (see #1) and rename it to 'courseData' 3) Modify the createNode function to receive a CourseInfo struct as a parameter. It should also display the address of the new node that is created. Display the address in both hex and decimal form. 4) Modify the display...

  • I need to be able to input first and last name. My code only outputs first...

    I need to be able to input first and last name. My code only outputs first when you put 2 names. This is not my full code. Any type of help wou;d be great! int main() { int main_choice; int grade_choice; int std_id; int new_grade; char g_name[100]; char s_name[100]; float a,b,c; gradebook g; do { cout <<endl; cout<< " -=| MAIN MENU |=-" << endl; cout<< "1. Add a student" << endl; cout << "2. Remove a student" << endl;...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT