dimanche 5 décembre 2021

Problem with looping menu and output from external file

hope you are having a good day. I'm attempting to create a project where I can display the contents of an external file, with a menu that loops. I can not figure out if the problem is with my readFile function, or with my menu, as once the program displays the data file, it will not loop back to the menu. Here's my main:

int main()
{
    getMenu();
    unsigned short menuChoice = 0;
    cin >> menuChoice;
    do {
        switch (menuChoice) {
        case DisplayFile:
            readFile();
            break;

        case AddToFile:
            addToFile();

        case Exit:
            return 0;
        default:
            cout << "You have entered an invalid menu 
                    choice.\n "
                 << "Please try again";
            getMenu();
        }
        //menuChoice=0;
    } while (menuChoice != Exit);
    return 0;
}

Here is my read/display file code


void readFile()
{

    ifstream HomeWorknInfo;
    HomeWorknInfo.open("dates.txt", ios::in);
    if (!HomeWorknInfo.is_open()) {
        exit(EXIT_FAILURE);
    }
    while (HomeWorknInfo.is_open()) {
        string line;
        while (getline(HomeWorknInfo, line))
            cout << line << "\n";
    }
    HomeWorknInfo.close();
}

Also here's my menu choice options and includes in case ya need them:

#include <iostream>
#include <vector>
#include <stream>
#include <cstdlib>

enum MenuChoice {
    DisplayFile,
    AddToFile,
    Exit
};

Aucun commentaire:

Enregistrer un commentaire