Efficency of my user pass prgram
Oct 15, 2014 at 5:23am UTC
So I was using a practice problem from Jumping into C++, and it works, Without the correct password combo the "username and password correct!" dosent come up, I was just curious to how effecient it is (if efficient at all) Im still learning but I would like honest criticsm. What bits can I cut, what should I add.
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
#include <iostream>
#include <string>
using namespace std;
string GetUser(){
string User;
cout << "Please enter username : " << endl;
cin >> User;
return User;
}
int GetPass(){
int Pass;
cout << "Please enter Password : " << endl;
cin >> Pass;
return Pass;
}
int CheckUser(string UserTry){
int UserNumb = 10;
string UserData [10];
UserData [0] = "Thatkid14" ;
UserData [1] = "DeathCultist" ;
UserData [2] = "DragonPro" ;
UserData [3] = "Wowzers32" ;
UserData [4] = "NahBrah" ;
UserData [5] = "Lolikilu99" ;
UserData [6] = "CrackBaby93" ;
UserData [7] = "FDatShat" ;
UserData [8] = "GoLeft" ;
UserData [9] = "Eminem" ;
while ( UserNumb == 10 )
for ( int i = 0; i < 9; i++ ){
if (UserData [i] == UserTry){
UserNumb = i;
}
}
if (UserNumb > 0 && UserNumb < 9){
return UserNumb;
}
}
int CheckPass(int PassTry){
int PassNumb = 10;
int PassData [10];
PassData [0] = 2394;
PassData [1] = 4253;
PassData [2] = 1256;
PassData [3] = 8798;
PassData [4] = 1023;
PassData [5] = 6403;
PassData [6] = 9812;
PassData [7] = 4985;
PassData [8] = 5463;
PassData [9] = 7745;
while ( PassNumb == 10 )
for ( int i = 0; i < 9; i++ ){
if (PassData [i] == PassTry){
PassNumb = i;
}
}
if (PassNumb > 0 && PassNumb < 9){
return PassNumb;
}
}
bool VerifyLogin(int User, int Pass){
if (User == Pass){
return 1;
}
}
int main(){
string UserAttempt;
int PassAttempt;
int UserCorrect;
int PassCorrect;
bool Verified;
UserAttempt = GetUser();
PassAttempt = GetPass();
UserCorrect = CheckUser(UserAttempt);
PassCorrect = CheckPass(PassAttempt);
Verified = VerifyLogin(UserCorrect, PassCorrect);
if (Verified = 1){
cout << "Username and password correct!" << endl;
}
}
Last edited on Oct 15, 2014 at 5:49am UTC
Oct 15, 2014 at 6:53am UTC
increase efficiency?
you may write like this in main function
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
string UserAttempt;
int PassAttempt;
int UserCorrect;
bool Verified;
int PassData [10];
PassData [0] = 2394;
PassData [1] = 4253;
PassData [2] = 1256;
PassData [3] = 8798;
PassData [4] = 1023;
PassData [5] = 6403;
PassData [6] = 9812;
PassData [7] = 4985;
PassData [8] = 5463;
PassData [9] = 7745;
UserAttempt = GetUser();
PassAttempt = GetPass();
UserCorrect = CheckUser(UserAttempt);
if (userCorrect>0&&userCorrect<9&&PassData[userCorrect]==PassAttempt)
{
cout << "Username and password correct!" << endl;
}
else
{
//error
}
the number of comparation will decrease to 10
Topic archived. No new replies allowed.