Simple Menu Program
Apr 12, 2016 at 7:31am UTC
Hi everyone
I'm very new to C++ but I am learning !
I have written a program to give the user a menu with choices and interaction, however, I have a problem with it. The aim was to teach myself how functions and global variables work.
The problem is that if the user enters a username and password and then logs in with it a selection of exit shows the login lines again and I can't work out why !! Seems like it goes to the login function again ?? Any help ??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#include <iostream>
#include <string>
using namespace std;
int selection = 0;
string username = "0" ;
string password = "0" ;
int launchcode = 1;
void menu ();
void setpassword ();
void setlaunch ();
void login();
void launch ();
void exit ();
int main()
{
cout << endl;
menu ();
}
void menu ()
{
cout << endl;
selection = 0;
cout << " Please choose from the following options - \n" ;
cout << " 1. Set username and password.\n" ;
cout << " 2. Set launch codes.\n" ;
cout << " 3. Login.\n" ;
cout << " 4. Enter launch codes.\n" ;
cout << " 5. Exit.\n" ;
cout << "\t" ;
cin >> selection;
if (selection == 1)
{
setpassword ();
}
else if (selection == 2)
{
setlaunch ();
}
else if (selection == 3)
{
login();
}
else if (selection == 4)
{
launch();
}
else if (selection == 5)
{
exit ();
}
}
void setpassword()
{
cout << " Please enter new username - " ;
cin >> username;
cout << " Please enter new password - " ;
cin >> password;
menu ();
}
void setlaunch()
{
cout << " Please enter a 4 digit launch code - " ;
cin >> launchcode;
menu ();
}
void login()
{
string usertest;
string passtest;
if (username != "0" | password != "0" ) //make sure a username/password has been set
{
for ( int i = 0; i < 5; i++)
{
cout << " Please enter username - " ;
cin >> usertest;
cout << " Please enter password - " ;
cin >> passtest;
if ( username != usertest | password != passtest)
{
cout << " USERNAME OR PASSWORD NOT RECOGNISED\n" ;
}
else
{
cout << " Credentials Accepted\n" ;
menu ();
}
}
menu ();
}
else
{
cout << " NO USERNAME OR PASSWORD SET." ;
menu ();
}
}
void launch ()
{
int launchtest;
if (launchcode != 1)
{
cout << " Please enter launch code - " ;
cin >> launchtest;
if (launchtest == launchcode)
{
cout << " MISSILES LAUNCHED" ;
menu ();
}
else
{
cout << " Launch code incorrect" ;
menu ();
}
}
else
{
cout << " NO LAUNCH CODE SET\n" ;
menu ();
}
}
void exit ()
{
cout << " See ya !\n" ;
}
Apr 12, 2016 at 9:15am UTC
Apr 12, 2016 at 10:44am UTC
Thanks chicofeo
I came up with this that seems to work. Anything I'm doing wrong here ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#include <iostream>
#include <string>
using namespace std;
int k = 0;
int selection = 0;
string username = "0" ;
string password = "0" ;
int launchcode = 1;
void menu ();
void setpassword ();
void setlaunch ();
void login();
void launch ();
void exit ();
void menu ()
{
cout << endl;
cout << " Please choose from the following options - \n" ;
cout << " 1. Set username and password.\n" ;
cout << " 2. Set launch codes.\n" ;
cout << " 3. Login.\n" ;
cout << " 4. Enter launch codes.\n" ;
cout << " 5. Exit.\n" ;
cout << " " ;
cin >> selection;
if (selection == 1)
{
setpassword ();
}
else if (selection == 2)
{
setlaunch ();
}
else if (selection == 3)
{
login();
}
else if (selection == 4)
{
launch();
}
else if (selection == 5)
{
exit ();
}
}
void setpassword()
{
cout << " Please enter new username - " ;
cin >> username;
cout << " Please enter new password - " ;
cin >> password;
cout << endl;
cout << " ** NEW USER CREDENTIALS ACCEPTED **\n" ;
}
void setlaunch()
{
cout << " Please enter a new launch code - " ;
cin >> launchcode;
cout << endl;
cout << " ** NEW LAUNCH CODE ACCEPTED **\n" ;
}
void login()
{
string usertest;
string passtest;
if (username != "0" | password != "0" ) //make sure a username/password has been set
{
for ( int i = 0; i < 5; i++)
{
cout << " Please enter username - " ;
cin >> usertest;
cout << " Please enter password - " ;
cin >> passtest;
if ( username != usertest | password != passtest)
{
cout << endl;
cout << " USERNAME OR PASSWORD NOT RECOGNISED\n" ;
}
else
{
cout << endl;
cout << " Credentials Accepted\n" ;
break ;
}
}
}
else
{
cout << endl;
cout << " NO USERNAME OR PASSWORD SET.\n" ;
}
}
void launch ()
{
int launchtest;
if (launchcode != 1)
{
cout << endl;
cout << " Please enter launch code - " ;
cin >> launchtest;
if (launchtest == launchcode)
{
cout << endl;
cout << " ** MISSILES LAUNCHED **\n" ;
}
else
{
cout << endl;
cout << " Launch code incorrect\n" ;
}
}
else
{
cout << endl;
cout << " NO LAUNCH CODE SET\n" ;
}
}
void exit ()
{
cout << endl;
cout << " See ya !\n" ;
k = 1;
}
int main()
{
while (true )
{
cout << endl;
if (k == 1)
{
break ;
}
menu ();
}
return 0;
}
Apr 12, 2016 at 11:27am UTC
Just noticed you used a bit wise OR.
At line 76
Was it your intention?
Apr 12, 2016 at 11:34am UTC
I just wanted to ask if either of the 2 arguments is false. Should I use something else here?
Apr 12, 2016 at 1:48pm UTC
@daisy8
Yes. On lines 76 and 85, you should use || which means OR, as in this or that. A single | is what oren drobitsky said, a bit wise OR which is not what you want.
Apr 13, 2016 at 3:44am UTC
Thanks for that !
Topic archived. No new replies allowed.