1,105,644 Community Members

Switch Statements and Functions - Please Help

Member Avatar
Octet
Posting Pro
579 posts since Nov 2011
Reputation Points: 45 [?]
Q&As Helped to Solve: 53 [?]
Skill Endorsements: 16 [?]
Featured
Sponsor
 
0
 

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

Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,418 posts since Oct 2010
Reputation Points: 659 [?]
Q&As Helped to Solve: 229 [?]
Skill Endorsements: 28 [?]
 
0
 

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.

Member Avatar
Octet
Posting Pro
579 posts since Nov 2011
Reputation Points: 45 [?]
Q&As Helped to Solve: 53 [?]
Skill Endorsements: 16 [?]
Featured
Sponsor
 
0
 

Thanks for the help, it works a dream!

Question Answered as of 2 Years Ago by Schol-R-LEA
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: