Question

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 represents start node, tail represent last node
       node *head, *tail;
       public:
       list()
       {
           head=NULL;
           tail=NULL;
       }
       void createnode(string name,string id,int score)
       {
           //Create a new node temp with passed data
           node *temp=new node;
           temp->name=name;
           temp->id=id;
           temp->score=score;
           temp->next=NULL;
           //If list is empty then temp will be the node
           if(head==NULL)
           {
               head=temp;
               tail=temp;
               temp=NULL;
           }
           //append temp at end of the list
           else
           {  
               tail->next=temp;
               tail=temp;
           }
       }
       void display(string id)
       {
           node *temp=new node;
           temp=head;
           //Traverse entire list
           while(temp!=NULL)
           {
               //if We found the key print details and returns
               if(temp->id==id)
               {
                   cout<<"Name:"<<temp->name<<endl;
                   cout<<"ID:"<<temp->id<<endl;
                   cout<<"Grade:"<<getGrade(temp->score)<<endl;
                   return;
               }
               //Move to next node
               temp=temp->next;
           }
           //We come here when key is not found
           cout << "ID not found";
       }
       //Funcntion for grading
       string getGrade(int score)
       {
           if(score<=100 && score>=93)
               return "A";
           else if(score<=92 && score>=90)
               return "A-";
           else if(score<=89 && score>=87)
               return "B+";
           else if(score<=86 && score>=83)
               return "B-";
           else if(score<=79 && score>=77)
               return "C+";
           else if(score<=76 && score>=73)
               return "C";
           else if(score<=72 && score>=70)
               return "C-";
           else if(score<=69 && score>=67)
               return "D+";
           else if(score<=66 && score>=60)
               return "D";
           else
               return "F";
       }
       void deleteRecord(string id)
       {
           node *temp=new node;
           node *prev=new node;
           temp=head;
           //If id at head, move head to next node
           if(temp!=NULL && temp->id==id)
           {
               head=temp->next;
               free(temp);
               return;
           }
           //iterate untill id found
           while (temp != NULL && temp->id != id)
            {
                prev = temp;
                temp = temp->next;
            }
            //we come here when id not found
           if(temp==NULL)
           {
               cout << "ID not found";
               return;
           }
           //delete temp node
           prev->next=temp->next;
           free(temp);           
       }
};
int main()
{
   list obj;
   int choice,score;
   string name,id;
   while(true)
   {
       cout << "\n1.Insert\n2.Display\n3.Delete\n4.exit: ";
       cin >> choice;
       if(choice==1)
       {
           cout << "Enter name: ";
           cin >> name;
           cout << "Enter id: ";
           cin >> id;
           cout << "Enter score: ";
           cin >> score;
           obj.createnode(name,id,score);
       }
       else if(choice==2)
       {
           string searchId;
           cout << "Enter ID to Display: ";
           cin >> searchId;
           obj.display(searchId);
       }
       else if(choice==3)
       {
           string searchID;
           cout << "Enter ID to delete record: ";
           cin >> searchID;
           obj.deleteRecord(searchID);
       }
       else
           break;
   }
   return 0;
}

Insert Display Delete exit: 1 nter name Eswar Enter id 1 nter score: 78 Insert Display Delete exit: 1 nter name Aruna Enter id2 nter score: 76 Insert Display Delete exit: 1 nter name Akhi Enter id3 nter score: 99 Insert Display Delete exit 2 nter ID to Display: 3 Name Akhi ID:3 rade H . Insert Display Delete 4.exit: 2 Enter ID to Display: 4 ID not found . Insert Display Delete 4.exit: 3 Enter ID to delete record: 5 ID not found . Insert Display Delete exit:

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

Program.cpp

