need help...

this is for a school ok?
i wanna to do some thing that if person press 1 start to add student and when press EOF something like 999 or -1 finish the entering... beside that, i wanna to save this on a file in order that if he/she press number 3 and chose to search by name or lname... be able to find student from the file. but every time i start to add new student it replaced to the last student :/

to sum up:
1- i don't know how to add names over and over in the file without losing last data
2- i don't know how to search in the file by the name or lname :(
oh i have number 3 too: i don't know how to delete a information of a student that i have entered already
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


class NewStudent{
    public:
     string Name,Lname,Major,Location;
     int  Grade=0,Age=0;
};
int Adder = 0;
NewStudent STU[255];
int New(){
for(Adder;Adder< 55;Adder++){
cout << "Please Enter The Name Of The Student: ";
cin  >>STU[Adder].Name;

cout << "Please Enter The Last Name Of The Student: ";
cin  >>STU[Adder].Lname;

cout << "Please Enter The Age Of The Student: ";
cin  >> STU[Adder].Age;

cout << "Please Enter The Location Of The Student: ";
cin  >>STU[Adder].Location;

cout << "Please Enter The Major Of The Student: ";
cin  >>STU[Adder].Major;


cout << "Please Enter The Grade Of The Student: ";
cin  >> STU[Adder].Grade;

    ofstream fout ("output.txt");
    fout << STU[Adder].Name << "," << STU[Adder].Lname << "," << STU[Adder].Age
         << "," << STU[Adder].Location << "," << STU[Adder].Major << ","
         << STU[Adder].Grade << endl;
    fout.close();
    cout << "\nYour Student Have Bean Saved";
}
}
int Delete(){

}
Fiind(){
    cout << "You have Name(1) Or Lastname(2)";
    int chose;
    cin >> chose;
    switch(chose)
    {
        case 1:
        {
            cout << "\n\nYou Have Name. So Please Enter The Name of Student That You Want" <<endl;
            int n = STU.name.size();
            for (int i=0;i<=n;i++)

            break;
        }

        case 2:
        {
            cout << "\n\nYou Have Name. So Please Enter The Last Name of Student That You Want" <<endl;
            break;
        }
    }
}
int Average(){

}

int main()
{
    int chose;
cout << "(1) -> Enter New Student. "
     << "\n(2) -> Delete A Student. "
     << "\n(3) -> Ask For Information Of a Student. "
     << "\n(4) -> Ask For Numbers Of a Computer Student"
     << "\n(5) -> Ask For Numbers Of how many lives in esf\n"
     << "\nPlease Select One Of Numbers > ";

cin >> chose;
switch(chose)
{
    case 1:
    {
        cout << "\n\nEnter New Student." <<endl;
        New();
        cout << "\n\n" << endl;
        main();
        break;
    }

    case 2:
    {
        cout << "\n\nDelete A Student." <<endl;
        Delete();
        break;
    }

    case 3:
    {
        cout << "\n\nAsk For Information Of a Student." <<endl;
        Fiind();
        break;
    }
    case 4:
    {
        cout << "\n\nAsk For Numbers Of a Computer Student." <<endl;
        break;
    }
    case 5:
    {
        cout << "\n\nAsk For Numbers Of how many lives in esf." <<endl;
        break;
    }
}


    return 0;
}
1- i don't know how to add names over and over in the file without losing last data
Open file in append mode or place cursor at the end:
1
2
std::ofstream fout ("output.txt", std::ios::app);
std::ofstream fout ("output.txt", std::ios::ate);

2- i don't know how to search in the file by the name or lname :(
Read record. Check if names match. If not, load next record, etc.
oh i have number 3 too: i don't know how to delete a information of a student that i have entered already
Copy all records from input file to output, skipping records to delete.
TnxX For Your help :) but... :) I don't get any thing :s
could you show me in a example?



As you say so I had mistake to create a array of my class?

When I should use ios::app and when ios::ate ?

How should I skip records :/
Well in pseudocode it will be:
1)
1
2
3
4
5
6
Student sudents[255];
int student_count = 0;
//<Read one students or more>
std::ofstream fout ("output.txt", std::ios::app);
for(int i = 0; i < student_count; ++i)
    //<Write students> 

2)
1
2
3
4
5
std::ifstream fin(/*...*/);
Student student;
do {
    student = //Read student from in
} while(student.name != /*name we want*/)

1
2
3
4
5
6
7
std::ifstream fin(/*...*/);
std::ofstream fout(/*...*/);
while(fin) {
    Student student = //Read student from in
    if (/*record is not one to delete*/)
        //write student to fout.
}

Another approach would be read all students to array and make changes/search here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int chose;
    cin >> chose;
    NewStudent Student;
    switch(chose)
    {
        case 1:
        {
            cout << "\n\nYou Have Name. So Please Enter The Name of Student That You Want";
            ifstream fin('output.txt', ios::app);

           do {
           STU = fin >> output.txt;
                 }while(Student.Name != jack)
            break;
        }


Is It Going to Next Recoerd???????how to search by the name and going to next record from my file?

this is have error an my code::block :/
error: 'jack' was not declared in this scope|
error: 'output' was not declared in this scope|
error: expected ';' before 'break'|
error: no match for 'operator>>' in 'fin >> "output.txt"'|
Last edited on
STU = fin >> output.txt; What is it? how is it going to work?

while(Student.Name != jack) What is jack? How it was declared? What does it contains?
i really think about that and i don't know how to make this well :(

its really hard to understand what it dose.. how to read file . .. how to put fully main arrey in a new array to study about that...
how to know is read all main array... :(

why you get mad? its my second program
I am not mad, I am trying to explain reasoning behind each line. Remember: if you do not know how line you wrote works, it is wrong.

I will show examples on simple structure denoting pair of coords:
1
2
3
4
5
struct coord
{
    double x;
    double y;
};


read from file by creating a function returning structure:
1
2
3
4
5
6
7
8
coord read(std::istream& in)
{
    coord C;
    in >> C.x >> C.y;
};
//...
std::ifstream fin("input.txt");
coord point_x = read(fin);
..Or by overloading operator>>:
1
2
3
4
5
6
7
8
9
std::istream& operator>>(std::istream& in, coord& val)
{
    in >> val.x >> val.y;
    return in
}
//...
std::ifstream fin("input.txt");
coord point_x;
fin >> point_x;
http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/

It seems that you are still struggling with basics. I suggest refresh your knowledge on basic info like variables, strings, loops, arrays, functions, etc. before you go to more advanced topics.
Topic archived. No new replies allowed.