mardi 30 juillet 2019

What other control can i use to replace 'Ctrl + Z'?

So I make a program where you can custom write a text file. These is a program meaning that there are functions. I have 3 different functions where the user will have to choose one when starting the program. The first function is to add new text into tect file. The second fucntion is to display the text file. The third fucntion is to create a new text file in the directory. However my first function seem to have a problem, when writing a new text file it require you to press ctrl+z to add and exit at the same time. Since my file is in a loop this cause infinite loop and in order to continues is to close the program and re-run it. Is there anyone who know how to help me fix this? Thank you

I've tried break, return, ctrl+c, ctrl +d but nothing work.

    using namespace std;
    #include <iostream>
    #include <fstream>

    void welcome();
    void addFile();
    void displayFile();
    void createFile();

    int main() {
        char choice;
        welcome();  
        cin >> choice;
        switch (choice) {
            case 'a': 
                cout << "You has choose to add to files. \n";
                addFile();
                break;
            case 'b': 
                cout << "You has choose to display the schedule. \n";
                displayFile();
                break;
            case 'c':
                cout << "You has chose to create a new file. \n";
                createFile();
                break;
            default:
                cout << "Please enter a valid choice.";
                break;
        }
        main();
    }

    void welcome() {
        cout << "Welcome to your Personal Schedule \n";
        cout << "Choose 'a' to add to schedule. \n" << "Choose 'b' to                 display the schedule \n" << "Choose 'c' to create a new file \n"; 
    }

    void createFile() {
        ofstream scFile("schedule.txt");
        scFile << "It has 4 doors! \n";
        scFile.close(); 
    }

    void addFile() {
        ofstream scFile("schedule.txt");

        cout << "Enter date, month and task" << endl;
        cout << "Press Control + Z to confirm and quit" << endl;

        int date, month;
        string task;

        while(cin >> date >> month >> task) {
            scFile << date << ' ' << month << ' ' << task << endl;
        }
    }

    void displayFile() {
        ifstream scFile("schedule.txt");

        int date, month;
        string task;

        while(scFile >> date >> month >> task) {
            cout << date << ", " << month << ", " << task << endl;
        }
    }

Aucun commentaire:

Enregistrer un commentaire