0

I was making student attendance program in c++ which is not giving desired output due to 2 problems

  1. the display function is not working properly, the last data is displayed twice
  2. the main file that is attend2.txt is not deleted
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string>
#include<conio.h>
using namespace std;
class attend
{
    public:
    int idn;

    void getdata()
    {
        cout<<"ENTER THE ID NUMBER "<<endl;
        cin>>idn;
        ofstream outf("Attend2.txt",ios::app);
        outf<<idn<<endl;
        outf.close();
    }
    void checkdata()
    {
        int idc,k,m=0;
        cout<<"ENTER THE ID TO CHECK "<<endl;
        cin>>idc;
        ifstream fin("Attend2,txt");
        while(fin.eof()==0)
        {
            fin>>idn;
            if(idn==idc)
            m=1;
        }
        if(m==1)
            cout<<"RECORD FOUND "<<endl;
        else
            cout<<"RECORD NOT FOUND "<<endl;
    }
    void deletedata()
    {
        int idc,k;
        cout<<"ENTER THE ID TO DELETE "<<endl;
        cin>>idc;
        ifstream fin("Attend2.txt");
        while(fin.eof()==0)
        {
            fin>>idn;
            if(idc==idn)
            {
                 cout<<"INSIDE IF "<<endl;
            }
            else
            {
                 ofstream outf("Attend3.txt",ios::app);
                 outf<<idn<<endl;
                 cout<<"ELSE IS RUNNING "<<endl;

            }
       }
       remove("Attend2.txt");
       rename("Attend3.txt","Attend4.txt");
       remove("Attend4.txt");
    }
    void displaydata()
    {
        int k=0;
        ifstream fin("Attend2.txt");
        while(fin.eof()==0)
        {
            fin>>idn;
            cout<<"THE NUMBER IS      "<<idn<<endl;
            k++;
        }
        cout<<"TOTAL NUMBER OF STUDENTS ARE   "<<k<<endl;
   }
};
int main()
{

    int k=1,ch;
    attend obj;
    cout<<"******************************************************************************"<<endl;
    cout<<"                    WELCOME TO THE ATTENDANCE SHEET                           "<<endl;
    cout<<"******************************************************************************"<<endl;
    while(k==1)
    {
        system("cls");
        cout<<"******************************************************************************"<<endl;
        cout<<"                        PRESS 1 TO ADD DATA                                   "<<endl;
        cout<<"                        PRESS 2 TO CHECK THE DATA                             "<<endl;
        cout<<"                        PRESS 3 TO DELETE DATA                                "<<endl;
        cout<<"                        PRESS 4 TO DISPLAY DATA                               "<<endl;
        cout<<"                        PRESS 5 TO EXIT                                       "<<endl;
        cout<<"                        ENTER YOUR CHOICE                                     "<<endl;
        cin>>ch;
        switch(ch)
        {
             case 1:
                    obj.getdata();
                    cout<<"                   PRESS 1 TO CONTINUE                                      "<<endl;
                    cout<<"                   PRESS 2 TO EXIT                                          "<<endl; 
                    cout<<"                   ENTER YOUR CHOICE                                        "<<endl;
                    cin>>k;
                    break;
                    case 2:
                    obj.checkdata();
                    cout<<"                   PRESS 1 TO CONTINUE                                      "<<endl;
                    cout<<"                   PRESS 2 TO EXIT                                          "<<endl; 
                    cout<<"                   ENTER YOUR CHOICE                                        "<<endl;
                    cin>>k;
                    break;
        case 3:
                obj.deletedata();
                cout<<"                   PRESS 1 TO CONTINUE                                      "<<endl;
                cout<<"                   PRESS 2 TO EXIT                                          "<<endl; 
                cout<<"                   ENTER YOUR CHOICE                                        "<<endl;
                cin>>k;
                break;
        case 4:
                obj.displaydata();
                cout<<"                   PRESS 1 TO CONTINUE                                      "<<endl;
                cout<<"                   PRESS 2 TO EXIT                                          "<<endl; 
                cout<<"                   ENTER YOUR CHOICE                                        "<<endl;
                cin>>k;
                break;
            case 5:
                    exit(0);    
            default:
                    cout<<"                  WRONG CHOICE                                              "<<endl;
                    cout<<"                  PRESS 1 TO CONTINUE                                       "<<endl;
                    cout<<"                  PRESS 2 TO EXIT                                           "<<endl; 
                    cout<<"                  ENTER YOUR CHOICE                                         "<<endl;
                    cin>>k;
        }

}
}
Ðаn
  • 10,400
  • 11
  • 57
  • 90
  • 2
    Wow compiling for two different language standard revisions and one major language variant! You're making this too hard on yourself, sir. I recommend scaling back your ambitions. – user4581301 Feb 09 '20 at 18:45
  • `while(fin.eof()==0)` [needs to be rethought](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – user4581301 Feb 09 '20 at 18:46
  • Back up your code, then cut it back so that you are only attacking one bug at a time. This makes it easier to isolate the bug (and once isolated, the solution is often right around the corner) and prove that you really have solved the bug. Use [mcve] as inspiration. – user4581301 Feb 09 '20 at 18:49
  • If you think that i used 2 languages then sir please let me know in my comment section @user4581301 – KUSHAL UPADHYAY Feb 11 '20 at 17:30
  • Sir my code is working with `while(fin.eof()==0)` then why should i again think about it @user4581301 – KUSHAL UPADHYAY Feb 11 '20 at 17:37
  • That was me trying to be funny while warning you to watch how you use the question tags. Think of tags as AND not OR. Your initial set of tags included c++-cli, a set of extensions to the C++ language that make it so different to practically be a different language. You're also tagging C++14 and C++17. It is unusual to be using two different language standard revisions to write a program. Typically you would pick only the most recent of the two language standards. Occasionally a question contrasts standard revisions and requires more than one tag. – user4581301 Feb 11 '20 at 17:39
  • [Click the link.](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). while eof has several problems: It only tests for one--end of the file--of many possible fatal IO cases. Not checking the others results using invalid data from failed reads and probably an infinite loop as if the reading fails early, it will never reach the end of the file. – user4581301 Feb 11 '20 at 17:45
  • The most common problem is it is testing the validity of the read before reading data. You need to read data, test that data was successfully read, and then use the data. `while (!EOF)` tests that the end of the file hasn't been reached BEFORE reading data, not after. If there was no data to be read, the read failed and the EOF bit is set but the invalid result of the read was used anyway. THEN the test for EOF is re-run, too late o be of use in preventing the invalid read. – user4581301 Feb 11 '20 at 17:49
  • If your code works, it is only because the file ends exactly at the end of valid input. Add a space or a new line to the end of the file and your program will fail. – user4581301 Feb 11 '20 at 17:51

0 Answers0