'temp' is undeclared, first use this function - c++

#include<iostream>
#include<conio.h>
#include<string>
#include<cstring>
#include<stdlib.h>
using namespace std;
bool rgstr_stdnt(struct student *stud,struct list *ls);
double calculate_aggregate(struct student *);
void srch_student(struct student * next);
void addToList(struct student *stud, struct list *l);
void display(struct student *stud, struct list *l);
struct student
{
char name[20];
int matric_marks, inter_marks, aptitude_marks;
int temp;
student *next;
};
struct list
{
char name[20];
double aggr;
list *next;
};
this is where the problem shows up, it says temp undeclared, first use this function and I'm not able to rectify it
void srch_stdnt(struct student *stud)
{
char name[60];
cout << "Enter Student to search :";
cin>>name;
cout << name;
//down here, the error comes and whatever i knew, i have tried to solve it but could not
while (temp!=NULL){
if(strcmp(temp->name, name)==0){
}
temp = temp->next;
}
cout << "No match found";
}
int main()
{
student * temp=new student();
student *s;
s = (struct student *) malloc(sizeof(struct student));
struct list *ls;
ls = (struct list *) malloc(sizeof(struct list));
strcpy(ls->name,"");
ls->aggr = 0;
ls->next= NULL;
do
{
cout<<" STUDENT ENROLLMENT AND RECORDS MANAGEMENT"<<endl;
cout<<"1- To Enroll A New Sudent."<<endl;
cout<<"2- To View The Enrolled Students."<<endl;
cout<<"3- To Search Through The Already Enrolled Students."<<endl;
cout<<"4- Exit."<<endl;
int input;
cin>>input;
if (input == 1)
{
rgstr_stdnt(s, ls);
}
else if (input == 2)
{
display(s, ls);
}
else if(input == 3)
{
void srch_student(struct student * stud);
}
else if (input == 4)
exit(0);
cout<<endl;
} while(1);
getch();
}
bool rgstr_stdnt(struct student *stud,struct list *ls)
{
student *s = stud;
cout<<"Enter Name Of The Student: "<<endl;
cin>>s->name;
cout<<"Enter Percentage in 10th Grade: "<<endl;
cin>>s->matric_marks;
cout<<"Enter Intermediate Percentage:"<<endl;
cin>>s->inter_marks;
cout<<"Enter Percentage In Aptitude Test: "<<endl;
cin>>s->aptitude_marks;
double aggregate;
aggregate = calculate_aggregate(s);
cout<<"Aggregate Percentage Is: "<< aggregate<<"%"<<endl;
if (aggregate >= 70)
{
cout<<"-> Student Enrolled In BSCS. <-"<<endl;
addToList(s,ls);
return true;
}
else if (aggregate >= 60)
{
cout<<"-> Student Enrolled In BE. <-"<<endl;
addToList(s,ls);
return true;
}
else if (aggregate >=50)
{
cout<<"-> Student Enrolled In MS. <-"<<endl;
addToList(s,ls);
return true;
}
else
{
cout<<"Sorry, Low Percentage. Student Can't Be Enrolled!"<<endl;
return false;
}
}
double calculate_aggregate(struct student *stud)
{
student *s = stud;
double aggr;
aggr = s->matric_marks * 10/100 + s->inter_marks * 50/100 +
s->aptitude_marks * 40/100;
return aggr;
}
void addToList(struct student *stud, struct list *l)
{
list *pointer = l;
while (pointer->next != NULL)
{
pointer = pointer->next;
}
pointer->next = (struct list *) malloc(sizeof(struct list));
pointer = pointer->next;
strcpy(pointer->name , stud->name);
pointer->aggr = calculate_aggregate(stud);
pointer->next = NULL;
}
void display(struct student *stud, struct list *l)
{
list *pointer = l;
if (pointer->next == NULL)
cout<<"No Students Enrolled Yet."<<endl;
else
{
cout<<" !- - - - - - - - -. STUDENTS RECORDS .- - - - - - - - - -! "
<<endl;
while (pointer->next != NULL)
{
pointer = pointer->next;
cout<<"Name Of Student: "<<pointer->name<<endl;
cout<<"Aggregate Is: "<<pointer->aggr<<endl;
if (pointer->aggr >= 70)
cout<<"-> Student Enrolled In BSCS. <-"<<endl;
else if(pointer->aggr >=60)
cout<<"-> Student Enrolled In BE. <-"<<endl;
else
cout<<"-> Student Enrolled In MS. <-"<<endl;
cout<<endl;
}
}
}
Any who could help me solve this, i'd really be grateful.

