Security code not working

I tried to create a code that would ask a used for a authorization code and then their usename in a Windows Concole Application. I came up with the following code:

security.cpp:

#include "stdafx.h"
#include "security.h"

void AccessSuccess(char NameofUser[100])
{
std::cout << "Welcome " << NameofUser << ",\n"
<< "You may now use this program" << std::endl;
}

int CodeMatcher()
{
if (UserInput.CodeNumber == "Code-Code" ||
UserInput.CodeNumber == "CodeCode")
return true;
else
return false;
}

int main()
{
std::cout << "Please enter authorization code: ";
while (Letter <= 100)
{
LoginFail:
std::cin >> UserInput.CodeNumber[Letter];
if (UserInput.CodeNumber[Letter] == ';')
{
UserInput.CodeNumber[Letter] = 0;
if (CodeMatcher() == true)
{
std::cout << "Enter username:" << std::endl;
Letter = 0;
while (Letter <= 100)
{
std::cin >> UserInput2.UserNumber[Letter];
if (UserInput2.UserNumber[Letter] == ';')
{
UserInput2.UserNumber[Letter] = 0;
AccessSuccess(UserInput2.UserNumber);
}
}
}
else if (!CodeMatcher())
{
std::cout << "Failed to varify authorization code\n"
<< "Please enter vaild code: ";
Letter = 0;
goto LoginFail;
}
}
++Letter;
}
if (CodeMatcher())
{
std::cout << "Enter username:" << std::endl;
Letter = 0;
while (Letter <= 100)
{
std::cin >> UserInput2.UserNumber[Letter];
if (UserInput2.UserNumber[Letter] == ';')
{
UserInput2.UserNumber[Letter] = 0;
AccessSuccess(UserInput2.UserNumber);
}
}
}
else if (!CodeMatcher())
{
std::cout << "Failed to varify authorization code\n"
<< "Please enter vaild code: " << std::endl;
Letter = 0;
goto LoginFail;
}
}

security.h:

//Headers
#include <iostream>

//Global Variables
struct Code
{
char CodeNumber[100];
};

struct Username
{
char UserNumber[100];
};

Code UserInput;
Username UserInput2;
int Letter = 0;

And the other files I haven't modified. For some reason when I run this code I do the following:

Please enter authorization code: Code-Code;

and the outcome is the following:

Failed to varify authorization code
Please enter vaild code:

Can anyone help me fix this code?
closed account (S6k9GNh0)
I'll reply when you put your code in CODE BBCODE.
Your input is char array, the operater '==' is compare two 'char *' pointer, one point to "Code-Code", and another point to 'UserInput.CodeNumber[]'.
1
2
3
4
5
6
7
8
int CodeMatcher()
{
if (UserInput.CodeNumber == "Code-Code" || 
UserInput.CodeNumber == "CodeCode")
return true;
else
return false;
}

You should change the UserInput to class 'string'
1
2
3
4
5
6
7
8
int CodeMatcher()
{
if (0 == strcmp(UserInput.CodeNumber, "Code-Code") || 
0 == strcmp(UserInput.CodeNumber, "CodeCode"))
return true;
else
return false;
}

I think the UserInput use 'string' class is better.
ewan
Topic archived. No new replies allowed.