c - 登录时没有匹配的二维字符串数组
问题描述
我正在解决我在一本关于 C 编程的书中看到的示例。我在本书中看到的这个特殊问题是关于匹配一种对我来说没问题的“客户代码”(整数)。但是我在为要证明我的登录名的二维字符串应用相同的逻辑时遇到了麻烦。
这是书上的,挺好的。
/*
This program searches a sorted list of customer IDs in order to get credit totals
*/
#include <stdio.h>
int main()
{
int ctr; // Loop counter
int idSearch; // Customer to look for (the key)
int found = 0; // 1 (true) if customer is found
/* Defines the 10 elements in each of the parallel arrays */
int custID[10] = {313, 453, 502, 101, 892, 475, 792, 912, 343, 633};
float custBal[10]={0.00, 45.43, 71.23, 301.56, 9.08, 192.41, 389.00, 229.67, 18.31, 59.54};
int tempID, inner, outer, didSwap, i; // For sorting
float tempBal;
// First, sort the arrays by customer ID
for (outer=0; outer<9; outer++)
{
didSwap=0; // Becomes 1 (true) if list is not yet ordered
for (inner=outer; inner<10; inner++)
{
if (custID[inner] < custID[outer])
{
tempID = custID[inner]; // Must switch both arrays of they
tempBal = custBal[inner]; // are no longer linked
custID[inner] = custID[outer];
custBal[inner] = custBal[outer];
custID[outer] = tempID;
custBal[outer]=tempBal;
didSwap = 1; // True because a swap took place
}
}
if (didSwap == 0)
{
break;
}
}
/* Interact with the user looking to find a balance */
printf("\n\n*** Customer Balance Lookup ***\n");
printf("What is the customer number? ");
scanf(" %d", &idSearch);
// Now, look for the ID in the array
for (ctr=0; ctr<10; ctr++)
{
if (idSearch == custID[ctr]) // Do they match?
{
found = 1; // Yes, match flag is set to TRUE
break; // No need to keep looping
}
if (custID[ctr] >idSearch) // No need to keep searching
{
break;
}
}
// Once the loop has completed, the ID was either found
// (found = 1) of not
if (found)
{
if (custBal[ctr]>100)
{
printf("\n** That customer's balance is $%.2f.\n", custBal[ctr]);
printf("No additional credit.\n");
}
else // Balance is less than $100.00
{
printf("\n**The customer's credit is good!\n");
}
}
else // The ID was not found
{
printf("** You have entered an incorrect customer ID.");
printf("\n ID %3d was not found in the list.\n", idSearch);
}
return(0);
}
这是我为了登录而尝试创建的。
#include <stdio.h>
#include <string.h>
int main()
{
int ctr; // Loop counter
char userSearch[20], passSearch[10]; // to look for (the key)
int found = 0; // 1 (true) if it is found
/* Defines the 5 elements in each of the parallel arrays */
char user[5][20] = {"John", "Ivan", "Paul", "Robert", "Leo"};
char pass[5][10]={"1111", "2222", "3333", "4444", "5555"};
int inner, outer, didSwap, i; // For sorting
char tempUser, tempPass;
for (outer=0; outer<4; outer++)
{
didSwap=0; // Becomes 1 (true) if list is not yet ordered
for (inner=outer; inner<5; inner++)
{
if (user[inner] != user[outer])
{
strcpy(tempUser, user[inner]);
strcpy(tempPass, pass[inner]);
strcpy(user[inner], user[outer]);
strcpy(pass[inner], pass[outer]);
strcpy(user[outer], tempUser);
strcpy(pass[outer], tempPass);
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
}
printf("User: ");
scanf(" %s", userSearch);
printf("Password: ");
scanf(" %s", passSearch);
for (ctr=0; ctr<5; ctr++)
{
if ((strcmp(userSearch, user[ctr])==0) && (strcmp(passSearch, pass[ctr])==0)) // Do they match?
{
found = 1;
break;
}
}
if (found)
{
printf("You entered sucessfully.");
}
else
{
printf("** You have entered an incorrect data.");
printf("\n %s was not found.\n", userSearch);
}
return(0);
}
我的最后一个代码到底有什么问题?如何使用二维字符串数组进行正确登录?例如,我想放置 100 个用户,并且指向每个用户不是一个选项,所以我认为这种方式是一个好主意,使用这个算法。但不确定我的错误是什么。
解决方案
推荐阅读
- youtube - YouTube 数据 API 的问题:有时会崩溃并且无法正常工作
- api - 如何在使用 POSTMAN 发送另一个请求之前触发请求
- symfony - Symfony 和 Doctrine 在不同项目上的交流
- python - Ansible 和 python 实现
- android - Android 单元测试 Mockito 返回 null
- c# - 如何从 .NET Core 中的 appsettings.json 读取节值
- java - 如何使用单选按钮更改android应用程序中的设置?
- mysql - 从 MySQL 获取每小时数据
- javascript - 导入的网格没有碰撞 - Babylon.js
- algorithm - 如何以每位球员的最大休息时间对循环赛进行排序?