Thread: Needs urgent help !!!

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    Question Needs urgent help !!!

    I've made a project in C++.
    Everything in it is working fine. It executes very well.
    Only only problem is in the file reading.
    It reads from the file but does match the input student roll number with the roll number that exists in the file already.

    Please have a look and please tell me whats the problem.

    The problem only is in the last function (i.e. Search). Its at the end on the code.
    I'd be very glad if anyone could help me.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <conio.h>
    #include <string>
    #include <fstream>
    #include <windows.h>
    
    
    
    using namespace std;
    
    
    /******************************************************************************************************/
    /************************************** PROTOTYPES OF FUNCTIONS ***************************************/
    /******************************************************************************************************/
    
    void menu();
    void studentInfo();
    void attendanceRecord();
    void disC();
    void hod();
    void arrangeMeeting();
    void filewrite();
    void search();
    
    
    /***************************************** END OF PROTOTYPES ******************************************/
    
    
    
    /******************************************************************************************************/
    /*********************************** DEFINITION OF VARIABLES ******************************************/
    /******************************************************************************************************/
        char *rollNumber= new char[100];
    
        char *studentName= new char[100];
    
        char *fathersName= new char[100];
    
        char *dob= new char[50];
    
        char *enrollmentDate= new char[50];
    
        char *batch= new char[20];
    
        char *department= new char[50];
    
        float semester[9][6]={100,100,100,100,100,100};
    
        int offences=1;
    
        int *natOffences= new int [100];
    
        int fine = 0 ;
        
        int sem;
    
        int warning;
    
        int appProcess[5][5]={0,0,0,0,0};
    
        int appcount=0;
    
        int *sick=new int [500];
    
        int *shrtatnd=new int[500];
    
        int *drop=new int[500];
    
        int sickcounter=0;
    
        int dropcounter=0;
    
        int shrtatndcounter=0;
        
        int criticality[4]={0,0,0,0};
    
        int attendancecheck=0;
    
    /***************************************** END OF DEFINITIONS *****************************************/
    
    
    
    
    /******************************************************************************************************/
    /****************************************** MAIN FUNCTION *********************************************/
    /******************************************************************************************************/
    
        
    
    main()
    {
        int opt;
    
        system("color 03");
        system("cls");
        cout<<"                             __  __   _ _   __     __     "<<endl;
        cout<<"                            /   /  \\ | | | |__  * |__     "<<endl;
        cout<<"                            \\__ \\__/ | | |  __| |  __|     "<<endl;
    
        cout<<"\n\n"<<setw(50)<<"Welcome to COMSiS\n"<<setw(60)<< "COMSATS Atd Student Management System";
        cout<<"\n\n********************************************************************************";
        cout<<"\n"<<setw(80)<<"Written by BILAL & AQIB.\n";
        cout<<"********************************************************************************\n";
        cout<<"ENTER CORRESPONDING NUMBER FOR DESIRED OPTION \n"<<endl;
        cout<<"1-View Existing Record\n2-Enter New Record"<<endl;
        cout<<"\n********************************************************************************\n=";
        cin>> opt;
    
        switch (opt)
        {
        case 1 :
            {
    
                cout<<"Enter Roll No. of Student: (For Example \"FA12-BSE-275\")\n";
                cin>>rollNumber;
    
                search();
                break;
            }
        case 2:
            {
                cout<<"Enter Roll No. of Student: (For Example \"FA12-BSE-275\")\n";
                cin>>rollNumber;
    
                menu();
            }
        }
    
    }
    
    /***************************************** END OF FUNCTION *******************************************/
    
    
    /******************************************************************************************************/
    /***************************************** MENU FUNCTION **********************************************/
    /******************************************************************************************************/
    void menu()
    {
        
        system("cls");
    
        int opt ;
        int x=0 ;
    
        cout<<setw(78)<<" ** COMSiS **" <<endl;
    
        do
        {
            system("cls");
            
            cout<<setw(78)<<" ** COMSiS **" <<endl;
    
            cout<<endl ;
            cout<<"ENTER CORRESPONDING NUMBER FOR DESIRED OPTION \n"<<endl;
            cout<<"1- Student Info \n2- Attendance Record \n3- Disciplinary Committee Records \n" << endl ;
            cin>> opt ;
    
    
            switch (opt)
            {
    
            case 1:
                {
                    studentInfo(); //FUNCTION CALL TO VIEW OR EDIT STUDENT INFO
                    break;
                }
            case 2:
                {
                    attendanceRecord(); //FUNCTION CALL TO VIEW OR EIDT ATTENDANCE RECORD OF STUDENT
                    break;
                }
            case 3:
                {
                    disC(); //FUNCTION CALL TO VIEW OR EDIT THE DISCIPLINARY RECORD OF STUDENT
                    break;
                }
            default:
                {
                    cout<<"Invalid option entered.\nPlease enter a valid option.\n\n";
                }
            }
    
        filewrite();    
    
        } while (x==0);
    
        
    }
    
    /***************************************** END OF FUNCTION *******************************************/
    
    
    
    
    
    /******************************************************************************************************/
    /************************************* STUDENT INFO FUNCTION ******************************************/
    /******************************************************************************************************/
    
    
    
    void studentInfo()
    {
        int opt ;
        
        system("cls");
        cout<<setw(78)<<" ** COMSiS **" <<endl;
    
        
        /* PROMPTING USER TO CHOOSE DESIRED ACTION */
    
    
        cout<<"Do you want to view or edit the information?\nPlease enter 1 to view and 2 to edit : "<<endl;
        cin>>opt;
    
        switch (opt)
        {
            case 1:
            {
    
                /* PRINTING ALL DATA TAKEN FROM THE USER FOR THE CURRENT STUDENT */
    
    
                cout<<"\nStudent's Full Name: "<<studentName;
                cout<<"\nFather's Full Name: "<<fathersName;
                cout<<"\nStudent's Date Of Birth (DD/MM/YY): "<<dob;
                cout<<"\nStudent's Date of Enrollment: "<<enrollmentDate;
                cout<<"\nBatch of Student: "<<batch;
                cout<<"\nDepartment of Student: "<<department;
                
    
                /*PRINTING SEMESTER WISE GPA OF STUDENT */
                if (sem>1)
    
                {
                    cout<<"\nStudent's Semester-wise GPA: ";
    
    
                    for (int i=1; i<sem; i++)
                    {
                        cout<<"\nGPA of Semester "<<i<<" is: ";
                    }
                    
                }
                
                cout<<"\nPlease enter any key to go back to the main menu."<<endl;
    
                getche();
    
                break;
            }
    
            case 2:
            {
                
                cin.ignore();
    
                /* TAKING ALL DATA FROM USER */
    
                cout<<"Enter Student's Full Name: ";
                cin.getline(studentName , 30 , '\n');
    
                cout<<"\nEnter Father's's Full Name: ";
                cin.getline( fathersName , 30 , '\n');            
        
                cout<<"\nEnter Student's Date Of Birth (DD/MM/YY): ";
                cin.getline( dob , 80 , '\n');    
            
                cout<<"\nEnter Student's Date of Enrollment: ";
                cin.getline( enrollmentDate , 80 , '\n');
            
                cout<<"\nEnter Batch of Student: " ;
                cin.getline( batch , 80 , '\n');
        
                cout<<"\nEnter Department of Student: " ;
                cin.getline( department, 80 , '\n');    
        
                cout<<"\nEnter current semester of the student: ";
                cin>>sem;
    
                /* TAKING SEMESTER WISE GPA FROM THE USER */
        
                if (sem>1)
                {
    
                    cout<<"\nEnter Student's Semester-wise GPA: ";
            
                    for (int i=1; i<sem; i++)
    
                        {
                            cout<<"\nEnter GPA of Semester "<<i<<": ";
    
                            cin>>semester[i][0];
                        }
                }
    
                /* RETURNING TO MAIN MENU AFTER THE COMPLETION OF TASK */
    
                cout<<"\n\nPress any key to go back to main menu"<<endl;
    
                getche();
    
                cout<<semester[0][0];
    
                break;
            
    
            }
    
            default:
                {
                    cout<<"\nPlease enter a valid option.\n";
                    getche();
                }
        
        
        }
    
    /***************************************** END OF FUNCTION *******************************************/
    
    
    
    
    /******************************************************************************************************/
    /*********************************** ATTENDANCE RECORD FUNCTION ***************************************/
    /******************************************************************************************************/
    
    
    
    }
    void attendanceRecord()
    {
        system("cls");
        cout<<setw(78)<<" ** COMSiS **" <<endl;
    
        int opt; 
    
        /* PROMPTING USER TO CHOOSE DESIRED ACTION */
        cout<<"Do you want to view or edit the information?\nPlease enter 1 to view and 2 to edit : "<<endl;
        cin>>opt;
    
        switch (opt)
        {
            case 1:
                {
                    
                    for ( int i=sem ; i<=sem ; i++ )
                    {
                            for (int j=1 ; j<=5 ; j++ )
                            {
                                    cout <<"\nPercentage Attendance In Course "<<j<<" Of Current Semester "<<i<<":"<<semester[i][j];
                            }
                    
                    }
    
                    cout<<"\nPlease enter any key to go back to the main menu."<<endl;
                    getche() ;
                    break;
                }
                    
            case 2:
                {
                    cout<<"Enter Percentage Attendence of Each Course" <<endl ;
    
                    cout<<"\nEnter Current Semester"<<endl ;
                    cin>> sem ;
    
                    for ( int i=sem ; i<=sem ; i++ )
                        {
                            for (int j=1 ; j<=5 ; j++ )
                            {
                                    cout <<"\nEnter Percentage Attendance In Course "<<j<<" Of Current Semester "<<i<<":";
                                    cin>> semester[i][j] ;
                            }
                        }
    
                    
                    break ;
    
                }    
                
                /* RETURNING TO MAIN MENU AFTER THE COMPLETION OF TASK */
    
                cout<<"\nPlease enter any key to go back to the main menu."<<endl;
                    getche();
        }
    }
    
    
    /***************************************** END OF FUNCTION *******************************************/
    
    
    
    /******************************************************************************************************/
    /************************************ DISCIPLINARY RECORD FUNCTION ************************************/
    /******************************************************************************************************/
    
    void disC()
    {
        system("cls");
        cout<<setw(78)<<" ** COMSiS **" <<endl;
    
        int opt; 
    
        /* PROMPTING USER TO CHOOSE DESIRED ACTION */
    
        cout<<"Do you want to view or edit the information?\nPlease enter 1 to view and 2 to edit : "<<endl;
        cin>>opt;
    
        switch (opt)
        {
            case 1:
                {
                    cout<<"\nNumber of Offences: "<<offences;
                    for (int i=0; i<offences; i++)
                    {
                        cout<<"\nNature of offence "<<i+1<<"is: ";
                        
                        switch (natOffences[i])
                        {
                            case 1 :
                            {
                                cout<<"\nSmoking\\Drugs\n";
                                cout<<" FINE : Rs 2000"<<endl ;
                                break ;
                            }
                            case 2 :
                            {
                                cout<<"\nCheating\n";
                                cout<<" FINE : Rs 2000"<<endl ;
                                break ;
                            }
                            case 3 :
                            {
                                cout<<"\nLoss of University Card\n";
                                cout<<" FINE : Rs 500"<<endl ;
                                break;
                            }
                            case 4 :
                            {
                                cout<<"\nFighting\n";
                                cout<<" FINE : Rs 2000"<<endl ;
                                break ;
                            }
                            case 5 :
                            {
                                cout<<"\nDamaging University Property\n";
                                cout<<" FINE : Rs 2000"<<endl ;
                                break ;
                            }
                            case 6 :
                            {
                                cout<<"\nStealing\n";
                                cout<<" FINE : Rs 2000"<<endl ;
                                break ;
                            }
                            case 7 :
                            {
                                cout<<"\nMisbehaviour with Faculty\n";
                                cout<<" FINE : Rs 2000"<<endl ;
                                break ;
                            }
                        }
                    }
    
                    cout<<"\nPress any key to go back to the main menu."<<endl;
    
                    getche();
                    
                    break;
    
                }
    
    
    
            case 2:
                {
        
                    cout<<"Enter Number of Offences: ";
                    cin>>offences;    
    
                    cout<<"\nPlease enter the corresponding number to the nature of offence\n1- Smoking\\Drugs\n2- Cheating\n3- Loss of university card\n4- Fighting\n5- Damaging university property\n6- Stealing\n7- Misbehaviour with faculty"<<endl ;
        
                    
        
                        for (int i=0; i<offences; i++)
                    {
                        cout<<"\n Enter nature of offence "<<i+1<<": ";
                        cin>>natOffences[i];
            
    
                    switch (natOffences[i])
                    {
                        case 1 :
                        {
                        cout<<" FINE : Rs 2000"<<endl ;
                        fine += 2000 ;
                        break ;
                    }
                    case 2 :
                    {
                        cout<<" FINE : Rs 2000"<<endl ;
                        fine += 2000 ;
                        break ;
                    }
                    case 3 :
                    {
                        cout<<" FINE : Rs 500"<<endl ;
                        fine += 500 ;
                        break ;
                    }
                    case 4 :
                    {
                        cout<<" FINE : Rs 2000"<<endl ;
                        fine += 2000 ;
                        break ;
                    }
                    case 5 :
                    {
                        cout<<" FINE : Rs 2000"<<endl ;
                        fine += 2000 ;
                        break ;
                    }
                    case 6 :
                    {
                        cout<<" FINE : Rs 2000"<<endl ;
                        fine += 2000 ;
                        break ;
                    }
                    case 7 :
                    {
                        cout<<" FINE : Rs 2000"<<endl ;
                        fine += 2000 ;
                        break ;
                    }
    
                    }
    
                        }
    
        cout<<"\nTOTAL FINE PAID: "<<fine<<endl;
    
    
                }
        }
        
        /* RETURNING TO MAIN MENU AFTER THE COMPLETION OF TASK */
        
        cout<<"\nPress any key to go back to the main menu"<<endl;
        getche();
    }
    
    /***************************************** END OF FUNCTION *******************************************/
    
    
    /******************************************************************************************************/
    /************************************** FILE WRITING FUNCTION *****************************************/
    /******************************************************************************************************/
    
    
    void filewrite()
    {
        ofstream input;
        input.open("bilalzaman.txt", ios::app);
    
    
    
        input<<rollNumber<<","<<studentName<<","<<fathersName<<","<<dob<<","<<enrollmentDate<<","<<batch<<","<<department<<","<<offences<<","<<fine<<","<<sem<<","<<warning<<",";
    
        for ( int i=1 ; i<2 ; i++)
        {
            for ( int j=0 ; j<6 ; j++)
            {
                input<<semester[i][j]<<"," ;
            }
        }
    
        input<<endl;
        input.close();
    
    }
    
    /***************************************** END OF FUNCTION ********************************************/
    
    
    void search()
    {
    
        system("cls");
        cout<<setw(78)<<" ** COMSiS **" <<endl;
    
        int x=0;
        char *a= new char [500];
        char *b= new char [500];
        char *c= new char [500];
    
        char *k= new char [100];
    
        ifstream fileread;
    
        fileread.open ("bilalzaman.txt", ios::in);
    
        
    
        while (!fileread.eof())        
        {
            fileread.getline(a, 10000000, '\n');
            
            b=strtok(a, ",");
    
        
            *k= *b;
    
    
            if (*k==*rollNumber)
            {
                cout<<"Record Found\n\n\n";
    
                studentName=strtok(NULL,",");
                fathersName=strtok(NULL,",");
                dob=strtok(NULL,",");
                enrollmentDate=strtok(NULL,",");
                batch=strtok(NULL,",");
                department=strtok(NULL,",");
                offences=atoi (strtok(NULL,","));
                fine=atoi (strtok(NULL,","));
                sem=atoi (strtok(NULL,","));
                warning=atoi (strtok(NULL,","));
                semester[1][0]=atoi (strtok(NULL,","));
                semester[1][1]=atoi (strtok(NULL,","));
                semester[1][2]=atoi (strtok(NULL,","));
                semester[1][3]=atoi (strtok(NULL,","));
                semester[1][4]=atoi (strtok(NULL,","));
                semester[1][5]=atoi (strtok(NULL,","));
    
                cout<<"Roll no : "<<rollNumber<<"\nName : "<<studentName<<"\nFather's Name : "<<fathersName<<"\nD.O.B : "<<dob<<"\nEnrollment Date : "<<enrollmentDate<<"\nBatch : "<<batch<<"\nDept. : "<<department<<"\nNo. of Offences : "<<offences<<"\nTotal Fine Paid : "<<fine<<"\nCurrent Semester : "<<sem<<"\nNo. of Warnings : "<<warning<<"\nGPA : "<<semester[1][0]<<"\n";
                
                for ( int i=1 ; i<2 ; i++)
                {
                    cout<<"ATTENDANCE FOR THE 5 COURSES IS AS FOLLOWS :-"<<endl;
                    for ( int j=1 ; j<6 ; j++)
                    {
                        cout<<semester[i][j]<<"," ;
                    }
                    cout<<endl;
                }
                getche();
                fileread.close();
                break;
    
    
            }
    
            
        
    
        }
    
    
    
        
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Hint3: The "==" also does not work with with C-strings (char arrays).
    Code:
    *k==*rollNumber

    I suggest reading about strcpy and strcmp if you wish to use C-strings (char arrays)


    Hint2: "*k= *b;" this does NOT work with C-strings (char arrays)

    Hint: 10000000 is larger than 500!

    Tim S.

    Code:
    char *a= new char [500];
    Code:
    fileread.getline(a, 10000000, '\n');
    Read http://www.cplusplus.com/reference/i...tream/getline/
    Last edited by stahta01; 12-25-2012 at 10:02 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    I changed
    Code:
    *k==*rollNumber
    to
    Code:
    (strncmp (rollNumber,k,2) == 0)

    and


    Code:
    *k= *b;
    to
    Code:
    strcpy (k,b);
    and finally changed the string length of new char [500] to

    Code:
    char *a= new char [10000000];
    in accordance to
    Code:
    fileread.getline(a, 10000000, '\n');
    Still not reading it correctly
    It reads out the Roll Number and the data only from the first entry.
    If I have more than 1 Students saved in the file, it only reads the first one !!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bilal Zaman
    Please have a look and please tell me whats the problem.
    The problem only is in the last function (i.e. Search). Its at the end on the code.
    Actually, the problem is everywhere because of overuse of global variables. Generally, global variables make it harder to reason about your program because an "innocent" function call could have changed global state that we might assume did not change.

    I suggest that you start by making a major rewrite of your entire program such that you do not use global variables anywhere: your variables should be declared in functions, near where they are first used, then passed to other functions as needed. Besides this, get rid of stuff like new char[100] in favour of std::string, and things like new int [500] with std::vector<int>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Quote Originally Posted by laserlight View Post
    start by making a major rewrite of your entire program
    I wrote this program this way cuz I don't know much about C/C++ right now. I'm a freshmen and started learning C/C++ 2 months back. Is it possible that this program will work with my code Or do I have to change it at any cost?
    P.S: I don't know what is
    Quote Originally Posted by laserlight View Post
    std::vector<int>

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bilal Zaman
    I wrote this program this way cuz I don't know much about C/C++ right now. I'm a freshmen and started learning C/C++ 2 months back. Is it possible that this program will work with my code Or do I have to change it at any cost?
    It should be possible to fix the specific problem your code in its current state. The problem is that this misses the important lesson of learning how to make good use of local variables, and how that makes it easier to reason about your code. So yes, change it at any cost. What you gain from the experience will help you in future programs more than just fixing some bugs.

    Quote Originally Posted by Bilal Zaman
    I don't know what is
    If you have not yet learned about std::vector, then for now stick to using new int [500], but remember to delete[] what you new[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!! Urgent !!
    By huwan in forum C++ Programming
    Replies: 21
    Last Post: 06-15-2007, 10:04 AM
  2. urgent
    By nightingale in forum C Programming
    Replies: 8
    Last Post: 07-19-2003, 10:29 AM
  3. very urgent
    By chandramouli_cm in forum C Programming
    Replies: 2
    Last Post: 09-17-2002, 05:07 AM
  4. Urgent!!!!!!please Help Me!
    By duffy in forum C Programming
    Replies: 2
    Last Post: 09-06-2002, 02:13 AM