-
parse errors
Hey everyone, Really struggling with a piece of code, I need to input data from a keyboard as a string and save it so that I can print it back to the screen later.
At the moment I keep getting a "parse error" after having checked thorugh 2 C++ books and the internet I'm none the wiser.
Any help please?
Craig Harper
-
-
This is his code:
Code:
//Cheers for any help CH
// Craig harper Code for student/person class
// 12/08/2003
// Problem lines are marked with comments
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
class Person
{
public:
Person() {
};
~Person() {
};
unsigned long int getLibraryNumber();
void SetLibraryNumber (unsigned long int);
char GetSurname();
void SetSurname(char);
char GetFirstName();
void SetFirstName(char);
char GetTitle();
void SetTitle(char);
protected:
unsigned long int LibraryNumber;
char Surname[50];
char Forename[30];
char Title[5];
};
class Student
{
public:
Student() {
};
~Student() {
};
char GetDepartment();
void SetDepartment(char);
int GetCourse();
void SetCourse(int);
protected:
char Department[30];
int Course;
};
void doEnterStudents();
void doEnterStaff();
void doPrintRecordsStudents();
void doPrintRecordsStaff();
void doEnterStudents()
{
int Counter;
char Surname[50], Name[30], title[5], department[30];
int LibNo, course;
for (Counter =0; Counter <10; Counter++)
Student.counter = new Student ; //parse error before '.'
cout << "What is your library number?\n";
cin >> LibNo;
Student.SetLibraryNumber(LibNo); //parse error before '.'
cout << "What is your surname?\n";
cin.getline(Surname, 50, '\n');
Student.SetSurname(Surname); //parse error before '.'
cout << "What is your First Names?\n";
cin >> Name;
Student.SetFirstName(Name); //parse error before '.'
cout << "What is your Title?\n";
cin >> title;
Student.SetTitle(title); //parse error before '.'
cout << "what is your department?\n";
cin >> department;
Student[Counter].SetDepartment(Dept); //parse error before '.'
cout << "what is your Course Number?\n";
cin >> course;
Student[Counter].SetCourse(course);
system ("PAUSE");
}
void doEnterStaff()
{
cout << "the enter staff will go here";
system ("PAUSE");
return ;
}
void doPrintRecordsStaff()
{
cout << "Print Staff will go here";
system ("PAUSE");
return ;
}
void doPrintRecordsStudents()
{
cout << "Print students will go here";
system ("PAUSE");
return ;
}
int main(int argc, char *argv[])
{
int menuChoice;
cout << "Please chose your menu choice\n";
cout << "1: Enter The New Students\n";
cout << "2: Enter The New Staff\n";
cout << "3: Print All Staff\n";
cout << "4: Print All Students\n";
cout << "5: Exit this program\n";
cin >> menuChoice;
switch (menuChoice)
{
case 1: doEnterStudents;
case 2: doEnterStaff;
break;
case 3: doPrintRecordsStudents;
break;
case 4: doPrintRecordsStaff;
break;
case 5: cout << "Thankyou good bye";
break;
default:
cout << "Thats not in the menu";
break;
system ("pause");
int holdScreen;
std::cin >> holdScreen;
return 0;
}
}
-
first problem I see is here:
Code:
for (Counter =0; Counter <10; Counter++)
Student.counter = new Student ; //parse error before '.'
cout << "What is your library number?\n";
cin >> LibNo;
Student.SetLibraryNumber(LibNo); //parse error before '.'
cout << "What is your surname?\n";
cin.getline(Surname, 50, '\n');
Student.SetSurname(Surname); //parse error before '.'
cout << "What is your First Names?\n";
cin >> Name;
Student.SetFirstName(Name); //parse error before '.'
cout << "What is your Title?\n";
cin >> title;
Student.SetTitle(title); //parse error before '.'
cout << "what is your department?\n";
cin >> department;
Student[Counter].SetDepartment(Dept); //parse error before '.'
cout << "what is your Course Number?\n";
cin >> course;
Student[Counter].SetCourse(course);
system ("PAUSE");
The only thing that's part of the loop is
Student.counter = new Student ;
This means that you're throwing away 9 instances of Student. Plus, I don't even see where you're trying to delete them
-
Plus, there is no such thing as Student.counter. In addition, you're trying to use the class name Student as if it is an instance.
What you probably want is this:
Student student; //create an instance of Student called student
student.SetLibraryNumber(LibNo); // call a method
etc...
-
Any particular reason why the member variables of the classes are protected and not private ? You dont seem to use any inhertance so there shouldnt be any reason as far as I can see.
You should probably place your menu inside a do-while loop
Code:
do
{
cout << "Please chose your menu choice\n";
cout << "1: Enter The New Students\n";
cout << "2: Enter The New Staff\n";
cout << "3: Print All Staff\n";
cout << "4: Print All Students\n";
cout << "5: Exit this program\n";
cin >> menuChoice;
// more code here...
} while (menuChoice != 5);
I think FillYourBrain has pointed out some other stuff aswell.
-
Was there any point to even declaring your constructor and destructor?
Code:
Person() {
};
~Person() {
};
Those are just empty methods. Why not let the compiler do that for you? Or, better still, stick somethin in there which will be useful.
-
thanks but...
Cheers for the help, i'm trying to set up a small program that will allow a user to enter 10 students. Thats what I thought i would get in the "for" loop. and fib what did you mean about deleting something I wasn't trying to delete anything just store it in the memory so it can be printed later.
thanks again for your help
CH
-
Quote:
blahblah = new Student;
the "new" keyword is dynamic allocation in C++. Meaning you are grabbing a piece of memory from the operating system to use. "new" allocates. "delete" de-allocates. If you do not de-allocate, we have what is called a memory leak. naughty