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
  • Hi I've a problem with this code. When I add more than 1 song to the...

    Hi I've a problem with this code. When I add more than 1 song to the list, it doesn't show the first one, only shows the latest one, when I called the function displaysong, let's say when you add 2 songs ID 1 then ID 2, it shows only ID 1. Can you fix it.  Everything else is fine.. #include <iostream> #include<string> using namespace std; //class Song class Song{ private: int songID; string title; string artist; string album; int year; public:...

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

  • This is just a partial C++ code. I am having a problem with the getline(cin, variable)...

    This is just a partial C++ code. I am having a problem with the getline(cin, variable) statement of my code. Please run the code for yourself and see that it doesn't let you enter the full name. It skips the prompt to enter the name and goes straight to the next cout statement. I can't use cin alone because I need to capture and store both the first name followed by space followed by last name. I was trying 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...

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

  • heree is the code #ifndef ASSIGNMENT_VERSION1_DATA_STRUCT_H #define ASSIGNMENT_VERSION1_DATA_STRUCT_H #endif //ASSIGNMENT_VERSION1_DATA_STRUCT_H #include #include #include #include // for...

    heree is the code #ifndef ASSIGNMENT_VERSION1_DATA_STRUCT_H #define ASSIGNMENT_VERSION1_DATA_STRUCT_H #endif //ASSIGNMENT_VERSION1_DATA_STRUCT_H #include #include #include #include // for setw and setfill stream manipulators #include // for invalid_argument exception class #include // for ostringstream class #include #include #include using namespace std; class data_struct { private: // data members int id, size_file, parent_id; char folder; string file_folder_name; public: data_struct *left; data_struct *right; // constructor with 4 parameters data_struct(int i, string nm, int l, char m, int n) { id = i; file_folder_name = nm;...

  • 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'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

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

  • The project I have is a link list that asks for a name and phone number...

    The project I have is a link list that asks for a name and phone number and it allows the user to insert ,delete ,modify ,and find an element in the structure. The program has a menu in which the user can choose which function they want to use I need to use  switch case to do this but it has not worked I need help with this. Thank you! #include <iostream> #include <string> #include <iomanip> using namespace std; const int...

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