how do i convert this to a file.dat and delete, edit, add and display

  • 12 years ago

    can anyone plzzz help me with this....basically i have to rewrite the program so that the data will store permanently in a file and it can be retrieved for adding, editing and deleting......

    how do i do this.....

    this is the code....

     

    # include <iostream>

    # include <string>

    # include <cctype>

    # include <iomanip>

    using namespace std;

    struct student // the structure of the students marks

    {

    // storage

    string id;

    float phy;

    float chem;

    float peng;

    float math;

    }marks [200];

    void menu (); // function for menu

    void help (); // function for help

    void add (); // function for new

    void edit (); // function for edit

    void del (); // function for delete

    void display (); // function for display

    void exit (); // function for exit

    int z = 0; // storage that will set the integer value so that when run in the main program it wont turn 0

    // function main begins program execution

    int main ()

    {

    // storage

    char choice;int n=0;

     

    while(1)

    {

    menu(); // call menu function

    cin >> choice;

    choice = toupper(choice); // change input to uppercase

    if(choice == '?')

    {

    system (
    "cls");

    help (); // call help function

    }

    else if(choice == '+')

    {

    system (
    "cls");

    add (); // call add function

    }

    else if(choice == '#')

    {

    system (
    "cls");

    edit (); // call edit function

    }

    else if(choice == '-')

    {

    system (
    "cls");

    del (); // call delete function

    }

    else if (choice == '=')

    {

    system (
    "cls");

    display (); // call display function

    }

    else if (choice == 'X')

    {

    system(
    "cls");

    exit (); // call the exit function

    return 0;

    }

    else

    {

    cout <<
    "Incorrect sign." << endl;

    system ("pause");

    system("cls");

    cout << endl;

    }

    };

    return 0; // indicate that program end successfully

    }// end function main

    // function menu begins program execution

    void menu()

    {

    // output

    cout << "----------------------------MENU-----------------------------" << endl;

    cout << setw(10)<< left << "1.HELP" << setw(5) << left << " -->" << " ?" << endl;

    cout << setw(10)<< left << "2.ADD" << setw(5) << left << " -->" << " +" << endl;

    cout << setw(10)<< left << "3.EDIT" << setw(5) << left << " -->" << " #" << endl;

    cout << setw(10)<< left << "4.DELETE" << setw(5) << left << " -->" << " -" << endl;

    cout << setw(10)<< left << "5.DISPLAY" << setw(5) << left << " -->" << " =" << endl;

    cout << setw(10)<< left << "6.EXIT" << setw(5) << left << " -->" << " X" << endl << endl << endl;

    cout << "P/S : If u have not entered any data, please do so before proceed to '#' and '='. Enter '+' to add new data." << endl << endl;

    cout << "Please enter your choice : ";

    }// end function menu

    // function help begins program execution

    void help()

    {

    // output

    cout << endl;

    cout <<
    "This program basically will ask user to enter the students ID's and their exam marks for certain subjects. ";

    cout << "User can either add a new set of data by entering '+' sign, ";

    cout << "edit or make a correction in the data by entering '#' sign, ";

    cout << "delete a data by entering the '-' sign, and also display ";

    cout << "the data on the screen by entering '=' sign. To exit ";cout << "program, user should enter the letter 'X'.";

    cout << endl << endl;

    system("pause");

    }// end function help

    // function add begins program execution

    void add ()

    {

    char ans;

    int n;

    // call the goto loop

    loop:

    n=z;

    cout <<
    "Enter ID [Eg : 07UEB01234] : ";

    fflush (stdin); // so that the program can read properly

    getline(cin,marksNo.id);

    // the for loop is to change the whole string to uppercase

    for (int j=0; j<((marksNo.id).length()); ++j)

    {

    (marksNo.id)[j]=toupper((marksNo.id)[j]);

    }

    a:

    cout <<
    "Enter Physic's marks [Eg : 50] : ";

    cin >> marksNo.phy;

    if (marksNo.phy < 0 || marksNo.phy > 100)

    {

    cout <<
    "Please enter between 0 - 100 only." << endl << endl;goto a;

    }

    b:

    cout <<
    "Enter Chemistry's marks [Eg : 50] : ";

    cin >> marksNo.chem;

    if (marksNo.chem < 0 || marksNo.chem > 100)

    {

    cout <<
    "Please enter between 0 - 100 only." << endl << endl;goto b;

    }

    c:

    cout <<
    "Enter Pengajian Am's marks [Eg : 50] : ";

    cin >> marksNo.peng;

    if (marksNo.peng < 0 || marksNo.peng > 100)

    {

    cout <<
    "Please enter between 0 - 100 only." << endl << endl;goto c;

    }

     

    d:

    cout <<
    "Enter Math's marks [Eg : 50]: ";

    cin >> marksNo.math;

    if (marksNo.math < 0 || marksNo.math > 100)

    {

    cout <<
    "Please enter between 0 - 100 only." << endl << endl;goto d;

    }

    z++;

     

    // call the goto loop

    aaa:

    cout <<
    "Continue?. [Y(to add more data)/N(menu)] : ";

    cin >> ans;

    ans = toupper(ans);

    cout << endl << endl;

     

    if (ans == 'Y')

    goto loop;

    else if (ans != 'N')

    {

    cout <<
    "Please enter Y or N only." << endl <<endl;goto aaa;

    }

    system("cls");

    }// end function add

    // function edit begins program execution

    void edit ()

    {

    // storage

    string id2;

    string ans;

    cout <<
    "Please enter your ID no to edit : ";

    fflush (stdin);

    getline(cin,id2);

    // change the string to uppercase

    for (int j=0; j<(id2.length()); ++j)

    {

    id2[j]=toupper(id2[j]);

    }

     

    for(int n=0;n<=z;n++)

    {

    if(marksNo.id == id2)

    {

    // call the goto loop

    loop:

    cout <<
    "FOUND!!. Which one would you like to edit [ID / PHYSIC / CHEMISTRY / PENG AM / MATHS ]: ";

    fflush (stdin);

    getline(cin,ans);

    // change the string to uppercase

    for (int j=0; j<(ans.length()); ++j)

    {

    ans[j]=toupper(ans[j]);

    }

     

    if(ans == "ID")

    {

    marksNo.id=
    " ";cout << "Please enter new ID number : ";

    fflush (stdin);

    getline(cin,marksNo.id);

    for (int j=0; j<((marksNo.id).length()); ++j)

    {

    (marksNo.id)[j]=toupper((marksNo.id)[j]);

    }

    }

    else if(ans == "PHYSIC")

    {

    marksNo.phy=NULL;

    cout <<
    "Please enter new Physic's marks : ";

    cin >> marksNo.phy;

    }

    else if(ans == "CHEMISTRY")

    {

    marksNo.chem=NULL;

    cout <<
    "Please enter new Chemistry's marks : ";

    cin >> marksNo.chem;

    }

    else if(ans == "PENG AM")

    {

    marksNo.peng=NULL;

    cout <<
    "Please enter new Peng Am's marks : ";

    cin >> marksNo.peng;

    }

    else if(ans =="MATHS")

    {

    marksNo.math=NULL;

    cout <<
    "Please enter new Maths's marks : ";

    cin >> marksNo.math;

    }

    else

    {

    cout <<
    "Please enter the correct name [ID / PHYSIC / CHEMISTRY / PENG AM / MATHS ]" << endl << endl;goto loop;

    }

    system("cls");

    break;

    }

    if(marksNo.id != id2 && n == z)

    {

    cout <<
    "Sorry, data not found" << endl;system ("pause");

    cout << endl;

    system (
    "cls");

    }

    }

    }
    // end function edit

    // function delete begins program execution

    void del ()

    {

    string id3;

    cout <<
    "Please enter the ID number to delete the whole data : ";

    fflush (stdin);

    getline(cin,id3);

    for (int j=0; j<(id3.length()); ++j)

    {

    id3[j]=toupper(id3[j]);

    }

     

    for(int n=0;n<=z;n++)

    {

    if(marksNo.id == id3)

    {

    marksNo.id =
    " ";

    marksNo.phy = marksNo.chem = marksNo.peng = marksNo.math = NULL;

    cout <<
    "Data found and has been deleted." << endl;

    system ("pause");

    system("cls");break;

    }

    if(marksNo.id != id3 && n == z)

    {

    cout << "Sorry, data not found. " << endl;

    system("pause");system("cls");

    }

    }

    }
    // end function del

    // function dislay begins program execution

    void display ()

    {

    cout << setw(15) << left <<
    "ID" << setw(10) << left << "PHY" << setw(10) << left << "CHEM" << setw(10) << left << "PENG" << setw(10) << left << "MATH";

    cout << endl << endl;

    //for loop that is use to display the structure up to interger z

    for (int n=0; n<z; n++)

    {

    // if is use to only diplay structure that is not empty

    if(marksNo.id != " " && marksNo.phy != NULL && marksNo.chem != NULL && marksNo.peng != NULL && marksNo.math != NULL)

    cout << setw(15) << left << marksNo.id << setw(10) << left << marksNo.phy << setw(10) << left << marksNo.chem << setw(10) << left << marksNo.peng << setw(10) << left << marksNo.math ;

    cout << endl << endl;

    }

    cout << endl;

    system(
    "pause");

    system ("cls");

    } // end function display

    // function exit begins program execution

    void exit ()

    {

    //output

    cout << "Thank you for using this program." << endl;

    }// end function exit

  • 12 years ago

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra