Computer Science Canada

I cant find one error in my program

Author:  HolyDude [ Sat Nov 08, 2008 10:09 pm ]
Post subject:  I cant find one error in my program

Hi,

I am new to this board,
this is a small program of mine
and I compiled it in turbo c++,
my dev wasnt working.

I was resulted in 3 errors,
2 of which I correced by putting a curly bracked and ';' in the end.

I am left with one more error which says
Line Number 67 : Declaration syntax error

code:
#include<fstream.h>
#include<stdio.h>
#include<conio.h>

class Student{ int rollno;
               char name[30];
               float marks;
              public:
               void gdata();
               void pdata();
               void read_namemarks();
               int getrollno();
             };

void Student::gdata()
{
 cout<<"\n Enter the rollno :";
 cin>>rollno;
 cout<<"\nEnter the name :";
 gets(name);
 cout<<"\nEnter the marks:";
 cin>>marks;
}

void Student::pdata()
{
 cout<<"\n\nRoll number :"<<rollno;
 cout<<"\t\tName :"<<name;
 cout<<"\t\tMarks:"<<marks;
}

void Student::read_namemarks()
{
 cout<<"Enter name:";
 gets(name);
 cout<<"Enter marks:";
 cin>>marks;
}

int Student::getrollno()
{ return rollno;
}

