Login Program

first off some introduction I have extremely basic c++ programming experience. I

am currently taking a game programming class as an elective in high school but

we have moved from C++ to Game Maker which to me seemed like learning to write

a book and then saying instead of writing a book lets use someone elses writing

to make the book and draw some pictures to go along with it...

anyway i am currently done with AP testing and decided to get back in to c++

because it seemed like it had so much to offer.

so that is why i am here. I have hit a roadblock early on in my program and

need some help with strings in the hierarchy section

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
// login with username & password

#include <iostream>
#include <string>

using namespace std;

// program variables
string beta;
int privelage;

//program start sequence
int programstart ()
{
int alpha;
   cout <<"\t Password Program" << endl;
   cin >> alpha;  
}
// end start sequence


// Hierarchy

int hierarchy ()
{
  
   // set privelage for usernames
   if (beta = 'root')
   {
   privilege = 0;
   }
   else if (beta = 'wasing')
   {
   privilege = 1;
   }
   else if (beta = 'guest')
   {
   privilege = 2;
   }
   else
   {
   privilege = 3;
   }
}

// End Hierarchy

//Clear Screen

int screenclear ()
{
    cout << string(50, '\n');
}

// Pause the Screen
int screenpause ()
{
    int alpha;
    cout <<" Press Enter to Continue... "<< endl;
    cin >> alpha;
}

// main program
int main ()
{

programstart ();
hierarchy ();
screenclear ();
screenpause ();

    return 0;
}


i am trying to set up the fuction so that when you type in "root" your

privelage for the program is zero however i am using dev c++ (what we used in

school) and it returned

"27 Login Program.cpp could not convert `(&x)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)"root"))' to `bool' "

which to me means it was trying to convert the string into a true or false

value. if you can show me what i am doing wrong it would be highly appreciated


thank you for your time,

Wa Sing

Last edited on
Use double quotes with strings, and don't forget that the operator for equality testing is '==' and not '=':

if (beta = 'root') should be if (beta == "root")

Also, you declare int privelage; and then you use it like privilege = 0;
hey thanks for some reason i dont know why i just remembered using single quotes for strings also

i declared privilege at the beginning of the program however i spelled it wrong the first time lol :)

thanks for catching that.
I took a second look at your program.

1
2
3
4
5
6
7
8
9
//program start sequence
int programstart ()
{
   //int alpha; <- you don't need this!
   cout <<"\t Password Program" << endl;
   //cin >> alpha; <- you don't want to get an integer
   cin >> beta; //<- you want to get a string 
}
// end start sequence 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// main program
int main ()
{
    programstart ();
    hierarchy ();
    screenclear ();
    
    //you may want to add this here...
    cout << "your privilege: " << privilege << endl;

    screenpause ();

    return 0;
}
Last edited on
yeah thanks i was looking at ways to pause the screen while i worked on it in Dev C++ that didnt

work out well so i used duoas code for pausing it instead thanks for the input however ill post my

complete code here soon if i dont run into anymore roadblocks thanks for your help and input

m4ster r0shi
my code so far somethings wrong with the do while loop but im working on it

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
// login with username & password
// establish clear screen and pause
// start program screen
// login (username & password) prompt
// establish privelages of each screen name
// password ** fix after completion to read pass from file **



#include <iostream>
#include <string>
#include <limits>

using namespace std;

// program variables
string alpha, beta;
// alpha is password, beta is username
int privilege = 0;

//program start sequence
int programstart ()
{
   cout <<"\t Password Program" << endl;
}
// end start sequence



// login
int login ()
{
    // login prompt
    do
    {
    cout <<" \t Login \n" <<endl;
    cout <<" For Guest Access **Username: Guest** **Password: Guest** \n";
    cout <<"Username:";
    cin >> beta;
    cout <<"\nPassword:";
    cin >> alpha;
    // repeat login until login successful
    }while (privilege == 0);
    
}

// Hierarchy

int hierarchy ()
{
  
   // set privelage for usernames and passwords
   if (beta == "root" && alpha == "admin")
   {
   privilege = 1;
   }
   else if (beta == "wasing" && alpha == "user" )
   {
   privilege = 2;
   }
   else if (beta == "guest" && alpha == "guest" )
   {
   privilege = 3;
   }
   else
   {
   cout <<"\nInvalid Username Or Password ";
   privilege = 0;
   }
}   
// End Hierarchy


// Tell What your Current Privelage Level Is
int rights ()
{
    if (privilege !=0)
    {
    // see if i can use a switch to replace these if statements
    if (privilege = 1)
    {
    cout << "you have root (administrator) access";      
    }
    else if (privilege = 2)
    {
    cout << "you have normal access";      
    }
    else if (privilege = 3)
    {
    cout << "you have restricted guest access";      
    }
    else 
    {
    cout << "how'd you do that? Shenanigans";
     }
     }
    
}

//Clear Screen

