Link to home
Start Free TrialLog in
Avatar of Sargie93
Sargie93

asked on

Add a menu system

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.
Avatar of jkr
jkr
Flag of Germany image

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

Avatar of Sargie93
Sargie93

ASKER

@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;
....
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

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?
Could you post your actual code and the full error message?
Post your entire code
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?)
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)
Oh right, sorry about that.... Okay so I've updated it like you said, but it's the same issue.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MASSIVE help, exactly what I needed! Thank you