gpt4 book ai didi

比较字符串和指针?比较 C 中的字符串

转载 作者:行者123 更新时间:2023-11-30 20:35:20 24 4
gpt4 key购买 nike

这是我的学校作业代码,现在我的问题出在我的 inputID 函数上。如果评论说“如果相同!!!!!!!!!!!!!!!!!!!!!!!!!!!”,我尝试比较用户给出的字符串和字符串存储在我的字符串数组“IDArray”中。我尝试使用 strcmp 函数,但是我不断收到错误。任何对此的帮助将不胜感激。它读取的文本文件的内容显示在代码下方。谢谢您

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_RECORDS 1000
#define MAX_INPUT 40

void writeFile();
void inputPass();
void inputID();
void userInput();
void printDB();
void readFile();
void inputInit();
void DBInit();
void init();

FILE *fp;

char **IDArray;
char **passwordArray;

char *IDInput;
char *passInput;

int main()
{
init();
readFile();

printf("\n\n\tWelcome to CPS_633, Lab 1\t\n\n");

userInput();
writeFile();
printDB();

return 0;
}

void writeFile()
{
fp = fopen("Database_Table.txt", "w");
int i;
for (i = 0; i < MAX_RECORDS; i++)
{
if (IDArray[i][0] != '\0')
{
fprintf(fp, "%s\t%s\n", IDArray[i], passwordArray[i]);
}
else
{
break;
}
}
fclose(fp);
}

void printDB()
{
printf("\nUsername\tPassword\n");
int i;
int databaseLength = 0;

for (i = 0; i <= MAX_RECORDS; i++)
{
if (IDArray[i][0] != '\0')
{
printf("%s\t\t%s\n", IDArray[i], passwordArray[i]);
}
else
{
break;
}
}
}

void inputPass()
{
int correct = 1, strLength;

printf("Please Enter Your Password (no special characters): ");
fgets(passInput, MAX_INPUT, stdin);
strLength = strlen(passInput) - 1;

if (strLength > 0 && passInput[strLength] == '\n')
{
passInput[strLength] = '\0';
}

while (correct)
{
int k;
int specialCase = 1;
for (k = 0; k <= strLength; k++) //Searches for special characters
{
switch (passInput[k])
{
case '~':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '!':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '`':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '@':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '#':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '$':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '%':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '^':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '&':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '*':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '(':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ')':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '-':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '_':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '=':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '+':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '[':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ']':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '{':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '}':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '|':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '\\':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ':':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ';':
printf("Special Character(s) Found\n");
specialCase = 0;
break;

case '"':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case ',':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '<':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '.':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '>':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '?':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
case '/':
printf("Special Character(s) Found\n");
specialCase = 0;
break;
}
if (specialCase == 0)
break;
}
if (specialCase != 0)
{
if (strLength > 12) //Password longer than 12 characters
{
printf("This password is too long, the characters after the 12th has been cut off\n");
int i;
for (i = 0; i < MAX_RECORDS; i++)
{
if (passwordArray[i][0] == '\0')
{
strncpy(passwordArray[i], passInput, 12);
break;
}
else
{
continue;
}
}
correct = 0;
}
else //Password shorter than 12 characters
{
int i, j;
for (j = strLength; j <= 12; j++) //Pads password with "-"
{
passInput[j] = '-';
}

for (i = 0; i < MAX_RECORDS; i++) // Traverses array for first empty slot
{
if (passwordArray[i][0] == '\0') //If empty insert
{
strncpy(passwordArray[i], passInput, 12);
break;
}
else // If not empty continue
{
continue;
}
}
correct = 0;
}
}
else
{
printf("Please Enter Your Password (no special characters): ");
fgets(passInput, MAX_INPUT, stdin);
strLength = strlen(passInput) - 1;

if (strLength > 0 && passInput[strLength] == '\n')
{
passInput[strLength] = '\0';
}
}
}
}

void inputID()
{
int correct = 1, strLength;

printf("Please Enter Your Username ID: ");
fgets(IDInput, MAX_INPUT, stdin);
strLength = strlen(IDInput) - 1;

if (strLength > 0 && IDInput[strLength] == '\n')
IDInput[strLength] = '\0';

while (correct)
{
if (strLength > 32) //If longer than 32 characters
{
printf("This Username ID is longer than 32 characters\n");
printf("Please Enter Your Username ID: ");
fgets(IDInput, MAX_INPUT, stdin);

strLength = strlen(IDInput) - 1;

if (strLength > 0 && IDInput[strLength] == '\n')
IDInput[strLength] = '\0';
}
else if (strLength < 4) //If shorter than 4 characters
{
printf("This Username ID is shorter than 4 characters\n");
printf("Please Enter Your Username ID: ");
fgets(IDInput, MAX_INPUT, stdin);

strLength = strlen(IDInput) - 1;

if (strLength > 0 && IDInput[strLength] == '\n')
IDInput[strLength] = '\0';
}
else //If acceptable length
{
int i;
for (i = 0; i < MAX_RECORDS; i++)
{
if (IDArray[i][0] != '\0') //If element occupied, compare
{
if (strcmp(IDArray[i], inputID) == 0) // If the same!!!!!!!!!!!!!!!!!!
{
printf("Found Match");
correct = 0;
break;
}
else //If not the same
{
continue;
}
}
else //If element empty, insert
{
strcpy(IDArray[i], IDInput);
break;
}
}
correct = 0;
}
}
}

void userInput()
{
inputID();
inputPass();
}

void readFile()
{
fp = fopen("Database_Table.txt", "r");

char line[MAX_INPUT];
if (fp == NULL)
{
perror("Error in opening file");
}
else
{
int i = 0;
while (!feof(fp))
{
if (fgets(line, sizeof(line), fp) == NULL)
{
break;
}
else
{
sscanf(line, "%s\t%s", IDInput, passInput);
strcpy(IDArray[i], IDInput);
strcpy(passwordArray[i], passInput);
i++;
}
}
}
fclose(fp);
}

void inputInit()
{
IDInput = (char *)malloc(sizeof(char) * MAX_INPUT);
passInput = (char *)malloc(sizeof(char) * MAX_INPUT);
}

void DBInit()
{
IDArray = (char **)malloc(sizeof(char *) * MAX_RECORDS);
passwordArray = (char **)malloc(sizeof(char *) * MAX_RECORDS);

int i, j;
for (i = 0; i < MAX_RECORDS; i++)
{
IDArray[i] = (char *)malloc(sizeof(char) * MAX_INPUT);
passwordArray[i] = (char *)malloc(sizeof(char) * MAX_INPUT);
for (j = 0; j < MAX_INPUT; j++)
{
IDArray[i][j] = '\0';
passwordArray[i][j] = '\0';
}
}
}

void init()
{
DBInit();
inputInit();
}
<小时/>

ID11 密码1

ID22密码2

ID33密码3

ID44密码4

ID55密码5

ID55密码5

最佳答案

第 325 行(带有 that 注释的行)有一个拼写错误:您要比较的是 IDInput,而不是 inputID是它所在函数的名称。

关于比较字符串和指针?比较 C 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39822866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com