As the error message states, you're using a variable called temp that you haven't declared yet. You need to declare it and give it an initial value:
struct student *temp = stud;
while (temp!=NULL){
...

//down here, the error comes and whatever i knew, i have tried to solve it but could not
while (temp!=NULL){
You have not declared temp in the function. That explains the compiler error.
Perhaps you meant to use:
struct student* temp = stud;
while ( temp != NULL )
Since you are in C++ land, discard the struct and use:
student* temp = stud;
while ( temp != NULL )
That change change can be made in rest of your code too.

Related

How to sort nodes from a stack and put into a queue

im trying to implement both queue and stack in my mini project. Customer will enter nodes into stack and the seller will sort the nodes from stack and put them into queue. I want to sort nodes from a stack based on college name, where all nodes with college = "kdoj" will get into the queue first then followed by all nodes with college = "kdse". The problem is i have tried doing the sorting with 2 (for loop) and i dont know why only 2 nodes get into the queue when i have more than 2 nodes in the stack. i am not sure where is the problem and been stuck here for a while :(.
This is the input example:
name : david
ic num: 123
phone num: 123
college: kdse
quantity: 1
size: m
name : jojo
ic num: 123
phone num: 123
college: kdse
quantity: 1
size: m
name : zoro
ic num: 123
phone num: 123
college: kdoj
quantity: 1
size: m
This is the runnable code.
The problem will occur when you sort the list in seller menu after entering the data.
#include <iostream>
#include <cstring>
#include<iomanip>
#include <conio.h>
#include<string>
using namespace std;
//nodes
struct customerNode {
string name, id, college;
int phoneNum,quantity;
char size;
public:
customerNode* next;
customerNode(string name, string id, int phoneNum,string college, int quantity, char size)
{
name = name;
id = id;
phoneNum = phoneNum;
college = college;
quantity = quantity;
size = size;
next = NULL;
}
};
//class customer
class Customer{
public:
customerNode *head = NULL;
bool isEmpty(){
return(head == NULL);
}
void pushStack(string name, string id, int phoneNum, string college, int quantity, char size){
customerNode *newnode = new customerNode(name,id,phoneNum,college,quantity,size);
newnode->name = name;
newnode->id= id;
newnode->phoneNum = phoneNum;
newnode->college = college;
newnode->quantity = quantity;
newnode->size = size;
if(isEmpty()){
newnode->next = head;
head = newnode;
cout<<endl<<"created new stack."<<endl;
}
else{
newnode->next = head;
head = newnode;
cout<<endl<<"added node into a stack."<<endl;
}
}
//pop out node from stack
void popStack(){
customerNode *temp;
temp = head; //temp variable
head = temp->next;
temp->next = NULL;
cout<<endl<<"Order has been deleted!";
}
//push node into stack
void enterOrder(){
int phoneNum, quantity;
char size;
string name, id, college;
cout<<endl<<"\tADDING YOUR ORDER.\n";
cout<<"\tName: ";
cin.ignore();
getline(cin,name);
cout<<"\tIC Num: ";
cin>>id;
cout<<"\tPhone Number: ";
cin>>phoneNum;
cout<<"\tCollege name: ";
cin>>college;
cout<<"\tQuantity: ";
cin>>quantity;
cout<<"\tSize (S/M/L/XL): ";
cin>>size;
pushStack(name, id, phoneNum,college, quantity, size);
cin.get();
}
//see order and trigger popout funct
void deleteorder(){
char choice;
cout<<"YOUR ODER."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
stackTop();
cout<<endl;
cout<<endl<<"Confirm to delete?: ";
cin>>choice;
if(choice == 'y' || choice == 'Y'){
popStack();
}
}
//display node at the top
void stackTop(){
customerNode *check;
if(!isEmpty()){
check = head;
cout<<endl<<
setw(10)<<left<<check->id
<<setw(20)<<left<<check->name
<<setw(10)<<left<<check->phoneNum
<<setw(20)<<left<<check->college
<<setw(10)<<left<<check->quantity
<<setw(5)<<left<<check->size;
}
else{
cout<<endl<<"Stack is an underflow.";
getch();
}
}
int getNumberNodes(){
customerNode *check;
check = head;
int counter = 0;
while(check!= NULL){
counter++;
check = check->next;
}
return counter;
}
};
//Customer main menu
void customerMenu(Customer &c){
int choice;
do{cout
<<endl<<"CUSTOMER MENU"
<<endl<<"Please choose your task."
<<endl<<"1. Insert Order"
<<endl<<"2. Delete Order"
<<endl<<"3. Check Order"
<<endl<<"4. Exit"
<<endl<<"Your Choice: ";
cin>>choice;
//customer task
switch(choice){
case 1: c.enterOrder();
break;
case 2: c.deleteorder();
break;
case 3:
cout<<"\nYOUR ODER."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
c.stackTop();
cout<<endl;
break;
}
}while(choice != 4);
};
//SELLER CLASS
class Seller{
string id;
string password;
public:
customerNode *backPtr, *frontPtr;
//constructor seller
Seller(){
id = "12345";
password = "12345";
backPtr = NULL;
frontPtr = NULL;
}
//check queue empty
bool queueEmpty(){
return (backPtr == NULL && frontPtr == NULL);
}
//display all nodes in stack
void getStack(Customer &c){
customerNode *check;
if(!c.isEmpty()){
check = c.head;
while(check!=NULL){
cout<<endl<<
setw(10)<<left<<check->id
<<setw(20)<<left<<check->name
<<setw(10)<<left<<check->phoneNum
<<setw(20)<<left<<check->college
<<setw(10)<<left<<check->quantity
<<setw(5)<<left<<check->size;
check=check->next;
}
}
else{
cout<<endl<<"Stack is an underflow.";
getch();
}
}
//will trigger getStack()
void displayStack(Customer &c){
cout<<"\nCUSTOMER FULL ORDERLIST."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
getStack(c);
cout<<endl;
}
//display all nodes in queue
void getQueue(){
customerNode *check;
if(!queueEmpty()){
check = frontPtr;
while(check!=NULL){
cout<<endl<<
setw(10)<<left<<check->id
<<setw(20)<<left<<check->name
<<setw(10)<<left<<check->phoneNum
<<setw(20)<<left<<check->college
<<setw(10)<<left<<check->quantity
<<setw(5)<<left<<check->size;
check=check->next;
}
}
else{
cout<<endl<<"Stack is an underflow.";
getch();
}
}
//display node in queue
void displayQueue(){
cout<<"\nSORTED CUSTOMER FULL ORDERLIST."<<endl<<
setw(10)<<left<<"ID"
<<setw(20)<<left<<"NAME"
<<setw(10)<<left<<"CONTACT"
<<setw(20)<<left<<"COLLEGE"
<<setw(10)<<left<<"QUANTITY"
<<setw(5)<<left<<"SIZE";
getQueue();
cout<<endl;
}
//insert queuee
void insertQueue(customerNode *newNode){
if(!queueEmpty()){
newNode->next = NULL;
backPtr->next = newNode;
backPtr = newNode;
cout<<endl<<"inserted node into queue."<<endl;
}
else{
frontPtr = newNode;
backPtr = newNode;
}
}
void sortOrder(Customer &c){
for(customerNode *node = c.head; node!=NULL; node = node->next){
if(node->college == "kdoj"){
cout<<endl<<node->college;
insertQueue(node);
}
}
for(customerNode *node = c.head; node!=NULL; node = node->next){
if(node->college == "kdse"){
cout<<endl<<node->college;
insertQueue(node);
}
}
displayQueue();
}
};
//seller main menu
void sellerMenu(Customer &c, Seller &s){
int choice;
do{cout
<<endl<<"SELLER MENU"
<<endl<<"Please choose your task."
<<endl<<"1. Display OrderList"
<<endl<<"2. Sort OrderList"
<<endl<<"4. Exit"
<<endl<<"Your Choice: ";
cin>>choice;
//choose task
switch(choice){
case 1: s.displayStack(c);
break;
case 2: s.sortOrder(c);
break;
case 3:
break;
}
}while(choice != 4);
}
void menu(Customer &c, Seller &s){ //main menu function
int choice;
do{
cout
<<endl<<"Welcome to HotspotPrinting!"
<<endl<<"Please choose your role."
<<endl<<"1. Customer"
<<endl<<"2. Seller"
<<endl<<"3. Exit"
<<endl<<"Your Choice: ";
cin>>choice;
switch(choice){
case 1: customerMenu(c);
break;
case 2: sellerMenu(c,s);
break;
}
}while(choice != 3);
}
int main()
{
Customer c;
Seller s;
menu(c,s);
cout<<endl<<"Thank You For Using Our Service :)!";
return 0;
}
your problem is that customer node records exist in two containers, your queue and a list. But you only have one next pointer, when you insert a customer into the queue you set its next pointer to null, this destroys the linkage in the customer list.
If you need to have the same object in 2 containers then use shared_ptr in standard containers, like std::list and std::queue

Debugging C++ file

I am getting this error main.cpp:23:5: error: ‘Department’ does not name a type
/*
* File: main.cpp
* Author: anonymous
*
* Created on May 11, 2015, 9:44 PM
*/
#include <cstdlib>
#include <iostream>
using namespace std;
class College{
public :
string name; //for name of the college
int numOfColleges;
int numDepartments; //number of departments in this college
Department* dep; //this will point to the department in this college
College* next; //the next pointer to point to the next college
College(){
name =" ";
numDepartments=0 ;
dep = NULL;
next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){
cout<<name<<"\n";
}
};
class Department
{
public:
string name;
int numOfStudents;
Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
void Print(){cout<<name<<" "<<numOfStudents;}
};
void AddCollege(College *head)
{
string n;
int numD;
cout<<"Enter the name of the College : ";
cin>>n;
cout<<"Enter the number of Departments : ";
cin>>numD;
College* tmp = new College(n,numD);
if(!head)
{
head=tmp;
return;
}
College * t=head;
while(t->next) t=t->next;
t->next=tmp;
cout<<"college added";
}
void DeleteCollege(College*&head)
{
string name;
cout<<"enter name of college:";
cin>>name;
if((!head)||(!head->next && head->name!=name))
{
cout<<"could not find "<<name<<" in the list\n"; return;
}
if(head->next && head->name==name)
{
College *tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
return;
}
College* tmp = head;
College* t;
while(tmp->next)
{
if(tmp->name==name)
{
College* tmp = head;
head = head->next;
delete tmp;
tmp->next=NULL;
}
if(tmp->next->name == name)
{
t = tmp->next;
tmp->next = tmp->next->next;
delete t;
return;
}
tmp=tmp->next;
}
cout<<"could not find "<<name<<" in the list\n";
};
/*Print the list of colleges int the college head*/
void printColleges(College *head)
{
cout<<"ALL Colleges in Database : \n";
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
}
void AddDepartment(College *head)
{
if(!head)
{
cout<<"NO COLLEGES ADDED!!";
return;
}
string n,Dname;
int numS;
College* tmp = head;
College*tmp2 = tmp;
;
while(tmp)
{
cout<<"->"<<tmp->name;
tmp=tmp->next;
cout<<endl;
}
cout<<"Type a College name to Add a Department inside\n";
cin>>n;
cout<<"Enter Department name:";
cin>>Dname;
cout<<"Enter the number of students :";
cin>>numS;
while(n!=tmp->name)
{
tmp=tmp->next;
}
if(!tmp-> dep)
{
College * tmpD = new Department(Dname,numS);
Department*tmpDD = tmpD;
head=tmp;
return;
}
/*if(tmp->dep)
{
Department *tmp3 = tmp->dep->next;
t=tmp->dep;
dep->next=tmp3;
}*/
};
//void DeleteDepartment(College* head)
//{
//
//
// ;}
void PrintAll(College*head)
{
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
};
int main(int argc, char** argv) {
int choice;
College*h = NULL;
while(choice!=11){
cout<<"\n\nMenu\n";
cout<<"1: Add a new college in the list  \n";
cout<<"2: Delete a college from the list \n";
cout<<"3: Add a department in a college\n";
cout<<"4: Delete a department from a college.\n";
cout<<"5: Print all the colleges along with their departments\n ";
cout<<"6: Delete all the departments of a particular college \n";
cout<<"7: Delete all the colleges from the list.\n";
cout<<"8: Print the total number of students in a college.\n";
cout<<"9: Find and print the college that has the highest number of students. \n";
cout<<"10: Find and print the department that has the highest number of students.\n";
cout<<"EXIT\n";
cin>>choice;
switch(choice)
{
case 1: AddCollege(*&h);
break;
case 2: DeleteCollege(*&h);
break;
case 3: printColleges(*&h);
break;
case 4:
AddDepartment(*&h);
break;
/* case 4:
DeleteDepartment(*&h);
break;
case 5:
PrintAll(*&h);
break;
case 6:
DeleteDepartment(*&h);
break;
case 7:
DeleteAllColleges(*&h);
break;
case 8:
NumOfStudentsInCollege(*&h);
break;
case 9:
HighestNumOfStudentsInCollege(*&h);
  break;
case 10:
HighestNumOfStudentsInDep(*&h);
break;
case 11:
cout<<"bye";
break;*/
default:
cout<<"\nInvalid menu choice";
}
}
}
Declare your Department class before it's called in College. (Move your Department declaration above your College declaration):
class Department
{
public:
string name;
int numOfStudents;
Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
void Print(){cout<<name<<" "<<numOfStudents;}
};
class College{
public :
string name; //for name of the college
int numOfColleges;
int numDepartments; //number of departments in this college
Department * dep; //this will point to the department in this college
College * next; //the next pointer to point to the next college
College(){
name =" ";
numDepartments=0 ;
dep = NULL;
next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){
cout<<name<<"\n";
}
};

What else can i do to improve this c++ Code

So I am trying to improve this code in c++. What this does it creates two classes: Student and Studentlist. Any suggestions on improving the linked list data structure here will be greatly appreciated.
#include <iostream>
using namespace std;
//declaring a class student
class Student
{
public:
char *RollNo;
Student *next;
//function student that includes arguments roll number and a pointer poniting to next node student
Student(char *rollNo, Student *Next)
{
this->RollNo = rollNo;
this->next = Next;
}
//fucntion to get roll number
char* getRollNo()
{
return RollNo;
}
//function to get the pointer to next node named student
Student *getNext()
{
return next;
}
void setNext(Student *aNode)
{
this->next = aNode;
}
};
//declareing a class StudentList
class StudentList
{
public:
Student *head;
// default constructor
StudentList()
{
head = NULL;
}
void Add(char *aRollNo)
{
Student *newStudent = new Student(aRollNo, NULL);
Student *temp = head;
if (temp != NULL)
{
while (temp->getNext() != NULL)
{
temp = temp->getNext();
}
temp->setNext(newStudent);
}
else
{
head = newStudent;
}
}
void display()
{
Student *temp = head;
if (temp == NULL)
{
cout << "no student data in the Student List" << endl;
return;
}
if (temp->getNext() == NULL)
{
cout << temp->getRollNo();
}
else
{
do
{
cout << temp->getRollNo() << " --next--> ";
temp = temp->getNext();
} while (temp != NULL);
cout << " end --> null" << endl;
}
}
};
main()
{
StudentList list;
list.Add("My Roll Number is 411\n");
list.display();
cout << "--------------------------------\n";
system("pause");
return 0;
}
The declaration of main() is not complete.
main()
See What is the proper declaration of main?
Also literal strings have a type of char const*. So your method call Add("XXX") has no matching point in the class. The closest you have is Add(char*) which does not match the const part.
Personally I would avoid using C-Strings in C++. You should look at using std::string to handle strings of characters it will avoid many problems.
while you are always add at the last i recomended you to replace your Add algorithm code with this
Student* Add(char *aRollNo,Student* last)
{
Student *newStudent = new Student(aRollNo, NULL);
Student *temp = last;
if (head == NULL){
head=newStudent;
temp=head;}
else
temp=temp->setNext(newStudent);
return temp;
}

Stacks and Queues

good day. i have a problem with my program. this is designed for small clinics to basically
record the patients information for an appointment. so here is my problem, at first it was
working but whenever im going to input infos for the 3rd patient(3rd node), after inputting
the name it will go on an error. this is a queue. `
#include <iostream>
using namespace std;
struct clnc_hrs
{
std:: string name;
std:: string symptms;
int age;
int cont_num;
clnc_hrs *next;
};
class List{
private:
clnc_hrs *rear;
clnc_hrs *front;
public:
List();
void BookAppointment();
void DeletePrevious();
void DisplaySchedule();
};
List::List(){
rear = NULL;
front = NULL;
}
void List::BookAppointment ()
{
std:: string N;
std:: string S;
int A;
int CN;
clnc_hrs *newnode = (clnc_hrs*)malloc(sizeof(clnc_hrs));
fflush(stdin);
cout<<"enter name of the patient:";
std::getline(cin, N);
newnode->name = N;
fflush(stdin);
cout<<"enter age of the patient:";
cin>>A;
newnode->age = A;
fflush(stdin);
cout<<"enter contact number of the patient:";
cin>>CN;
newnode->cont_num = CN;
fflush(stdin);
cout<<"enter the complaints or symptoms of the patient:";
std::getline(cin, S);
newnode->symptms = S;
newnode->next = NULL;
if(front == NULL)
{
fflush(stdin);
front = newnode;
fflush(stdin);
}
else
{
fflush(stdin);
rear->next = newnode;
fflush(stdin);
}
rear = newnode;
}
void List::DisplaySchedule ()
{
clnc_hrs *disp = (clnc_hrs*)malloc(sizeof(clnc_hrs));
disp = front;
if(front == NULL)
{
cout<<"there's no appointment for today"<<endl;
}
else
{
while(disp != NULL)
{
cout<<endl;
cout<<"name:"<<disp->name<<endl;
cout<<"age:"<<disp->age<<endl;
cout<<"contact number:"<<disp->cont_num<<endl;
cout<<"symptoms/complaints:"<<disp->symptms<<endl;
cout<<endl;
disp = disp->next;
}
}
}
void List::DeletePrevious()
{
clnc_hrs *newnode = (clnc_hrs*)malloc(sizeof(clnc_hrs));
if(front == NULL)
{
cout<<"no appointments today"<<endl;
}
else
{
newnode = front;
front = front->next;
cout<<"The previous patient is: "<<endl;
cout<<newnode->name<<endl;
cout<<newnode->age<<endl;
cout<<newnode->cont_num<<endl;
cout<<newnode->symptms<<endl;
delete newnode;
}
}
int main ()
{
List list;
int ans;
while(true)
{
cout<<"press 1 for booking an appointment\npress 2 to delete previous patients info \npress 3 for display"<<endl;
cin>>ans;
if(ans == 1)
{
list.BookAppointment();
}
else if(ans == 2)
{
list.DeletePrevious();
}
else if(ans == 3)
{
list.DisplaySchedule();
}
}
system("pause");
}
`
i hope someone here can help me. i am using dev c++ 4.9.9.2
As you say this in not homework then just use STL - i.e. http://www.cplusplus.com/reference/queue/queue/
All the bits'n'bobs are done for you
Don't use malloc in a C++ program, it doesn't construct C++ objects correctly, use new instead.
clnc_hrs *newnode = new clnc_hrs();
You might have other errors but this is the first one.
cout<<"enter name of the patient:";
std::getline(cin, N);
newnode->name = N;
is not wrong, but just a bit wasteful. This is simpler
cout<<"enter name of the patient:";
std::getline(cin, newnode->name);
same for all you other inputs.
fflush(stdin);
is implementation defined behaviour. fflush only has defined behaviour on output streams not input streams. You seem to be using fllush(stdin); as some sort of magic incantation that you hope will solve your problems. Avoid that kind of thinking. Learn what the code you write really does. In this case you should remove all calls to fflush(stdin);.

How To Modify Data In a c++ Linked List

I have made a Student record system in c++ using the concept of linked list. The program will take the student id, name and marks for 3 different exams. Then it will calculate the aggregate percentage based on those exams marks and display the relevant message according to the aggregate percentage. The program is running without any errors.
But I want student id to be unique. For example, if a user inputs a student id which has been already assigned to previous student than it will show a message that "please enter a different id."
How would I do that. Any help will be appreciated.
struct student
{
int id;
char name[MAX];
string status;
double aggr;
int matric_marks, inter_marks, entryTest_marks;
};
struct node
{
struct student *data;
struct node *next;
node()
{
data = 0;
next = NULL;
}
};
struct student *readStudent()
{
clearWindow();
struct student *stdnt = new student;
gotoxy(33,8);cout<<"Student ID: ";
while(!(cin >> stdnt->id) || cin.peek() != '\n')
{
char ch;
cin.clear();
cout << "sorry";
while(cin.get(ch) && ch != '\n');
}
cin.ignore();
gotoxy(33,10);cout<<"Student Name: ";
cin.getline(stdnt->name, 50);
gotoxy(33,12);cout<<"Enter Matriculation Marks: ";
cin>>(stdnt->matric_marks);
gotoxy(33,14);cout<<"Enter Intermediate Marks: ";
cin>>(stdnt->inter_marks);
gotoxy(33,16);cout<<"Enter Entry Test Marks: ";
cin>>(stdnt->entryTest_marks);
stdnt->aggr = calculate_aggregiate(stdnt);
gotoxy(33,18);cout<<"Student Aggregate Marks: "<< stdnt->aggr;
if (stdnt->aggr >= 70)
{
gotoxy(33,20);cout<<"Student Registered In Electrical Engg";
}
else if (stdnt->aggr >= 60)
{
gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";
}
else if (stdnt->aggr >=50)
{
gotoxy(33,24);cout<<"Student Registered In Computer Science";
}
else
{
gotoxy(33,20);cout<<"Sorry! The Student Doesnt Qualify";
}
return stdnt;
}
struct node *createDatabase(int size)
{
int i = 0;
struct node *head = NULL;
struct node *last = NULL;
for(i = 0; i < size; i++)
{
struct student *stdnt = readStudent();
struct node * current = new node;
current->data = stdnt;
current->next = NULL;
if(last)
last->next = current;
else
head = current;
last = current;
}
return head;
}
struct node *InsertRecord(struct node *head)
{
struct node *record = new node;
struct student *stdnt = readStudent();
record->data=stdnt;
record->next=head;
return record;
}
double calculate_aggregiate(struct student *stud)
{
student *stdnt = stud;
double aggr;
aggr = stdnt->matric_marks * 10/100 + stdnt->inter_marks * 50/100 +
stdnt->entryTest_marks * 40/100;
return aggr;
}
struct node *DeleteRecord(struct node *head)
{
clearWindow();
struct node *curr,*prev, *temp;
int tdata;
if(head==NULL)
{
gotoxy(33,10);cout<<"NO RECORDS TO DELETE......!";
}
else
{
gotoxy(33,10);cout<<"Enter Student ID To Be Deleted: ";
cin>>tdata;
prev=curr=head;
while((curr!=NULL)&&(curr->data->id!=tdata))
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
{
gotoxy(33,12);cout<<"The Requested ID Is Not Found...!";
}
else if(curr==head)
{
head=head->next;
gotoxy(33,12);cout<<"DATA DELETED....!";
}
else
{
prev->next=curr->next;
if(curr->next==NULL)
{
temp=prev;
}
gotoxy(33,12);cout<<"DATA DELETED....!"<<tdata;
}
delete(curr);
}
return head;
}
void ModifyRecord(struct node *head)
{
clearWindow();
int ch, sid;
struct node *current;
struct student *stdnt;
if (head==NULL)
{
gotoxy(33,8);cout<<"NO RECORD TO MODIFY..!";
}
else
{
gotoxy(33,8);cout<<"Enter Student ID To Modify: ";
cin>>sid;
current=head;
while((current!=NULL) && (current->data->id!=sid))
{
current=current->next;
}
if (current==NULL)
{
gotoxy(33,10);cout<<"The Requested ID is Not Found";
}
else if(current->data->id==sid)
{
gotoxy(33,10);cout<<"What Do You Want To Modify";
gotoxy(33,12);cout<<"1. Student's Name";
gotoxy(33,14);cout<<"2. Student's Matric Marks";
gotoxy(33,16);cout<<"3. Student's Intermediate Marks";
gotoxy(33,18);cout<<"4. Student's Entry Test Marks";
gotoxy(33,20);cout<<"Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1 : gotoxy(33,22);cout<<"Enter New Name: ";
cin.getline(current->data->name, 50);break;
case 2 : gotoxy(33,22);cout<<"Enter New Matric Marks: ";
cin>>(current->data->matric_marks);break;
case 3 : gotoxy(33,22);cout<<"Enter New Intermediate Marks: ";
cin>>(current->data->inter_marks);break;
case 4 : gotoxy(33,22);cout<<"Enter New Entry Test Marks: ";
cin>>(current->data->entryTest_marks);break;
current->data->aggr = current->data->matric_marks * 10/100 + current->data- >inter_marks * 50/100 +
current->data->entryTest_marks * 40/100;
}
gotoxy(33,24);cout<<"RECORD MODIFIED....!";
}
}
}
void SearchRecord(struct node *head)
{
clearWindow();
int s_id;
struct node *current;
if (head==NULL)
{
gotoxy(33,8);cout<<"NO RECORD TO SEARCH..!";
}
else
{
gotoxy(33,8);cout<<"Enter Student ID To Be Searched: ";
cin>>s_id;
current=head;
while ((current!=NULL) && (current->data->id!=s_id))
{
current=current->next;
}
if (current==NULL)
{
gotoxy(33,10);cout<<"The Requested ID is Not Found";
}
else if (current->data->id==s_id)
{
gotoxy(33,10);cout<<"Student ID: "<<current->data->id;
gotoxy(33,12);cout<<"Student Name: "<<current->data->name;
gotoxy(33,14);cout<<"Student Matric Marks: "<<current->data->matric_marks;
gotoxy(33,16);cout<<"Student Intermediate Marks: "<<current->data - >inter_marks;
gotoxy(33,18);cout<<"Student Entry Test Marks: "<<current->data- >entryTest_marks;
gotoxy(33,20);cout<<"Student Aggregate Marks: "<<current->data->aggr;
if (current->data->aggr >= 70)
{
gotoxy(33,22);cout<<"Student Registered In Electrical Engg";
}
else if (current->data->aggr >= 60)
{
gotoxy(33,22);cout<<"Student Registered In Mechanical Engg";
}
else if (current->data->aggr >=50)
{
gotoxy(33,22);cout<<"Student Registered In Computer Science";
}
else
{
gotoxy(33,22);cout<<"Sorry! The Student Doesnt Qualify";
}
}
}
}
void print(struct node *head)
{
clearWindow_p();
struct node *current = head;
if (head == NULL)
{
gotoxy(33,8);cout<<"No Student Registered Yet......!";
}
else
{
cout<<"\n\t\t\t\t\tSTUDENTS STATISTICS";
while(current)
{
struct student *stdnt = current->data;
cout<<"\n\t\t\t\t\t-------------------------------- ";
cout<<"\n\t\t\t\t\tStudent ID :"<<stdnt->id;
cout<<"\n\t\t\t\t\tStudent Name :"<<stdnt->name;
cout<<"\n\t\t\t\t\tMatric Marks :"<<stdnt->matric_marks;
cout<<"\n\t\t\t\t\tIntermediate Marks :"<<stdnt->inter_marks;
cout<<"\n\t\t\t\t\tEntry Test Marks: "<<stdnt->entryTest_marks;
cout<<"\n\t\t\t\t\tAggregate: "<<stdnt->aggr;
if (stdnt->aggr >= 70)
{
cout<<"\n\t\t\t\t\tStudent Registered In Electrical Engg";
}
else if (stdnt->aggr >= 60)
{
cout<<"\n\t\t\t\t\tStudent Registered In Mechanical Engg";
}
else if (stdnt->aggr >=50)
{
cout<<"\n\t\t\t\t\tStudent Registered In Computer Science";
}
else
{
cout<<"\n\t\t\t\t\tSorry! The Student Doesnt Qualify";
}
current=current->next;
}
cout<<"\n\t\t\t\t\t--------------------------------";
}
}
int compareStudents(struct student *left, struct student *right)
{
return strcmp(left->name, right->name);
}
struct node *sort(struct node *head)
{
//using bubble sort
int swapped = 0;
do
{
swapped = 0;
struct node *current = head;
struct node *previous = NULL;
while(current && current->next)
{
if(compareStudents(current->data, current->next->data) > 0)
{
//swap here
struct node *next = current->next;
if(previous)
{
previous->next = next;
}
else
{
head = next;
}
current->next = next->next;
previous = next;
previous->next = current;
swapped = 1;
}
else
{
previous = current;
current = current->next;
}
}
} while(swapped);
return head;
}
int main()
{
system("color f0");
window();
SetColor(28);
int total,i,choice;
int x = 2;
struct node *head;
head=NULL;
do
{
menu:
gotoxy(x, 8);cout<<"1.Create a Record File";
gotoxy(x, 10);cout<<"2.Insert Student's Record";
gotoxy(x, 12);cout<<"3.Modify Student's Record";
gotoxy(x, 14);cout<<"4.Delete Student's Record";
gotoxy(x, 16);cout<<"5.Search Student's Record";
gotoxy(x, 18);cout<<"6.Print All Records";
gotoxy(x, 20);cout<<"7.Sort According To Names";
gotoxy(x, 22);cout<<"8.Clear The Screen";
gotoxy(x, 24);cout<<"9.Exit";
gotoxy(x, 26);cout<<"Enter your choice";
cout<<" [ ]\b\b";
cin>>choice;
switch(choice)
{
case 1:
clearWindow();
gotoxy(33, 8);cout<<"How Many Students Do You Want To Register: ";
cin>>total;
head=createDatabase(total);
break;
case 2:
head=InsertRecord(head);
break;
case 3:
ModifyRecord(head);
break;
case 4:
DeleteRecord(head);
break;
case 5:
SearchRecord(head);
break;
case 6:
print(head);
break;
case 7:
sort(head);
print(head);
break;
case 8:
main();
break;
default: gotoxy(33,8);cout<<"INVALID OPTION";
}
}while(choice!=9);
getch();
return 0;
}
Since you are using C++, you should take advantage of the built-in types. If there is only one student with a given ID, use the std::map<> template. For example:
struct student
{
char name[MAX];
string status;
double aggr;
int matric_marks, inter_marks, entryTest_marks;
};
map<int, student> students;
then, to traverse students:
for (auto i = students.begin(); i != students.end(); ++i) {
int studentId = i->first;
student& student = i->second;
student.aggr = ...
}
(auto is C++11, not all compilers enable it by default IIRC. See here.)
If You know the range of student ids , and it is within a feasible range of memory , you can use a UsedId[] array.
If the range is too big too be held , use a Hashmap ,(MAP in c++) .
I would probably implement something called FindRecordByID.
struct student *FindRecordByID(struct node *head, int id) {
for(; head != NULL; head = head->next)
if (head->data->id == id)
return head->data;
return NULL;
}
Then when you do your create_new_student routine, you could use that to verify that the given ID is not already used.