| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 491 人关注过本帖
标题:一个关于gets函数的奇怪问题 无人能解··
只看楼主 加入收藏
GUnever
Rank: 2
等 级:论坛游民
帖 子:59
专家分:25
注 册:2012-3-3
结帖率:95.24%
收藏
已结贴  问题点数:5 回复次数:5 
一个关于gets函数的奇怪问题 无人能解··
#include<stdio.h>
typedef    struct worker
    {
        int num;
        char name[20];
        char sex;
        int age;
    }Linklist;
void main()
    {
        void menu();
        void ccode();
        menu();
    }
void menu()
{
        int choose,flag=0,i;
        char *p1;
        int *p2;            /*密码  和 标志  都改变其值*/
        char code[10],change[]="520",ch;
        p1=change;
        p2=&flag;
Start:                                                                      /*为了让密码 不在重置*/
        printf("                        Workers Information Query System\nPlease give the code to enter the System:");
        for(;flag==0;)
    {
        for(i=0;(ch=getch())!='\r'&&i<10;i++)                /*回车是/r 换行才是/n*/
        {
            code[i]=ch;
            printf("*");
        }
        code[i]='\0';                                     /*  记住在这里的后面加上一个结束的标识符*/
        if(strcmp(code,p1)==0)                    /*这里用不用指针都没有关系*/
        {
            flag=1;
        }
        else
            printf("\nSorry! the code you give is wrong,please re-enter it as confirmation:\n");
    }
        system("cls");
        printf("                          Workers Information Query System\nPlease choose the survice as follow:\n  1.Information for all workers\n  2.Workers search\n  3.Change the information of workers\n");
        printf("  4.Adding workers\n  5.Password reset\n  6.Dismiss workers\n  7 End");
        scanf("%d",&choose);
        switch(choose)
    {

        case 5:ccode(p1,p2);goto Start;


    }
}
void ccode(char *p1,int *p2)
{
    char old[10], new[10];
    printf("Please input the old password:");
    /*gets(old);*/
    scanf("%s",old);
    if(strcmp(old,p1)!=0)
    {
        menu();                    /*如果输入不正确 就要返回菜单 这个设置 应该在完善一下*/
    }
    else
    {
        printf("Please input the new password:");
        gets(new);               
        strcpy(p1,new);
        *p2=0;
    }
    printf("Succee!\n");
    system("pause");
    system("cls");

}


首先这是一个职工修改密码的系统 (暂时只有修改密码)
然后再修改密码的地方gets函数居然不起作用 系统并没有叫输入字符 直接就走过gets函数。貌似自动给了0; 后来给了将第一个gets换成了scanf函数才可以的
第2个gets还没有换  请问这是什么原因呢
搜索更多相关主题的帖子: void name include change 
2012-04-05 18:27
lonmaor
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:郑州
等 级:版主
威 望:75
帖 子:2637
专家分:6423
注 册:2007-11-27
收藏
得分:2 
我的机器上调试没发现问题,用gets(old),gets(new)
环境:win7 64+codeblocks

从不知道到知道,到知道自己不知道,成长的道路上脚步深深浅浅
2012-04-05 18:59
Ayiis
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:2
帖 子:1086
专家分:3063
注 册:2011-4-10
收藏
得分:2 
清~~~~~~一~~~~~~下~~~~~~缓~~~~~~存~~~~~~吧~~~~~~亲
程序代码:
void ccode(char *p1,int *p2)
{
    char old[10], new[10];
    printf("Please input the old password:");
    /*gets(old);*/
    scanf("%s",old);
    if(strcmp(old,p1)!=0)
    {
        menu();                    /*如果输入不正确 就要返回菜单 这个设置 应该在完善一下*/
    }
    else
    {
        fflush(stdin);   // notice here
        printf("Please input the new password:");
        gets(new);               
        strcpy(p1,new);
        *p2=0;
    }
    printf("Succee!\n");
    system("pause");
    system("cls");

}

  • 该单位
  • 正在被拖走
2012-04-05 21:16
GUnever
Rank: 2
等 级:论坛游民
帖 子:59
专家分:25
注 册:2012-3-3
收藏
得分:0 
回复 3楼 Ayiis
你运行gets 正常么?
2012-04-05 23:13
Ayiis
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:2
帖 子:1086
专家分:3063
注 册:2011-4-10
收藏
得分:0 
程序代码:
void ccode(char *p1,int *p2)
{
    char old[10], new[10];
    printf("Please input the old password:");
    fflush(stdin);    //here
    gets(old);       

 //   scanf("%s",old);
    if(strcmp(old,p1)!=0)
    {
        menu();                    /*如果输入不正确 就要返回菜单 这个设置 应该在完善一下*/
    }
    else
    {
        fflush(stdin);      //here
        printf("Please input the new password:");
        gets(new);               
        strcpy(p1,new);
        *p2=0;
    }
    printf("Succee!\n");
    system("pause");
    system("cls");

}

刚才网路不好编辑帖子很多次。语无伦次了。。。

[ 本帖最后由 Ayiis 于 2012-4-5 23:22 编辑 ]

  • 该单位
  • 正在被拖走
2012-04-05 23:17
x072816
Rank: 1
等 级:新手上路
帖 子:7
专家分:2
注 册:2012-3-15
收藏
得分:2 
我不知道你是什么编辑器,我用的是dev C++ 发现new是一个关键字,还有许多函数应用没有用头文件
/*网页上的程序*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>

typedef    struct worker
    {
        int num;
        char name[20];
        char sex;
        int age;
    }Linklist;
   
void ccode(char * , int *);
void menu();
   
int main(void)
    {
        void menu();
        void ccode();
        menu();
    }
   
void menu()
{
        int choose,flag=0,i;
        char *p1;
        int *p2;            /*密码  和 标志  都改变其值*/
        char code[10],change[]="520",ch;
        p1=change;
        p2=&flag;
Start:                                                                      /*为了让密码 不在重置*/
        printf("                        Workers Information Query System\nPlease give the code to enter the System:");
        for( ; flag == 0 ; )
        {
           for(i=0 ; ((ch=getch())!='\r') && (i < 10 ); i++)                /*回车是/r 换行才是/n*/
              {
                 code[i]=ch;
                 printf("*");
              }
           code[i]='\0';                                     /*  记住在这里的后面加上一个结束的标识符*/
           if(strcmp(code,p1)==0)                    /*这里用不用指针都没有关系*/
             {
                flag=1;
             }
           else
                printf("\nSorry! the code you give is wrong,please re-enter it as confirmation:\n");
        }
        system("cls");
        printf("                          Workers Information Query System\nPlease choose the survice as follow:\n  1.Information for all workers\n  2.Workers search\n  3.Change the information of workers\n");
        printf("  4.Adding workers\n  5.Password reset\n  6.Dismiss workers\n  7 End");
        scanf("%d",&choose);
        switch(choose)
         {

            case 5:ccode(p1,p2);goto Start;


         }
}

void ccode(char *p1 , int *p2)
{
    char old[10];
    char new1[10];
    printf("Please input the old password:");
    /*gets(old);*/
    scanf("%s",old);
    if(strcmp(old,p1)!=0)
    {
        menu();                    /*如果输入不正确 就要返回菜单 这个设置 应该在完善一下*/
    }
    else
    {
        printf("Please input the new password:");
        gets( new1 );              
        strcpy(p1 , new1);
        *p2=0;
    }
    printf("Succee!\n");
    system("pause");
    system("cls");

}
2012-04-06 00:08
快速回复:一个关于gets函数的奇怪问题 无人能解··
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013636 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved