读书人

初学者的一个程序的有关问题

发布时间: 2012-04-05 12:42:40 作者: rapoo

菜鸟的一个程序的问题
各位大神,我想问一下这个程序哪里出了问题?

# include <stdio.h>
# include <string.h>
# define PASSWORD_SIZE 100
# define PASSWODD "myGOODpassword\n"
main()
{
int count=0,i;
char buff[PASSWORD_SIZE],*a,*b;
a=&buff[0];
b=&PASSWORD;
for(;;)
{
printf("Enter password:");
for(i=0;i<PASSWORD_SIZE;i++)
scanf("%c",&buff[i]);
if(strcmp(a,b))
printf("wrong password\n");
else break;
if(++count>3)
return -1;
}
printf("password ok\n");
}

[解决办法]

C/C++ code
参考下# include <stdio.h># include <string.h>#include<iostream>using namespace std;# define PASSWORD_SIZE 6# define PASSWORD "12345\n"int main(){    int count=0,i;    char buff[PASSWORD_SIZE+1],*a,*b;    a=&buff[0];    b=PASSWORD;    for(;;)    {        printf("Enter password:");        for(i=0;i<PASSWORD_SIZE;i++)            scanf("%c",&buff[i]);        buff[PASSWORD_SIZE] = '\0';        if(strcmp(a,b))            printf("wrong password\n");        else break;        if(++count>3)            return -1;    }    printf("password ok\n");    system("pause");}
[解决办法]
C/C++ code
# include <stdio.h># include <string.h># define PASSWORD_SIZE 100# define PASSWORD "myGOODpassword\n"//PASSWORDvoid main(){    int count=0,i;    char buff[PASSWORD_SIZE],*a,*b;    a=&buff[0];    b=PASSWORD;    for(;;)    {        printf("Enter password:");        for(i=0;i<PASSWORD_SIZE;i++)            scanf("%c",&buff[i]);        if(strcmp(a,b))            printf("wrong password\n");        else break;        if(++count>3)            return -1;    }    printf("password ok\n");} 

读书人网 >C语言

热点推荐