Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
int ID;
struct Student {
    int id;
    string name;
    int form;
    string degree;
    int semesterNo;
};

void addStudent() {
    Student student;
    cout << "\n\tEnter student name : ";
    cin.get();
    getline(cin, student.name); 
    cout << "\n\tEnter student age : ";
    cin >> student.form;
    cout << "\n\tEnter student degree : ";
    cin >> student.degree;
    cout << "\n\tEnter student semester number : ";
    cin >> student.semesterNo;
    ID++;

    ofstream write;
    write.open("student.txt", ios::app);
    write << "\n" << ID;
    write << "\n" << student.name;
    write << "\n" << student.form;

    write << "\n" << student.degree;
    write << "\n" << student.semesterNo;
    write.close();
    write.open("id.txt");
    write << ID;
    write.close();
    cout << "\n\tData save to file";
}

void print(Student s) {
    cout << "\n\t---Current List---";
    cout << "\n\tID is : " << s.id;
    cout << "\n\tName is : " << s.name;
    cout << "\n\tAge is : " << s.form;
    cout << "\n\tDegree is : " << s.degree;
    cout << "\n\tSmester Number is : " << s.semesterNo;

}

void readData() {
    Student student;
    ifstream read;
    read.open("student.txt");
    while (!read.eof()) {
        read >> student.id;
        read.ignore();
        getline(read, student.name);
        read >> student.form;
        read >> student.degree;
        read >> student.semesterNo;
        print(student);
    }
    read.close();
}

int searchData() {
    int id;
    cout << "\n\tEnter student id want to search : ";
    cin >> id;
    Student student;
    ifstream read;
    read.open("student.txt");
    while (!read.eof()) {
        read >> student.id;
        read.ignore();
        getline(read, student.name);
        read >> student.form;
        read >> student.degree;
        read >> student.semesterNo;
        if (student.id == id) {
            print(student);
            return id;
        }
    }
}

void deleteData() {
    int id = searchData();
    cout << "\n\tYou want to delete record (y/n) : ";
    char choice;
    cin >> choice;
    if (choice == 'y') {
        Student student;
        ofstream tempFile;
        tempFile.open("temp.txt");
        ifstream read;
        read.open("student.txt");
        while (!read.eof()) {
            read >> student.id;
            read.ignore();
            getline(read, student.name);
            read >> student.form;
            read >> student.degree;
            read >> student.semesterNo;
            if (student.id != id) {
                tempFile << "\n" << student.id;
                tempFile << "\n" << student.name;
                tempFile << "\n" << student.form;
                tempFile << "\n" << student.degree;
                tempFile << "\n" << student.semesterNo;
            }
        }
        read.close();
        tempFile.close();
        remove("student.txt");
        rename("temp.txt", "student.txt");
        cout << "\n\tData deleted successfuly";
    }
    else {
        cout << "\n\tRecord not deleted";
    }
}

void updateData() {
    int id = searchData();
    cout << "\n\tYou want to update record (y/n) : ";
    char choice;
    cin >> choice;
    if (choice == 'y') {
        Student newData;
        cout << "\n\tEnter student name : ";
        cin.get();
        getline(cin, newData.name);
        cout << "\n\tEnter student age : ";
        cin >> newData.form;
        cout << "\n\tEnter student degree : ";
        cin >> newData.degree;
        cout << "\n\tEnter student semester number : ";
        cin >> newData.semesterNo;
        Student student;
        ofstream tempFile;
        tempFile.open("temp.txt");
        ifstream read;
        read.open("student.txt");
        while (!read.eof()) {
            read >> student.id;
            read.ignore();
            getline(read, student.name);
            read >> student.form;
            read >> student.degree;
            read >> student.semesterNo;
            if (student.id != id) {
                tempFile << "\n" << student.id;
                tempFile << "\n" << student.name;
                tempFile << "\n" << student.form;
                tempFile << "\n" << student.degree;
                tempFile << "\n" << student.semesterNo;
            }
            else {
                tempFile << "\n" << student.id;
                tempFile << "\n" << newData.name;
                tempFile << "\n" << newData.form;
                tempFile << "\n" << newData.degree;
                tempFile << "\n" << newData.semesterNo;
            }
        }
        read.close();
        tempFile.close();
        remove("student.txt");
        rename("temp.txt", "student.txt");
        cout << "\n\tData updated successfuly";
    }
    else {
        cout << "\n\tRecord not deleted";
    }
}