void search(int rno)
{
 Student stud;
 int flag=0;
 fstream fin("Student.dat",ios::in|ios::binary);
 if(fin==NULL)
 { cout<<"\nFile does not exist";
   return ;
 }
 while(fin.read((char*)&stud,sizeof(Student)))
 {
  if(rno==stud.getrollno())
  {cout<<"\nStudent Details are as follows-->\n\n";
   stud.pdata();
   flag=1;
   break;
  }
 if(flag==0)
    cout<<"\n\nRollno does not exist";
 fin.close();
}

void Insert(Student st)
{
 Student stud;
 int p=0,c=1,i,rec;
 fstream.fin("Student.dat",ios::in|io::binary);
 while(fin.read((char*)&stud,sizeof(Student)))
 {
  p++;
  if(st.getrollno()<=stud.getrollno()
     break;
 }
 fin.close();
 if(st.getrollno()>stud.getrollno())
    p++;
 fin.open("Student.dat",ios::in|ios::binary);
 fstream fout("Temp.dat",ios::out|ios::binary);
 while(c<p)
 {
  fin.read((char*)&stud,sizeof(Student));
  fout.write((char*)&stud,sizeof(Student));
  c++;
 }
 fout.write((char*)&stud,sizeof(Student));
 while.(fin.read((car*)&stud,sizeof(Student))
 { fout.write((char*)&stud,sizeof(Student));
 }
 fin.close();
 fout.close();
 remove("Student.dat");
 rename("Temp.dat","Student.dat");
}

void Delete(int rno)
{
 Student stud;
 int flag=0;
 fstream fin("Student.dat",ios::in|ios::binary);
 if(fin==NULL)
   {cout<<"\nFile does not exist";
    return;
   }
 fstream fout("Temp.dat",ios::out|ios::binary);
 while(fin.read((char*)&stud,sizeof(Student)))
 {
  if(rno!=stud.getrollno())
  fout.write((char*)&stud,sizeof(Student));
  else
    flag=1;
 }
 if(flag==0)
   cout<<"\nRoll number does not exist";
 else
   cout<<"\nRecord deleted";
 fin.close()
 fout.close();
 remove("Student.dat");
 rename("Temp.dat","Student.dat");
}

void Modify(int rno)
{
 Student stud;
 int flag=0,rec=0;
 fstream fin("Student.dat",ios::in|ios::out|ios::binary);
 if(fin==NULL)
   {cout<<"\nFile does not exist";
    return;
   }
 while(fin.read((char*)&stud,sizeof(Student)))
 {
  rec++;
  if(rno==stud.getrollno())
    {cout<<"\nStudent details are as follows -->";
     stud.pdata();
     cout<<"\nEnter the new details -->\n";
     stud.read_namemarks();
     fin.seekg((rec-1)*sizeof(Student),ios::beg);
     fin.write((char*)&stud,sizeof(Student));
     flag=1;       
     break;
    }
 }
 if(flag==0)
   cout<<"\nRollno does not exist";
 else
   cout<<"\nRecord modified";
 fin.close();
}

void Display_Studentfile()
{
 Student stud;
 fstream fin("Student.dat",ios::in|ios::binary);
 if(fin++NULL)
   {cout<<"\nFile does not exist";
    return;
   }
 clrscr();
 cout<<"\n\nStudent Details are as follows-->";
 while(fin.read((char*)&stud,sizeof(Student)))
 {         stud,pdata();      }
 fin.close();
}

void main()
{
 Student stu;
 int choice,rn;
 do
 { clrscr();
   cout<<"\n1.Insert.";
   cout<<"\n2.Search.";
   cout<<"\n3.Delete.";
   cout<<"\n4.Modify.";
   cout<<"\n5.Display all";
   cout<<"\nEnter your choice...";
   cin>>choice;
   switch(choice)
   { case 1 :cout<<"\nEnter details of student record to be added -->\n";
             stu.gdata();
             insert(stu);
             break;
     case 2 :cout<<"\nEnter the roll number to be searched:";
             cin>>rn;
             Search(rn);
             break;
     case 3 :cout<<"\nEnter the roll number to be deleted:";
             cin>>rn;
             Delete(rn);
             break;
     case 4 :cout<<"\nEnter the roll number to be modified:";
             cin>>rn;
             Modify(rn);
             break;
     case 5 :Display_Studentfile();
             break;
   }
   getch();
 }while(choice>=1&&choice<=5);
};
};






That is my program,
It would be nice if anyone of you could
find the error and correct it and paste it.
Thank you.

Author:  OneOffDriveByPoster [ Sat Nov 08, 2008 10:18 pm ]
Post subject:  Re: I cant find one error in my program

You are missing a } somewhere.

Author:  HolyDude [ Sat Nov 08, 2008 10:26 pm ]
Post subject:  RE:I cant find one error in my program

Thats what am trying to find out Sad

Author:  OneOffDriveByPoster [ Sat Nov 08, 2008 11:05 pm ]
Post subject:  Re: RE:I cant find one error in my program

HolyDude @ Sat Nov 08, 2008 10:26 pm wrote:
Thats what am trying to find out Sad
Seriously, it is simple enough that you can find it easily.

Author:  gitoxa [ Sat Nov 08, 2008 11:27 pm ]
Post subject:  RE:I cant find one error in my program

Quote:
Line Number 67 : Declaration syntax error

This is a large clue. Razz
Actually, after going through it, there is another problem in the program that presents itself after you fix the current mistake you have. I'd post it but that'd be giving away your current problem.

Author:  Saad [ Sat Nov 08, 2008 11:38 pm ]
Post subject:  Re: I cant find one error in my program

One thing I want to say is this is why a indentation really helps in spotting these types errors. The indentation can make it much easier to spot where the error is since indentation is probably wrong starting there. If you can get a tool that will indent source code or just keep properly indenting code, it will make finding errors like these very easy to spot. While I can see you are indenting perhaps you should look over where braces match. Just my 2c Smile
The tool I use for indenting code is Astyle which works for C++ and some others.


Here's a slightly more indented version to show you why it makes it easier.


code:

#include<fstream.h>
#include<stdio.h>
#include<conio.h>

class Student {
    int rollno;
    char name[30];
    float marks;
public:
    void gdata();
    void pdata();
    void read_namemarks();
    int getrollno();
};

void Student::gdata() {
    cout << "\n Enter the rollno :";
    cin >> rollno;
    cout << "\nEnter the name :";
    gets(name);
    cout << "\nEnter the marks:";
    cin >> marks;
}

void Student::pdata() {
    cout << "\n\nRoll number :" << rollno;
    cout << "\t\tName :" << name;
    cout << "\t\tMarks:" << marks;
}

void Student::read_namemarks() {
    cout << "Enter name:";
    gets(name);
    cout << "Enter marks:";
    cin >> marks;
}

int Student::getrollno() {
    return rollno;
}

void search(int rno) {
    Student stud;
    int flag = 0;
    fstream fin("Student.dat", ios::in | ios::binary);
    if (fin == NULL) {
        cout << "\nFile does not exist";
        return ;
    }
    while (fin.read((char*)&stud, sizeof(Student))) {
        if (rno == stud.getrollno()) {
            cout << "\nStudent Details are as follows-->\n\n";
            stud.pdata();
            flag = 1;
            break;
        }
        if (flag == 0)
            cout << "\n\nRollno does not exist";
        fin.close();
    }

    void Insert(Student st) {
        Student stud;
        int p = 0, c = 1, i, rec;
        fstream.fin("Student.dat", ios::in | io::binary);
        while (fin.read((char*)&stud, sizeof(Student))) {
            p++;
            if (st.getrollno() <= stud.getrollno()
                    break;
                }
            fin.close()
                ;
        if (st.getrollno() > stud.getrollno())
            p++;
        fin.open("Student.dat", ios::in | ios::binary);
        fstream fout("Temp.dat", ios::out | ios::binary);
        while (c < p) {
            fin.read((char*)&stud, sizeof(Student));
            fout.write((char*)&stud, sizeof(Student));
            c++;
        }
        fout.write((char*)&stud, sizeof(Student));
        while.(fin.read((car*)&stud, sizeof(Student)) {
                   fout.write((char*)&stud, sizeof(Student));
               }
               fin.close();
               fout.close();
               remove
                   ("Student.dat");
                   rename("Temp.dat", "Student.dat");
               }

           void Delete(int rno) {
               Student stud;
               int flag = 0;
               fstream fin("Student.dat", ios::in | ios::binary);
               if (fin == NULL) {
                   cout << "\nFile does not exist";
                   return ;
               }
               fstream fout("Temp.dat", ios::out | ios::binary);
               while (fin.read((char*)&stud, sizeof(Student))) {
                   if (rno != stud.getrollno())
                       fout.write((char*)&stud, sizeof(Student));
                   else
                       flag = 1;
               }
               if (flag == 0)
                   cout << "\nRoll number does not exist";
               else
                   cout << "\nRecord deleted";
               fin.close()
               fout.close();
               remove
                   ("Student.dat");
               rename("Temp.dat", "Student.dat");
           }

           void Modify(int rno) {
               Student stud;
               int flag = 0, rec = 0;
               fstream fin("Student.dat", ios::in | ios::out | ios::binary);
               if (fin == NULL) {
                   cout << "\nFile does not exist";
                   return ;
               }
               while (fin.read((char*)&stud, sizeof(Student))) {
                   rec++;
                   if (rno == stud.getrollno()) {
                       cout << "\nStudent details are as follows -->";
                       stud.pdata();
                       cout << "\nEnter the new details -->\n";
                       stud.read_namemarks();
                       fin.seekg((rec - 1)*sizeof(Student), ios::beg);
                       fin.write((char*)&stud, sizeof(Student));
                       flag = 1;
                       break;
                   }
               }
               if (flag == 0)
                   cout << "\nRollno does not exist";
               else
                   cout << "\nRecord modified";
               fin.close();
           }

           void Display_Studentfile() {
               Student stud;
               fstream fin("Student.dat", ios::in | ios::binary);
               if (fin++NULL) {
                   cout << "\nFile does not exist";
                   return ;
               }
               clrscr();
               cout << "\n\nStudent Details are as follows-->";
               while (fin.read((char*)&stud, sizeof(Student))) { stud, pdata(); }
               fin.close();
           }

           void main() {
               Student stu;
               int choice, rn;
               do {
                   clrscr();
                   cout << "\n1.Insert.";
                   cout << "\n2.Search.";
                   cout << "\n3.Delete.";
                   cout << "\n4.Modify.";
                   cout << "\n5.Display all";
                   cout << "\nEnter your choice...";
                   cin >> choice;
                   switch (choice) {
                       case 1 :
                       cout << "\nEnter details of student record to be added -->\n";
                       stu.gdata();
                       insert(stu);
                       break;
                       case 2 :
                       cout << "\nEnter the roll number to be searched:";
                       cin >> rn;
                       Search(rn);
                       break;
                       case 3 :
                       cout << "\nEnter the roll number to be deleted:";
                       cin >> rn;
                       Delete(rn);
                       break;
                       case 4 :
                       cout << "\nEnter the roll number to be modified:";
                       cin >> rn;
                       Modify(rn);
                       break;
                       case 5 :
                       Display_Studentfile();
                       break;
                   }
                   getch();
               } while (choice >= 1 && choice <= 5);
           };
};



Hopefully you'll get it.

Author:  wtd [ Sun Nov 09, 2008 12:19 am ]
Post subject:  RE:I cant find one error in my program

It also helps if you use standard C++ instead of this bastardized C stuff.

Also, if you have forward declared your functions, then your main function does not need to come at the end of the program. In fact, it probably shouldn't.

Author:  HolyDude [ Sun Nov 09, 2008 4:34 am ]
Post subject:  Re: RE:I cant find one error in my program

gitoxa @ Sun Nov 09, 2008 9:57 am wrote:
Quote:
Line Number 67 : Declaration syntax error

This is a large clue. Razz
Actually, after going through it, there is another problem in the program that presents itself after you fix the current mistake you have. I'd post it but that'd be giving away your current problem.


I just want this program working, at any adjustments made..
I dont know why, but I cant solve this error,
please help...

Author:  md [ Sun Nov 09, 2008 11:18 am ]
Post subject:  RE:I cant find one error in my program

Here is the list of things you need to fix
1) wrong includes
2) it seems the standard these days is to teach "using namespace ...", which apparently you mean to do but do not do.
3) only after fixing both those will it become apparent: (after compiling Saad's code, with modifications for previous two points - no difference in line number)

Quote:
text.cpp: In function `void search(int)`:
text.cpp:63: error: a function-definition is not allowed here before `{` token


Incidentally, that line is
c++:
    void Insert(Student st) {

Author:  HolyDude [ Sun Nov 09, 2008 1:07 pm ]
Post subject:  Re: I cant find one error in my program

code:
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>



class Student
{
    int rollno;
    char name[50];
    float marks;
    public:
    void gdata();
    void pdata();
    void read_namemarks();
    int getrollno();
};

void Student::gdata()
{
 cout<<"\n Enter the rollno :";
 cin>>rollno;
 cout<<"\nEnter the name :";
 cin.getline(name,30);
 getchar();
 cout<<"\nEnter the marks:";
 cin>>marks;
 getchar();
}

void Student::pdata()
{
 cout<<"\n\nRoll number :"<<rollno;
 cout<<"\t\tName :"<<name;
 cout<<"\t\tMarks:"<<marks;
}

void Student::read_namemarks()
{
 cout<<"Enter name:";
 gets(name);
 cout<<"Enter marks:";
 cin>>marks;
}

int Student::getrollno()
{ return rollno;
}

void search(int rno)
{
 Student stud;
 int flag=0;
 fstream fin("Student.dat",ios::in|ios::binary);
 if(fin==NULL)
 { cout<<"\nFile does not exist";
   getchar();
   return ;
 }
 while(fin.read((char*)&stud,sizeof(Student)))
 {
  if(rno==stud.getrollno())
  {cout<<"\nStudent Details are as follows-->\n\n";
   stud.pdata();
   flag=1;
   break;
  }
 if(flag==0)
    cout<<"\n\nRollno does not exist";
 fin.close();
}
}

void Insert(Student st)
{
       Student stud;
       int p=0,c=1,i,rec;

       fstream fin("Student.dat", ios::in | ios::binary);
         
       while(fin.read((char*)&stud,sizeof(Student)))
          {
              p++;
              if(st.getrollno()<=stud.getrollno())
                break;
          }
       fin.close();

       if(st.getrollno()>stud.getrollno())
         p++;

       fin.open("Student.dat",ios::in|ios::binary);
       fstream fout("Temp.dat",ios::out|ios::binary);
   
       while(c<p)
       {
           fin.read((char*)&stud,sizeof(Student));
           fout.write((char*)&stud,sizeof(Student));
           c++;
       }

       fout.write((char*)&stud,sizeof(Student));
     
       while(fin.read((char*)&stud,sizeof(Student)))
       {
          fout.write((char*)&stud,sizeof(Student));
       }
       fin.close();
       fout.close();
       remove("Student.dat");
       rename("Temp.dat","Student.dat");
}

void Delete(int rno)
{
 Student stud;
 int flag=0;
 fstream fin("Student.dat",ios::in|ios::binary);
 if(fin==NULL)
   {cout<<"\nFile does not exist";
    return;
   }
 fstream fout("Temp.dat",ios::out|ios::binary);
 while(fin.read((char*)&stud,sizeof(Student)))
 {
  if(rno!=stud.getrollno())
  fout.write((char*)&stud,sizeof(Student));
  else
    flag=1;
 }
 if(flag==0)
   cout<<"\nRoll number does not exist";
 else
   cout<<"\nRecord deleted";
 fin.close();
 fout.close();
 remove("Student.dat");
 rename("Temp.dat","Student.dat");
}

void Modify(int rno)
{
 Student stud;
 int flag=0,rec=0;
 fstream fin("Student.dat",ios::in|ios::out|ios::binary);
 if(fin==NULL)
   {cout<<"\nFile does not exist";
    return;
   }
 while(fin.read((char*)&stud,sizeof(Student)))
 {
  rec++;
  if(rno==stud.getrollno())
    {cout<<"\nStudent details are as follows -->";
     stud.pdata();
     cout<<"\nEnter the new details -->\n";
     stud.read_namemarks();
     fin.seekg((rec-1)*sizeof(Student),ios::beg);
     fin.write((char*)&stud,sizeof(Student));
     flag=1;       
     break;
    }
 }
 if(flag==0)
   cout<<"\nRollno does not exist";
 else
   cout<<"\nRecord modified";
 fin.close();
}

void Display_Studentfile()
{
 Student stud;
 fstream fin("Student.dat",ios::in|ios::binary);
 
 if(fin == NULL) //binary ++ error
   {
      cout<<"\nFile does not exist";
      return;
   }
 

 cout<<"\n\nStudent Details are as follows-->";
 while(fin.read((char*)&stud,sizeof(Student)))
 {         stud.pdata();      }
 fin.close();
}

int main()
{
 Student stu;
 int choice,rn;
 do
 {
   cout<<"\n             Student Record Management System ";
   cout<<"\n            **********************************";
   cout<<"\n\n\nWhat would you like to do? ";
   cout<<"\n\n1.Insert.";
   cout<<"\n2.Search.";
   cout<<"\n3.Delete.";
   cout<<"\n4.Modify.";
   cout<<"\n5.Display all";
   cout<<"\nEnter your choice : ";
   cin>>choice;
   switch(choice)
   { case 1 :cout<<"\nEnter details of student record to be added :\n";
             stu.gdata();
             Insert(stu);
             break;
     case 2 :cout<<"\nEnter the roll number to be searched :";
             cin>>rn;
             search(rn);
             break;
     case 3 :cout<<"\nEnter the roll number to be deleted :";
             cin>>rn;
             Delete(rn);
             break;
     case 4 :cout<<"\nEnter the roll number to be modified:";
             cin>>rn;
             Modify(rn);
             break;
     case 5 :Display_Studentfile();
             break;
   };
 
 }while((choice>=1)&&(choice<=5));
return 0;}



This is a more revised version which runs in dev C++

The problem is when I try to enter a new student details,it get messed up then.


: