Textfile and fstream

I have problem when working with txt file. I have coding the section for input and search name but it does not work in this case when trying to input data or output data from the text file.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void input();
void searchName();

void input()
{
int id; string name; int age; string lang;
ofstream newstudent ("data.txt");

cout << "Enter id please";
cin >> id;

cout << "Enter student name";
cin >> name;

cout << "Enter student age";
cin >> age;

cout << "Enter student programming languages";
cin >> lang;

data.close();
system("pause"); 
}

void searchName()
{

ifstream student ("data.txt");
string name; string str; int id; int age; string lang;

cout << "Enter name";
cin >> str;

 while(student >> id >> name >> age >> lang){
    if(str == name){
     cout << "student have been found" << endl;
     cout << id << ' ' << name << ' ' << age << ' ' << lang << ' ' << endl;
     break;
    }
    if(str != name){
      cout << "student have not been found" << endl;
      break;
    }
  }
system("pause");
} 

When I input data it won't store the data within the text file, when I search for name it will output the first line of the data within the text file, only John will be outputted but everyone else "student have not been found". What the problem please?
1 John 22 Cplusplus
2 James 23 Java
3 Sarah 25 Python
4 Janet 19 VB.NET
5 Anil 18 cSharp
When I input data it won't store the data within the text file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void input()
{
int id; string name; int age; string lang;
ofstream newstudent ("data.txt");

cout << "Enter id please";
cin >> id;

cout << "Enter student name";
cin >> name;

cout << "Enter student age";
cin >> age;

cout << "Enter student programming languages";
cin >> lang;

data.close();
system("pause"); 
}

You aren't physically writing data to the file. Try adding the line newstudent << variableName; after each cin statement.

when I search for name it will output the first line of the data within the text file, only John will be outputted but everyone else "student have not been found". What the problem please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void searchName()
{

ifstream student ("data.txt");
string name; string str; int id; int age; string lang;

cout << "Enter name";
cin >> str;

 while(student >> id >> name >> age >> lang){
    if(str == name){
     cout << "student have been found" << endl;
     cout << id << ' ' << name << ' ' << age << ' ' << lang << ' ' << endl;
     break;
    }
    if(str != name){
      cout << "student have not been found" << endl;
      break;
    }
  }
system("pause");
} 

Try removing the break from the if(str != name) code block. The purpose behind it is that if the name isn't found, you don't want to exit the loop, you want to search for another name. Also, if you enter Anil, you will display "student have not been found" 4 times before you finally "find" the student. That's not very logical. Maybe consider adding in a boolean to store a flag on if the student was found or not and check against that?
Topic archived. No new replies allowed.