int main()
{
    ifstream read;
    read.open("id.txt");
    if (!read.fail()) {
        read >> ID;
    }
    else {
        ID = 0;
    }
    read.close();

    while (true) {
        cout << "\n\t1.Add student data";
        cout << "\n\t2.See student data";
        cout << "\n\t3.Search student data";
        cout << "\n\t4.Delete student data";
        cout << "\n\t5.Update student data";

        int choice;
        cout << "\n\tEnter choice : ";
        cin >> choice;
        switch (choice) {
        case 1:
            addStudent();
            break;
        case 2:
            readData();
            break;
        case 3:
            searchData();
            break;
        case 4:
            deleteData();
            break;
        case 5:
            updateData();
            break;
        }
    }

}


What I have tried:

How do I change this code to this kind of format?
(This is a project that my primary teacher ask for but sadly I don't really good at coding, so I'm here asking for help thank you very much guys.)
Format of the text file:
Student ID
Student Name
Form
BM BI BC Math Sci Sejarah Geo

Example:
20ACD1234
Alice Lim
1
80 93 78 75 86 82 67


how I want to list it
Student ID: 20ACD1234
Student Name: Alice Lim
Form: 1
BM: 80
BI: 93
BC: 78
Math: 75
Sci: 86
Sejarah: 82
Geo: 67

Main Menu Page:
Please select your choice:
1. Insert 
2. Filter/ Search 
3. Modify 
4. View Results 

//Insert
Please enter student information and marks.
Student Id: 20ACD1234
Student Name: Alice Lim
Form: 1
BM: 80
BI: 93
BC: 78
Math: 75
Sci: 86
Sejarah: 82
Geo: 67
Do you want to key in another record?
No

//Filter and search
Please select your choice:
1. Filter
2. Search
Choice: 1
Please key in the information that you want to filter.
Form: 1
Subject(s): BC Sci
Filtered list:
Student ID: 20ACD1234
Student Name: Alice Lim
Form: 1
BC: 78
Sci: 86
Student ID: 20ACD1235
Student Name: Derrick Tan
Form: 1
BC: 71
Sci: 68
Do you want to continue?
YesPlease select your choice:
1. Filter
2. Search

Choice: 2
Please key in the keyword you want to search: Alice
Search Results:
Student ID: 20ACD1234
Student Name: Alice Lim
Form: 1
BM: 80
BI: 93
BC: 78
Math: 75
Sci: 86
Sejarah: 82
Geo: 67

And for update and delete:
//Edit
Please select your choice:
1. Edit
2. Delete
Choice:1
Current list:
[Show the available list of student record so that user able to key in the student id by referring to the list]
Please key in the student id that you want to edit: 20ACD1234
Please select the attribute that you want to edit:
1. Name
2. Form
3. Marks
Please key in the attribute that you want to edit: 3
Please enter the subject that you want to change the mark (BM/BI/BC/Math/Sci/Sejarah/Geo): Sejarah
Please update the mark for Sejarah: 87
Do you still have anything to edit for this student?
Yes
Please key in the attribute that you want to edit: 2
Please update the form: 2
Do you still have anything to edit for this student?
No
Do you want to edit record for other students?
No
Successfully updated!
Updated List:
[Show the updated list of student record.]
Do you want to continue?
Yes

//Delete
Please select your choice:
1. Edit
2. Delete
Choice: 2
Current list:
[Show the available list of student record so that user able to key in the student id by referring to the list]
Please key in the student id that you want to delete the entire record: 20ACD1235
Successfully Deleted!
Updated List:
[Show the updated list of student record.]
Do you want to continue?
No
Posted
Updated 21-Aug-21 21:42pm

Quote:
(This is a project that my primary teacher ask for but sadly I don't really good at coding, so I'm here asking for help thank you very much guys.)

And you aren't going to get "really good at coding" by getting others to do it for you - particularly when we have no idea what exactly your teacher has asked you to do!

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You need to learn some C++ for your homework. Mostly you need to understand some Basic Input/Outputand than write some code for the functions with some menu. Watch this video tutorial for menus to get kickstarted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900