I've been having difficulties in searching a text in a text file when I choose case 4 or 'show video details'. It seems that ifstream in a switch statement is not working. This is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    int choice;
    //4
    string line;
    bool iffound = false;
    string id1;
    ifstream file("test.txt");
menu:
    cout << "MENU" << endl
         << endl;
    cout << "[1] NEW VIDEO" << endl;
    cout << "[2] RENT A VIDEO" << endl;
    cout << "[3] RETURN A VIDEO" << endl;
    cout << "[4] SHOW VIDEO DETAILS" << endl;
    cout << "[5] DISPLAY ALL VIDEOS" << endl;
    cout << "[6] CHECK VIDEO AVAILABILITY" << endl;
    cout << "[7] CUSTOMER'S MAINTENANCE" << endl;
    cout << "[8] EXIT PROGRAM" << endl;
    cout << "Choice: ";
    cin >> choice;
    switch (choice)
    {
    case 1:
        break;
    case 2:
        break;
    case 3:
        break;
    case 4:
    {
        file.open("test.txt");
        cout << "<< SHOW VIDEO DETAILS >>" << endl
             << endl;
        cout << "Enter Movie ID: ";
        cin >> id1;
        string idnum1 = "VIDEO ID: " + id1;
        cout << idnum1 << endl;
        while (file.good())
        {
            getline(file, line);
            if (line == idnum1)
            {
                iffound = true;
                for (int i = 0; i <= 3; i++)
                {
                    getline(file, line);
                    cout << line << endl;
                }
                break;
            }
        }
        file.close();
        if (iffound != true)
        {
            cout << "Video not found!" << endl;
        }
    }
    break;
    case 5:
        break;
    case 6:
        break;
    case 7:
        break;
    case 8:
        break;
    }
}
And this is my text file:
VIDEO ID : 1
MOVIE TITLE     : IT
GENRE           : HORROR
PRODUCTION      : NEW LINE CINEMA
NUMBER OF COPIES: 0
it.jpg
VIDEO ID : 2
MOVIE TITLE     : THE CALL
GENRE           : HORROR
PRODUCTION      : YONG FILM
NUMBER OF COPIES: 3
thecall.jpg
VIDEO ID : 3
MOVIE TITLE     : I AM LEGEND
GENRE           : HORROR
PRODUCTION      : VILLAGE ROADSHOW
NUMBER OF COPIES: 6
iamlegend.jpg
When I input a valid Id, for instance, 1, it outputs 'video not found', when clearly the Id is present.
But when I try this code, where there is no switch statement, it works:
int main()
{
    ifstream file("test.txt");
    string line;
    bool find = false;
    string id;
    string img;
    cout << "Enter movie ID: ";
    cin >> id;
    string idnum = "VIDEO ID : " + id;
    cout << idnum << endl;
    while (file.good())
    {
        getline(file, line);
        if (line == idnum)
        {
            find = true;
            for (int i = 0; i < 4; i++)
            {
                getline(file, line);
                cout << line << endl;
            }
            getline(file, img);
            cout << img;
        }
    }
    file.close();
    if (find != true)
    {
        cout << "Movie Not Found" << endl;
    }
    return 0;
}
But I need to run it in a switch statement.
 
     
    