Add a menu system

Sargie93
Sargie93 used Ask the Experts™
on
So this is my code so far (I apologize for the extensive use of | and "" :

#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <conio.h>
using namespace std;
struct first

{
      string user;

};

 int main()

{
      system("color 0F");

      struct first A, answer, yes;

      cout << "Initiating Program..." << endl;
      Sleep (1000);
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "NOW LOADING...:  ";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      system("CLS");
      cout << "Please Verify Your Name..." << endl;
      getline(cin, A.user);

      
bool correctName=false;
while(!correctName)
{

      //Set A.user here...

      if (A.user == "Josh Sarge")
      {
            correctName=true;

            cout << "" << endl;
            cout << "Program Now Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "Verification Complete..." << endl;
            Sleep (2000);
            Sleep (1000);
            system ("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "Access Granted..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Welcome Josh" << endl;
            cout << ":)" << endl;
            cout << "" << endl;
            Sleep (2000);
            Sleep (1000);
            system("pause");
            return 0;

      }
        else
        {
        cout << "" << endl;
            cout << "Program Processing. Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
            cout << "Access Denied..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "" << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Please Enter Your Full Name" << endl;
            cout << "" << endl;
            getline(cin, A.user);
      }

}

}

It all works so far, but I'm trying to make it so that there is a menu system once the correct name has been entered. I'm pretty sure it's got something to do with creating new functions, but I don't know how to  call them into the main. I'm VERY new with this. This isn't academic, just a project I've started.

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
Calls for starting to use functions ;o)

I.e.

void WelcomeUser(const struct first& u)
{
  string fname;
  size_t pos = u.name.find(' '); // locate blank

  if (string::npos == pos) // no blank?
  {
    cout << "Welcome " << u.name; // OK, full name
  }

  fname = u.name.substr(0,pos - 1); // extract first name

cout << "Welcome " << fname;
}

// ...

      if (A.user == "Josh Sarge" || A.user == "Mike Sarge")
      {
            correctName=true;

            cout << "" << endl;
            cout << "Program Now Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "Verification Complete..." << endl;
            Sleep (2000);
            Sleep (1000);
            system ("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "Access Granted..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            WelcomeUser(A); // <------------- call function to welcome user

  // ...
}

Open in new window

Author

Commented:
@jkr Thank you for answering, but I'm going to be honest, I have pretty much no idea what to do with this bit of code, there's a lot in there I've never seen before so when I see things like "locate blank", I don't know what to do there. Also, when I put it into my program, I got these errors:

error C2039: 'name' : is not a member of 'first'
error C2059: syntax error : 'if'

Thanks again.
If you restructure the control flow a bit it would be easier to code further like

bool correctName=false;
while(!correctName)
{
      cout << "Please Verify Your Name..." << endl;
      getline(cin, A.user);
      if (A.user == "Josh Sarge" || A.user == "Mike Sarge")
          break; /// Breaks out of the loop
      .... access denied etc
}

// This is where control returns from the break in loop
// You only reach here if the correct name has been entered

.... code for the access granted part
cout << "Welcome Josh" << endl;
....
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

jkr
Top Expert 2012

Commented:
Sorry, just replace 'name' by 'user', i.e.

void WelcomeUser(const struct first& u)
{
  string fname;
  size_t pos = u.user.find(' '); // locate blank

  if (string::npos == pos) // no blank?
  {
    cout << "Welcome " << u.user; // OK, full name
  }

  fname = u.user.substr(0,pos - 1); // extract first name

cout << "Welcome " << fname;
}

Open in new window

Author

Commented:
Okay so that's definitely helped, only problem now is that:

if (A.user == "Josh Sarge" || A.user == "Mike Sarge")

comes up with a syntax error, expected a declaration?
jkr
Top Expert 2012

Commented:
Could you post your actual code and the full error message?
Post your entire code

Author

Commented:
This is the code:

#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

struct first

{
      string user;

};

 int main()

{
      system("color 0F");

      struct first A;

      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "                                Press Enter                      " << endl;
      cin.ignore();
      cout << "Initiating Program..." << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "NOW LOADING...:  ";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      system("CLS");
      cout << "Please Verify Your Name..." << endl;
      getline(cin, A.user);

      {

bool correctName=false;
while(!correctName)
{

      //Set A.user here...

      if (A.user == "Holly Abraham")
      {
            correctName=true;

            cout << "" << endl;
            cout << "Program Now Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "Verification Complete..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "Access Granted..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Welcome Holly" << endl;
            cout << "" << endl;
            system("pause");
            return 0;

      }
        else
        {
        cout << "" << endl;
            cout << "Program Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
            cout << "Access Denied..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "" << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Please Enter Your Full Name" << endl;
            cout << "" << endl;
            getline(cin, A.user);
      }

}

}
            Sleep (1000);
            cout << "Please Enter Your Full Name" << endl;
            cout << "" << endl;
            getline(cin, A.user);
      }

        void WelcomeUser(const struct first& u)
{
  string fname;
  size_t pos = u.user.find(' '); // locate blank

  if (string::npos == pos) // no blank?
  {
    cout << "Welcome " << u.user; // OK, full name
  }

  fname = u.user.substr(0,pos - 1); // extract first name

cout << "Welcome " << fname;
}

        if (A.user == "Josh Sarge" || A.user == "Mike Sarge")

      {
            correctName=true;

            cout << "" << endl;
            cout << "Program Now Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "Verification Complete..." << endl;
            Sleep (2000);
            Sleep (1000);
            system ("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "Access Granted..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            WelcomeUser(A); // <------------- call function to welcome user

  // ...
        }


And these are the errors:

1>------ Build started: Project: Holly's Birthday, Configuration: Debug Win32 ------
1>  Holly's Birthday.cpp
1>c:\users\user\desktop\programming\holly's birthday\holly's birthday\holly's birthday.cpp(180): error C2059: syntax error : 'if'
1>c:\users\user\desktop\programming\holly's birthday\holly's birthday\holly's birthday.cpp(182): error C2143: syntax error : missing ';' before '{'
1>c:\users\user\desktop\programming\holly's birthday\holly's birthday\holly's birthday.cpp(182): error C2447: '{' : missing function header (old-style formal list?)
jkr
Top Expert 2012

Commented:
Hm, you can't put the function in the middle of your code, that should be nicely separated, e.g.

#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

struct first

{
      string user;

};


void WelcomeUser(const struct first& u)
{
  string fname;
  size_t pos = u.user.find(' '); // locate blank

  if (string::npos == pos) // no blank?
  {
    cout << "Welcome " << u.user; // OK, full name
  }

  fname = u.user.substr(0,pos - 1); // extract first name

cout << "Welcome " << fname;
}


 int main()

{
      system("color 0F");

      struct first A;

      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "                                Press Enter                      " << endl;
      cin.ignore();
      cout << "Initiating Program..." << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "NOW LOADING...:  ";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      system("CLS");
      cout << "Please Verify Your Name..." << endl;
      getline(cin, A.user);

      {

bool correctName=false;
while(!correctName)
{

      //Set A.user here...

      if (A.user == "Holly Abraham")
      {
            correctName=true;

            cout << "" << endl;
            cout << "Program Now Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "Verification Complete..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "Access Granted..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Welcome Holly" << endl;
            cout << "" << endl;
            system("pause");
            return 0;

      }
        else
        {
        cout << "" << endl;
            cout << "Program Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
            cout << "Access Denied..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "" << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Please Enter Your Full Name" << endl;
            cout << "" << endl;
            getline(cin, A.user);
      }

}

}
            Sleep (1000);
            cout << "Please Enter Your Full Name" << endl;
            cout << "" << endl;
            getline(cin, A.user);
      }

        if (A.user == "Josh Sarge" || A.user == "Mike Sarge")

      {
            correctName=true;

            cout << "" << endl;
            cout << "Program Now Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "Verification Complete..." << endl;
            Sleep (2000);
            Sleep (1000);
            system ("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "Access Granted..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            WelcomeUser(A); // <------------- call function to welcome user

  // ...
        }

Open in new window


Oh, and please use the 'code' tag to post code ;o)

Author

Commented:
Oh right, sorry about that.... Okay so I've updated it like you said, but it's the same issue.
Top Expert 2012
Commented:
Well, I just see that it's incomplete - you also should add the rest of your code as above that was just a snippet. Try

#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
#include <conio.h>
using namespace std;

struct first

{
      string user;

};

void WelcomeUser(const struct first& u)
{
  string fname;
  size_t pos = u.user.find(' '); // locate blank

  if (string::npos == pos) // no blank?
  {
    cout << "Welcome " << u.user; // OK, full name
  }

  fname = u.user.substr(0,pos - 1); // extract first name

cout << "Welcome " << fname;
}

 int main()

{
      system("color 0F");

      struct first A, answer, yes;

      cout << "Initiating Program..." << endl;
      Sleep (1000);
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "" << endl;
      cout << "NOW LOADING...:  ";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      Sleep (500);
      cout << "|";
      system("CLS");
      cout << "Please Verify Your Name..." << endl;
      getline(cin, A.user);

      
bool correctName=false;
while(!correctName)
{

      //Set A.user here...

      if (A.user == "Josh Sarge" || A.user == "Mike Sarge")
      {
            correctName=true;

            cout << "" << endl;
            cout << "Program Now Processing...Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "Verification Complete..." << endl;
            Sleep (2000);
            Sleep (1000);
            system ("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
            cout << "Access Granted..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            WelcomeUser(A);
            cout << ":)" << endl;
            cout << "" << endl;
            Sleep (2000);
            Sleep (1000);
            system("pause");
            return 0;

      }
        else
        {
        cout << "" << endl;
            cout << "Program Processing. Please Wait..." << endl;
            Sleep (2000);
            Sleep (1000);
            system("CLS");
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
            cout << "Access Denied..." << endl;
            Sleep (2000);
            Sleep (1000);
            cout << "" << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Please Enter Your Full Name" << endl;
            cout << "" << endl;
            getline(cin, A.user);
      }

}

}

Open in new window

Author

Commented:
MASSIVE help, exactly what I needed! Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial