Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, Here is the code I have so far. The program should be outputting \"Sorry,

ID: 3626392 • Letter: H

Question

Hello, Here is the code I have so far. The program should be outputting "Sorry, your input is invalid. Please re enter a valid name or userid or -1 to exit" when a special charcter is inputted. ex: *, !, $ etc. but it just exits the program.
Also, I can't get it to check the whole username before matching it with the user id, ex: I type in s and it says the user id is 1001 without going through each letter to check if it is "sam". Please help!! I'll give you the highest points if you could point out what's wrong and provide me with some sample code to fix it.

#include
#include
#include
#include
#include
#define DATAMAX 5

using namespace std;

struct Employee
{
char name[80];
int uid;
} database[DATAMAX]={{"bob",1000},{"sam",1001},{"tom",1002},{"sara",1003},{"lisa",1004}};

int findData(struct Employee e[], char* input)
{
int i=0;
int k=0;
int flag=0; // set this flag to -1 if it is an invalid name
int numsize=0; // set this flag to 1 if it is an invalid number
int alphasize=0;
string str;
// Search for uid based on name
str = input;
do {

if (!isalpha(input[i]) && !isspace(input[i]))
if (!isdigit(input[i]))
flag = -1 ; // contains a special character
else
{
flag = 1; // contains a digit
numsize++;
}

else
alphasize++;
i=i++;

} while ( input[i] != '');


if (flag == -1)
return -1;

else if (numsize == i && numsize < 5)
{
for (int j=0;j

if (e[j].uid == atoi(input))
{
cout << "Matched name for uid " << input << ":" << e[j].name;
return 0;
}
cout << "Sorry there's no matched name for uid: " << input;
return 0;
}

else if (alphasize==i)
{

for (int j=0;j
{

for (k=0;k
if (e[j].name[k] != input[k])
break;
if (k==i)
{
cout << "Matched uid for name " << input << ":" << e[j].uid ;
return 0;
}
}
cout << "Sorry there's no matched uid for the name: " << input;
}

else
return -1;
return 0;
}

int main ()
{
char input[80];
int x = 0 ;
int result = 0 ;

do {

if (result != -1)
cout << " Do you want to search by employee name or user id? -1 to exit " << endl;

else if (result =-1)
{
exit (-1);
}

else
{
cout << " Sorry, your input is invalid. Please re enter a valid name or userid or -1 to exit" << endl;
result = 0;
}


cin.getline(input,80,' ');

if (x != -1)
result = findData(database,input) ;

} while ( x !=-1);
return 0;
}

Explanation / Answer

First of all, how are you reading your input. You just pass in a char* for your method, but is it null terminated? You declare string str and then set str = input, but that doesn't null terminate it. Furthermore, exit() just exits the whole application. Read the specs so you understand what it does. if (result != -1) cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote