Student Directory Search/ Ifstream C++

I am trying to read the prog8in.txt file so I can search through roster using an ID # or a Last Name but I cannot get the program to read my .txt file correctly
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
 #include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
ifstream inStream;
struct student{
        int id;
        char fname[20];
        char lname[20];
        char email[40];
        double gpa;
};
struct roster{
    struct student;
};

// list all function prototypes here
int displayMenu(int recNum);
bool getRecord(int& recNum, int& id, char fname[20], char lname[20], char email[40], double& gpa);
void displayStudents();

int main(){
    int recNum = 0, x = 0, id;
    char fname, lname, email;
    double gpa;
    getRecord(recNum, id, &fname, &lname, &email, gpa);
    while (x == 0){
    displayMenu(recNum);
        cout << ">> ";
    int choice = 0;
    cin >> choice;
    if (choice == 1) {
            
    } else if (choice == 2) {
        
    }else if (choice == 3){
        
    }else if (choice == 4){
        break;
    }
}
    system("PAUSE");
    return 0;
}

// List all function definitions here
int displayMenu(int recNum){
    cout << "********** Class Roster ******* Total Students = ";
    cin >> recNum;
    cout << "1. Display all student" << endl;
    cout<<  "2. Search a student by ID" << endl;
    cout << "3. Search a student by Last Name" << endl;
    cout << "4. Exit" << endl;
    return recNum;
}
void displayStudents(){
    
}

bool getRecord(int& recNum, int& id, char fname[20], char lname[20], char email[40], double& gpa){
    if (recNum==0) {
        inStream.open("prog8in.txt");
        if (inStream.fail()){
            cout<<"File \"prog8in.txt\" cannot be opened."<<endl;
            system("pause");
            exit(1);
        } else cout<<"File \"prog8in.txt\" opened."<<endl;
    }
    inStream >>id >>fname >>lname >>email >>gpa;
    if (inStream.eof()) return false;
    else {
        recNum++;
        return true;
    }      
}


My Output:

File "prog8.txt" opened.
Press any key to continue...

My .txt file
1
2
3
4
5
6
9990001 Stephen Smith  me@gmail.com 2.33
9990002 Jane Doe me2@gmail.com 3.00
1110003 Barbara Fisher me3@gmail.com 3.50
2220004 Olin Johnson me4@gmail.com 2.99
9990003 Albert Jones me5@gmail.com 3.67
9990004 Lennard Johnson me6@gmail.com 3.00
Your character arrays are too small. By the time Albert Jones is reached your name arrays are already full.
I increased the size of my character arrays but got the same output. Any other suggestions?
You don't have a while loop. You will only read the first line of the file.

1
2
while(inStream)
inStream>>id>>fname<<lname<<email<<gpa;


For more clarity, what exactly are you trying to do?
Last edited on
I am trying to read the file so I can display the .txt file like so
Record
Student ID: 9990001
Student Name: Stephen Smith
Student Email: me@gmail.com
Student GPA: 2.33

and be able to read the file so I can be able to search for the students individually.
1
2
3
4
5
6
7
8
9
while(inStream)
{
inStream>>id>>fname<<lname<<email<<gpa;
cout<<"Student ID: "<<ID<<endl;
cout<<"Student Name: "<<fname<<" "<<lname<<endl;
cout<<"Student Email: "<<email<<endl;
cout<<"Student GPA: "<<gpa<endl;
}


try this.

EDIT: I was wrong earlier about the character array being too small.
Last edited on
Thank You! That's just what I needed, I think I'll be able to finish it from this point
Topic archived. No new replies allowed.