#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 represents start node, tail represent last node
node *head, *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void createnode(string name,string id,int score)
{
//Create a new node temp with passed data
node *temp=new node;
temp->name=name;
temp->id=id;
temp->score=score;
temp->next=NULL;
//If list is empty then temp will be the node
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
//append temp at end of the list
else
{
tail->next=temp;
tail=temp;
}
}
void display(string id)
{
node *temp=new node;
temp=head;
//Traverse entire list
while(temp!=NULL)
{
//if We found the key print details and returns
if(temp->id==id)
{
cout<<"Name:"<<temp->name<<endl;
cout<<"ID:"<<temp->id<<endl;
cout<<"Grade:"<<getGrade(temp->score)<<endl;
return;
}
//Move to next node
temp=temp->next;
}
//We come here when key is not found
cout << "ID not found";
}
//Funcntion for grading
string getGrade(int score)
{
if(score<=100 && score>=93)
return "A";
else if(score<=92 && score>=90)
return "A-";
else if(score<=89 && score>=87)
return "B+";
else if(score<=86 && score>=83)
return "B-";
else if(score<=79 && score>=77)
return "C+";
else if(score<=76 && score>=73)
return "C";
else if(score<=72 && score>=70)
return "C-";
else if(score<=69 && score>=67)
return "D+";
else if(score<=66 && score>=60)
return "D";
else
return "F";
}
void deleteRecord(string id)
{
node *temp=new node;
node *prev=new node;
temp=head;
//If id at head, move head to next node
if(temp!=NULL && temp->id==id)
{
head=temp->next;
free(temp);
return;
}
//iterate untill id found
while (temp != NULL && temp->id != id)
{
prev = temp;
temp = temp->next;
}
//we come here when id not found
if(temp==NULL)
{
cout << "ID not found";
return;
}
//delete temp node
prev->next=temp->next;
free(temp);   
}
void average_grade_score(){//calculate average
node *temp=new node;
int avg=0,length=0;
temp=head;
//Traverse entire list
while(temp!=NULL)
{
avg+=temp->score;
temp=temp->next;
length++;
  
}
cout<<"Average is: "<<avg/length;
}
};
int main()
{
list obj;
int choice,score;
string name,id;
while(true)
{
cout << "\n1.Insert\n2.Display\n3.Delete\n4.Calculate Average\n5.exit: ";
cin >> choice;
if(choice==1)
{
cout << "Enter name: ";
cin >> name;
cout << "Enter id: ";
cin >> id;
cout << "Enter score: ";
cin >> score;
obj.createnode(name,id,score);
}
else if(choice==2)
{
string searchId;
cout << "Enter ID to Display: ";
cin >> searchId;
obj.display(searchId);
}
else if(choice==3)
{
string searchID;
cout << "Enter ID to delete record: ";
cin >> searchID;
obj.deleteRecord(searchID);
}else if (choice == 4){
obj.average_grade_score();
}
else
break;
}
return 0;
}

Output

1.Insert Display 3.Delete ●Calculate Average 5.exit: 1 Enter name: abc Enter id: 1 Enter score: 23 1.Insert Display 3.Delete 4.Calculate Average 5.exit: 1 Enter name: def Enter id: 2 Enter score: 45 1.Insert Display 3.Delete 4 Calculate Average 5.exit: 4 Average iS: 34 1.Insert Display 3.Delete

Add a comment
Know the answer?
Add Answer to:
I have a C++ code that lets me enter, display and delete a student record. I...
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
  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • 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...

  • 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;...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • below i added the code, but while running it, makefile.win error showing. how i fix this?...

    below i added the code, but while running it, makefile.win error showing. how i fix this? 1.)waitlist.h #include<iostream> using namespace std; class guest{    public:    string name;    guest*next;    guest*prev;    guest(string);    }; class waitList{    int numberOfGuests;    guest* first;    guest* last;    public:        waitList();        void addPerson(string);        void deletePerson(string);        guest* getFirst();        guest* getLast();    }; 2.)waitlist.cpp #include<iostream> #include"waitlist.h" using namespace std; guest::guest(string name){    this->name...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • 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...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

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