C++/c++linked list
Expert: vijayan - 5/23/2009
QuestionQUESTION: my problem is still the insetStudent function,still many errors.and there is also an error in the upDateStudent function.i am greatly appreciated for all the help from you.
wen i compile the errors are:
-In constructors 'studentNode::studentNode char*,int,int,int,studentNode*)
-18:error:anachronistic old-style base class initializer
-18:error:unnamed initializer for 'studentNode',which has no base classes
-In function 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 3 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 4 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 5 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
In function 'void upDateStudent(studentNode*&,const char*,int,int,int)
-109:error:invalid conversion from 'const char* to char*
You are to implement the following functions :
void initializeList(studentNode*& head)
void insertStudent(studentNode*& head, char *no, int s1=0, int s2=0, int s3=0)
void updateStudent(studentNode*& head, const char *no, int s1,int s2, int s3)
void printStudentList(studentNode *s)
#include<iostream>
#include<fstream>
#include<conio.h>
#include <string>
using namespace std;
struct studentNode
{
string student_no;
char no[20];
int score1;
int score2;
int score3;
studentNode *next;
studentNode( const char* no, int s1=0 , int s2=0 , int s3=0 , studentNode* nxt=0 )
:student_no(no),score1(s1),score2(s2),score3(s3),(next) {}
double average() const { return ( score1 + score2 + score3 ) / 3.0 ; }
};
void initializeList(studentNode*& head);
void insertStudent( studentNode*& head, char *no, int s1=0, int s2=0, int s3=0);
void updateStudent(studentNode*& head, const char *no, int s1,int s2, int s3);
void printStudentList(studentNode *s);
int main()
{
ifstream inFile;
studentNode *head, *current=NULL;
inFile.open("test.txt",ios::in);
if (!inFile)
{
cerr << "Unable to open file test.txt";
exit(1); // call system to stop
}
studentNode *s;
inFile >> s->no;
inFile >> s->score1;
inFile >> s->score2;
inFile >> s->score3;
inFile.close();
return 0;
}
void initializeList(studentNode*& head)
{
head=NULL;//empty linked list
}
//To insert a node into the list
void insertStudent( studentNode*& head, char *no, int s1=0, int s2=0, int s3=0)
{
if(head == 0) // empty list
head = new studentNode(no, s1, s2, s3, 0);
else
{
double avg = ( s1 + s2 + s3 ) / 3.0 ; // average score
// search in list to determine correct place
studentNode* prev = head ;
studentNode* curr = head->next ;
while(prev != 0)
{
if( (curr == 0) || (curr->average() < avg))
{
// insert after prev, before curr
prev->next = new studentNode(no, s1, s2, s3, curr);
return ;
}
else
{
prev = curr ;
curr = curr->next ;
}
}
}
}
void updateStudent(studentNode*& head, const char* no, int s1, int s2, int s3)
{
// locate the student in the list
studentNode* prev = head ;
studentNode* curr = head->next ;
while( curr != 0 )
{
if( curr->student_no == no ) // is this the student?
{
// yes, remove the student from the list
prev->next = curr->next ;
delete curr ;
// and reinsert into the list in the right position
insertStudent( head, no, s1, s2, s3 ) ;
return ; // we are done
}
else // look in the next node for the student
{
prev = curr ;
curr = curr->next ;
}
}
}
void printStudentList(studentNode *s)
{
// for each student in the list
for(studentNode * s; s != 0 ; s = s->next )
{
// print out the information
cout << "The list contains :" << endl;
cout << endl;
cout << "Student No: " << s->student_no << "\tAverage Score: " << s->average() << '\n';
}
}
ANSWER: -In constructors 'studentNode::studentNode char*,int,int,int,studentNode*)
-18:error:anachronistic old-style base class initializer
-18:error:unnamed initializer for 'studentNode',which has no base classes
Modify
:student_no(no),score1(s1),score2(s2),score3(s3),(next) {}
To:
:student_no(no),score1(s1),score2(s2),score3(s3),next(nxt) {}
-63:error:default argument given for parameter 3 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 4 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 5 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
In function 'void upDateStudent(studentNode*&,const char*,int,int,int)
-109:error:invalid conversion from 'const char* to char*
Modify the first declaration of insertStudent() from
void insertStudent( studentNode*& head, char *no, int s1=0, int s2=0, int s3=0)
To:
void insertStudent( studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0)
And modify line 63 from:
void insertStudent( studentNode*& head, char *no, int s1=0, int s2=0, int s3=0)
To:
void insertStudent( studentNode*& head, const char *no, int s1, int s2, int s3)
---------- FOLLOW-UP ----------
QUESTION: i hv modify the code as you say.i still gt alot of errors.i do nt knw y.could you pls help mi.i am so greatly appreciated for all the help from you.
wen i compile the errors are:
-In function 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 3 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 4 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
-63:error:default argument given for parameter 5 of 'void insertStudent(studentNode*&,char*,int,int,int)
-25:error:after previous specification in 'void insertStudent(studentNode*&,char*,int,int,int)
-In function 'void insertStudent(studentNode*&,char*,int,int,int)
-66:error:no matchine function for call to 'studentNode::studentNode(const char*&,int&,int&,int&,int)
-17:note:candidates are:studentNode::studentNode(char*,int,int,int,studentNode*)<(near match>
-9:note:studentNode::studentNode(const studentNode&)
-81:error:no matching function for call to 'studentNode::studentNode(const char*&,int&,int&,int&,studentNode*&)
-17:note:candidates are:studentNode::studentNode(char*,int,int,int,studentNode*)<(near match>
-9:note:studentNode::studentNode(const studentNode&)
ANSWER: I repeat:
Modify the first declaration of insertStudent() from
void insertStudent( studentNode*& head, char *no, int s1=0, int s2=0, int s3=0)
To:
void insertStudent( studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0)
And modify line 63 from:
void insertStudent( studentNode*& head, char *no, int s1=0, int s2=0, int s3=0)
To:
void insertStudent( studentNode*& head, const char *no, int s1, int s2, int s3)
---------- FOLLOW-UP ----------
QUESTION: i hv already done it as you say.i still gt errors.i do nt knw y.
#include<iostream>
#include<fstream>
#include<conio.h>
#include <string>
using namespace std;
struct studentNode
{
string student_no;
char no[20];
int score1;
int score2;
int score3;
studentNode *next;
studentNode(char* no, int s1=0, int s2=0, int s3=0, studentNode* nxt=0)
:student_no(no),score1(s1),score2(s2),score3(s3),next(nxt){}
double average() const { return ( score1 + score2 + score3 ) / 3.0 ; }
};
void insertStudent( studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0);
void updateStudent(studentNode*& head, const char *no, int s1,int s2, int s3);
int main()
{
ifstream inFile;
studentNode *head, *current=NULL;
inFile.open("test.txt",ios::in);
if (!inFile)
{
cerr << "Unable to open file test.txt";
exit(1); // call system to stop
}
studentNode *s;
inFile >> s->no;
inFile >> s->score1;
inFile >> s->score2;
inFile >> s->score3;
inFile.close();
return 0;
}
//To insert a node into the list
void insertStudent(studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0)
{
if(head == 0) // empty list
head = new studentNode(no, s1, s2, s3, 0);
else
{
double avg = ( s1 + s2 + s3 ) / 3.0 ; // average score
// search in list to determine correct place
studentNode* prev = head ;
studentNode* curr = head->next ;
while(prev != 0)
{
if( (curr == 0) || (curr->average() < avg))
{
// insert after prev, before curr
prev->next = new studentNode(no, s1, s2, s3, curr);
return ;
}
else
{
prev = curr ;
curr = curr->next ;
}
}
}
}
void updateStudent(studentNode*& head, const char* no, int s1, int s2, int s3)
{
// locate the student in the list
studentNode* prev = head ;
studentNode* curr = head->next ;
while( curr != 0 )
{
if( curr->student_no == no ) // is this the student?
{
// yes, remove the student from the list
prev->next = curr->next ;
delete curr ;
// and reinsert into the list in the right position
insertStudent( head, no, s1, s2, s3 ) ;
return ; // we are done
}
else // look in the next node for the student
{
prev = curr ;
curr = curr->next ;
}
}
}
Answer#include<iostream>
#include<fstream>
// ************** modified ********************
// removed
// ************** modified ********************
#include <string>
using namespace std;
struct studentNode
{
string student_no;
char no[20];
int score1;
int score2;
int score3;
studentNode *next;
// ************** modified ********************
studentNode( const char* no, int s1=0, int s2=0, int s3=0, studentNode* nxt=0)
// ************** modified ********************
:student_no(no),score1(s1),score2(s2),score3(s3),next(nxt){}
double average() const { return ( score1 + score2 + score3 ) / 3.0 ; }
};
void insertStudent( studentNode*& head, const char *no, int s1=0, int s2=0, int s3=0);
void updateStudent(studentNode*& head, const char *no, int s1,int s2, int s3);
int main()
{
ifstream inFile;
// ************** modified ********************
// line deleted
// ************** modified ********************
inFile.open("test.txt",ios::in);
if (!inFile)
{
cerr << "Unable to open file test.txt";
// ************** modified ********************
return 1 ; // call system to stop
// ************** modified ********************
}
// ************** modified ********************
std::string no ;
int score1, score2, score3 ;
// ************** modified ********************
inFile >> no;
inFile >> score1;
inFile >> score2;
inFile >> score3;
inFile.close();
return 0;
}
//To insert a node into the list
// ************** modified ********************
void insertStudent(studentNode*& head, const char *no, int s1, int s2, int s3)
// ************** modified ********************
{
if(head == 0) // empty list
head = new studentNode(no, s1, s2, s3, 0);
else
{
double avg = ( s1 + s2 + s3 ) / 3.0 ; // average score
// search in list to determine correct place
studentNode* prev = head ;
studentNode* curr = head->next ;
while(prev != 0)
{
if( (curr == 0) || (curr->average() < avg))
{
// insert after prev, before curr
prev->next = new studentNode(no, s1, s2, s3, curr);
return ;
}
else
{
prev = curr ;
curr = curr->next ;
}
}
}
}
void updateStudent(studentNode*& head, const char* no, int s1, int s2, int s3)
{
// locate the student in the list
studentNode* prev = head ;
studentNode* curr = head->next ;
while( curr != 0 )
{
if( curr->student_no == no ) // is this the student?
{
// yes, remove the student from the list
prev->next = curr->next ;
delete curr ;
// and reinsert into the list in the right position
insertStudent( head, no, s1, s2, s3 ) ;
return ; // we are done
}
else // look in the next node for the student
{
prev = curr ;
curr = curr->next ;
}
}
}