C++ read from file

I have a C++ program that Im working on and it should read from a Text file and i can't figure out the program and why it wont work.
So when you run the program, it should asks the users to entered the course name and number something like this:

(Enter a course number > COMP375)

then the program should search the text file and look for that course and display the course details such as


CRN: 10070
Instructor: Williams
Comp Architecture & Org
Days: MWF
Starts time: 1300
Building: GRAHA210

and here is my codes

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
    bool check=true;
    do{
    
    check=true;
    string srg;
    
    ifstream filestream("text.txt");
    string input = "";
    
    // How to get a string/sentence with spaces
    cout << "Please enter a Course number \n ";
    getline(cin, input);
    cout << "Your course number is  " << input << endl << endl;
    if (filestream.is_open())
    {
        while ( getline (filestream,srg) )
        {
            if (srg.find(input) != string::npos) {
              
                
                cout << srg <<endl;
            }
        }
        filestream.close();
    }
    else {
        cout << "File opening is fail."<<endl;
    }
    int i=0;
    cout<<"Please press 1 for again !!:: \n";
    cin>>i;
    if(i==1)
    {
        check=true;
    }
    else{
        check=false;
    }
        {
         
            return 0;
        }
Last edited on
Topic archived. No new replies allowed.