为什么程序执行一半就不执行了呢?
zxywd 2010-04-02 08:51:13 大家好!我遇到了一个问题,程序执行了一部分就不执行了。代码如下:
stud-mysql.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <mysql/mysql.h>
#include <syslog.h>
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_FIELD *fields;
int main()
{
char *host="localhost";
char *user=malloc(30);
char *password=malloc(30);
char *db=NULL;
char answer;
unsigned int i;
unsigned int num_fields;
int m,n;
printf("请先登录!\n");
login:
printf("请输入用户名:\n");
scanf("%s",user);
printf("请输入密码:\n");
scanf("%s",password);
if(mysql_init(&mysql)==NULL)
{
printf("初始化MySQL数据库错误!\n");
return -1;
}
if(mysql_real_connect(&mysql,host,user,password,db,0,NULL,0)==NULL)
{
printf("连接MySQL数据库错误:%s!",mysql_error(&mysql));
printf("想重新登录吗?输入y/Y确认\n");
scanf("%c",&answer);
if(answer=='y'||answer=='Y')
goto login;
else return -1;
}
}
else printf("登录成功!请进行下一步操作。\n");
return 0;
}
执行结果:
请先登录!
请输入用户名:
sdfsdfs
请输入密码:
sdfsfds
连接MySQL数据库错误:Access denied for user 'sdfsdfs'@'localhost' (using password: YES)!想重新登录吗?输入y/Y确认
到这一步后scanf("%c",&answer);不执行了。我想重新登录也不行。我把else return -1;中的else去掉也不行,把这行代码注释掉也不行。这是怎么回事?麻烦各位帮忙解决一下。