I am creating a database search and I am having a bit of issue when I want to return to my main menu.
I have attempted to use loops with no success, could anyone suggest how I can do this?

#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>

using namespace std;

int Password = -1;
int Option = -1;
char Complete;
string Display;

void View_Account()                                                                  //View All Available Accounts
{
    system("CLS");

    cout << "--Viewing All Available Resources--\n" << endl;

    {
        ifstream File ("Log.txt", ifstream::in);

        if(File.good())
        {
            while (!File.eof())
            {
                getline (File, Display);
                cout << Display << endl;
            }
            File.close();
        }
    }
    cout << "\n\nComplete (Y/N): ";

}

void Modify_Account()                                                                //Modify All Available Accounts
{
    cout << "Modify Account";
}

int main()
{
    while (Password != 1234)
    {
        system("CLS");

        cout << "Welcome to the MAC Database Search Facility\n" << endl;            //Log In Screen
        cout << "Please enter your password now: ";

        cin >> Password;                                                            //User Input
    }
    system("CLS");

    cout << "Welcome ****, please select your option\n" << endl;                    //Welcome Screen

    cout << "1 - View All Accounts" << endl;                                        //All Available Options
    cout << "2 - Modify an Account" << endl;
    cout << "3 - Delete an Account" << endl;
    cout << "4 - Add an Account" << endl;
    cout << "5 - Search Accounts" << endl;
    cout << "6 - Exit\n" << endl;

    cin >> Option;

    switch (Option)                                                                 //Switch Statements
    {
        case 1:
        View_Account();
        break;

        case 2:
        Modify_Account();
        break;
    }
while (Option != 6);
}

How would I get it so from lines 13 to 32 (as an example) the user types Y or N, if the user types Y it goes to main(), 41 to 75?
And when they type N it repeates the question as to if they are complete?

Thank you

Recommended Answers

All 2 Replies

If you put the final while() conditional at the beginning of the main() function, and put braces around the rest of the function, it should loop around the whole function:

#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>

using namespace std;

#define DEBUG 0

int Password = -1;
int Option = -1;
char Complete;
string Display;

void pause()
{
 #if (DEBUG != 1)
    system("pause");
#endif
}

void clearScreen()
{
#if (DEBUG != 1)
    system("CLS");
#endif
}

void View_Account()                                                                  //View All Available Accounts
{
    clearScreen();

    cout << "--Viewing All Available Resources--\n" << endl;

    {
        ifstream File ("Log.txt", ifstream::in);

        if(File.good())
        {
            while (!File.eof())
            {
                getline (File, Display);
                cout << Display << endl;
            }
            File.close();
        }
    }
    cout << "\n\nComplete (Y/N): ";
    pause();
}

void Modify_Account()                                                                //Modify All Available Accounts
{
    cout << "Modify Account";

    pause();
}

int main()
{
    while (Option != 6)
    {

        while (Password != 1234)
        {
            clearScreen();

            cout << "Welcome to the MAC Database Search Facility\n" << endl;            //Log In Screen
            cout << "Please enter your password now: ";

            cin >> Password;                                                            //User Input
        }
        clearScreen();

        cout << "Welcome ****, please select your option\n" << endl;                    //Welcome Screen

        cout << "1 - View All Accounts" << endl;                                        //All Available Options
        cout << "2 - Modify an Account" << endl;
        cout << "3 - Delete an Account" << endl;
        cout << "4 - Add an Account" << endl;
        cout << "5 - Search Accounts" << endl;
        cout << "6 - Exit\n" << endl;

        cin >> Option;

        switch (Option)                                                                 //Switch Statements
        {
        case 1:
            View_Account();
            break;

        case 2:
            Modify_Account();
            break;
        }
    }
}

Note that I moved the references to system("cls") to a function; this is because it is advisable to comment out the clear screen function while debugging, so you can see the whole screen without it refreshing and losing information. Putting it in a small function like this, and using the DEBUG variable as a sentinel for conditional compilation, you can switch the screen refresh on and off with a single variable.

Thanks for the help, it works a dream!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.