int screenclear ()
{
    cout << string(50, '\n');
}

// Pause the Screen
int screenpause ()
{
   // duoas pause code
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

}

// main program
int main ()
{

programstart ();
screenpause();
screenclear();
login();
hierarchy ();
rights();
screenpause ();

    return 0;
}


feel free to comment you wont hurt my feelings i know im a noob lol
Remove the loop from the login function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// login
int login ()
{
    // login prompt
    //do
    //{
    cout <<" \t Login \n" <<endl;
    cout <<" For Guest Access **Username: Guest** **Password: Guest** \n";
    cout <<"Username:";
    cin >> beta;
    cout <<"\nPassword:";
    cin >> alpha;
    // repeat login until login successful
    //}while (privilege == 0);    
}

And put it in your main function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// main program
int main ()
{

programstart ();
screenpause();
screenclear();

do{
login();
hierarchy ();
}while (privilege==0);

rights();
screenpause ();

    return 0;
}

EDIT: Also don't forget to test equality in the proper way (i.e. with '==' and not '=')

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
// Tell What your Current Privelage Level Is
int rights ()
{
    if (privilege !=0)
    {
    // see if i can use a switch to replace these if statements
    if (privilege == 1) //<- here!
    {
    cout << "you have root (administrator) access";
    }
    else if (privilege == 2) //<- and here!
    {
    cout << "you have normal access";
    }
    else if (privilege == 3) //<- and here!
    {
    cout << "you have restricted guest access";
    }
    else
    {
    cout << "how'd you do that? Shenanigans";
     }
     }

}

Last edited on
hey thanks m4ster r0shi the do while loop works now however the privilege is not changing causing it to keep looping

here is my revised code

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
// login with username & password
// establish clear screen and pause
// start program screen
// login (username & password) prompt
// establish privelages of each screen name
// password ** fix after completion to read pass from file **



#include <iostream>
#include <string>
#include <limits>

using namespace std;

// program variables
string alpha, beta;
// alpha is password, beta is username
int privilege = 0;

//program start sequence
int programstart ()
{
   cout <<"\t Password Program" << endl;
}
// end start sequence



// login
int login ()
{
    // login prompt
    cout <<"\n\t Login \n" <<endl;
    cout <<" For Guest Access \n";
    cout <<"**Username: Guest**\n";
    cout <<"**Password: Guest** \n";
    cout <<"Username:";
    cin >> beta;
    cout <<"\nPassword:";
    cin >> alpha;
    
}

// Hierarchy

int hierarchy ()
{
  
   // set privelage for usernames and passwords
   if (beta == "root" && alpha == "admin")
   {
   privilege == 1;
   }
   else if (beta == "wasing" && alpha == "user" )
   {
   privilege == 2;
   }
   else if (beta == "guest" && alpha == "guest" )
   {
   privilege == 3;
   }
   else
   {
   cout <<"\nInvalid Username Or Password\n";
   privilege == 0;
   }
}   
// End Hierarchy


// Tell What your Current Privelage Level Is
int rights ()
{
    if (privilege !=0)
    {
    // see if i can use a switch to replace these if statements
    if (privilege == 1)
    {
    cout << "you have root (administrator) access";      
    }
    else if (privilege == 2)
    {
    cout << "you have normal access";      
    }
    else if (privilege == 3)
    {
    cout << "you have restricted guest access";      
    }
    else 
    {
    cout << "how'd you do that? Shenanigans";
     }
     }
    
}

//Clear Screen

int screenclear ()
{
    cout << string(50, '\n');
}

// Pause the Screen
int screenpause ()
{
   // duoas pause code
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}


// main program
int main ()
{
programstart ();
screenpause();
screenclear();
do
{
login();
hierarchy ();
}while (privilege == 0);
screenpause();
rights();
screenpause ();


return 0;
}


the problem i believe is in this section

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int hierarchy ()
{
  
   // set privelage for usernames and passwords
   if (beta == "root" && alpha == "admin")
   {
   privilege == 1;
   }
   else if (beta == "wasing" && alpha == "user" )
   {
   privilege == 2;
   }
   else if (beta == "guest" && alpha == "guest" )
   {
   privilege == 3;
   }
   else
   {
   cout <<"\nInvalid Username Or Password\n";
   privilege == 0;
   }
}   


i found that it works if i set the equality to

1
2
3
4
 else if (beta == "guest" && alpha == "guest" )
   {
   privilege += 3;
   }


but i also want to add a logout portion so i can set

the privilege back to zero no matter which privilege i had. which i can still do but would require a

switch statement rather than just setting privelages == 0 again

sry for the long post and i really appreciate your help m4ster r0shi
Ok, look, when you want to assign a value to a variable you must use the '=' operator. And when you want to test equality you must use the '==' operator. If you understand these basic things you can find the answer on your own.

Info on '=' , '==' and other operators -> http://cplusplus.com/doc/tutorial/operators/
Topic archived. No new replies allowed.