我的代码无法正常工作,我认为它的逻辑错误,但我没有得到它
My code is not working properly,its logical error i think but i'm not getting it
它无法正常工作,因为我希望它工作..根据我的想法,下面给出的代码总是正确的,我不知道为什么,但如果条件总是为真并且数字匹配..名为CNIC的变量是这样全局声明的(char CNIC[15])。
private:
char ch;
char str[256];
std::fstream file1;
public:
void verify()
{
cout<<"Enter your CNIC number for verifivation : ";
for(int i=0;i<15;i++)
{
CNIC[i] = getche();
}
file1.open("D:\UOL\OoP\Nadra database.txt",ios::in);
check = false;
while(!file1.eof())
{
file1.getline(str, 255);
if(check = strcmp(str, CNIC) == 0);
check=true;
}
file1.close();
if(check)
{
cout<<endl<<"CNIC number matched"<<endl;
}
else
{
cout<<endl<<"CNIC number did'nt match " ;
}
}
};
您的代码包含几个错误。
if 语句由 ;
终止,因此check
将始终设置为 true
// the ';' terminates the if statement
if(check = strcmp(str, CNIC) == 0);
// check will always be set to true
check=true;
您需要删除分号,将代码更改为:
if(strcmp(str, CNIC) == 0) {
check=true;
// my guess would be that you really like to break the loop here
break;
}
我还必须在 for 循环之后添加一个终止空字符CNIC
,您需要在变量中为 16 个字符腾出空间,即char CNIC[16];
为空字符腾出空间:
for(int i=0;i<15;i++)
{
CNIC[i] = getche();
}
// add null character
CNIC[15] = 0;
相关文章:
- 错误处理.将系统错误代码映射到泛型
- 为什么我在使用void函数时得到错误代码C2276
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 逻辑运算符上出现错误代码 a')'
- 'val' Arduino 错误代码之前的预期'('
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 根据 GetLastError 直接写入磁盘会导致错误代码 5
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- WSALookupServiceBegin() 上的错误代码 10022(无效的 arugment)
- C++ Schannel POST 400 错误代码错误请求
- openCL-创建子缓冲区返回错误代码13
- 更新了Runge-Kutta(RK4)C++错误代码中的二阶DE
- 获取有关使用未初始化内存的错误代码
- 排序程序的意外错误代码
- 正在将DJI错误代码记录到流中
- 来自API的错误代码..处理什么是好的做法