鱼C论坛

 找回密码
 立即注册
查看: 2339|回复: 4

猜谜语这道题有时候报内存不能read 有时候是 the value of esp

[复制链接]
发表于 2012-5-22 11:44:35 | 显示全部楼层 |阅读模式
1鱼币
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 3
int n=0;
void one();
void two();
void three();
void one()
{
    unsigned char str[3];
    int i;
    printf("请听题:\n第1题:“多出各一半”,打一字!\n答案是:");
    scanf("%s",str);
    str[2]='\0';
    fflush(stdin);
    for(i=0;i<M;i++)
    {
        if (strcmp(str,"岁"))
        {
            if (i<(M-1))
            {
                printf("差一点!你一定行的!\n ");
                printf("第1题:“多出各一半”,打一字!\n 答案是:");
                scanf("%s",str);
                str[2]='\0';
                fflush(stdin);
                continue;
            }
            else
            {
                printf("真是无语了!\n");
            }
        }
        else
        {
            printf("你太有才了!\n ");
            n++;
            break;
        }
    }
}
void two()
{
    unsigned char str[3];
    char que;
    int i;
    printf("第2题:“闭着口,却在笑”,打一字!\n答案是:");
    scanf("%s",str);
    str[2]='\0';
    fflush(stdin);
    for(i=0;i<M;i++)
    {
        if (strcmp(str,"哈"))
        {
            if (i<(M-1))
            {
                printf("努力!请再试一次 !\n ");
                printf("第2题:“闭着口,却在笑”,打一字!\n 答案是:");
                scanf("%s",str);
                str[2]='\0';
                fflush(stdin);
                continue;
            }
            else
            {
                printf("又错了,是否继续? Y/N:\n");
                scanf("%c",&que);
    //scanf("%*c");
                if(que=='Y')
                {
                    printf("进入第三题!\n");
                    three();
        
                }
                else
                {
                    printf("答题结束!\n");
                }
               
            }
        }
        else
        {
            printf("还行,下一关!\n ");
            n++;
            three();
            break;/*如果不加break会继续没完成的循环,break 一定要放在调用韩素three()之后,如果放在函数前面不会继续执行three()这个函数,直接跳出函数。*/
        }
    }
}
void three()
{
    unsigned char str[3];
    int i;
    printf("第3题:“五口人说话”,打一字!\n答案是:");
    scanf("%s",str);
    str[2]='\0';
    fflush(stdin);
    for(i=0;i<M;i++)
    {
        if (strcmp(str,"语"))
        {
            if (i<(M-1))
            {
                printf("努力!请再试一次 !\n ");
                printf("第3题:“五口人说话”,打一字!\n 答案是:");
                scanf("%s",str);
                str[2]='\0';
                fflush(stdin);
                continue;
            }
            else
            {
                printf("你又错了,回去再修炼几年吧!\n");
            }
        }
        else
        {
            printf("你真聪明!\n ");
            n++;
            break;
        }
    }
}
void main()
{
    one();
    two();
    printf("您一共答对了 %d 道题!\n",n);
}

调试时,输入乱七八糟的东西就常常会出现问题,要么是内存不能read 要不就是 堆栈错误The value of ESP ......
怎么解决这些个问题?


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-5-22 13:34:21 | 显示全部楼层
本帖最后由 jolly 于 2012-5-23 09:24 编辑

我把数组长度定义成很长。只要输入的长度不超过这个长度就不会有问题。但是治标不治本啊。


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-5-22 13:35:01 | 显示全部楼层
6.0版本的不支持scanf_s()还
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-5-22 14:45:54 | 显示全部楼层
本帖最后由 jolly 于 2012-5-22 15:23 编辑

问题解决了!!!!!!!!!!!!!!!!!!!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M 2

int n=0;

void one();
void two();
void three();


void one()
{
    unsigned char str[3];
    int i;
    char ch;
    printf("请听题:\n第1题:“多出各一半”,打一字!\n答案是:");
    fgets(str,3,stdin);
    while (str[0]=='\n' || str[1]=='\n' )
    {
        printf("Too short,please input again!\n");
        fgets(str,3,stdin);
    }
   
    //scanf("%s",str);
    str[2]='\0'; //字符串结束标志
    while (((ch = getchar()) != '\n') && (ch != EOF));
    //fflush(stdin); 清除标准输入缓冲区,不过貌似没有用,超过数组最大长度还是会溢出
    for(i=0;i<M;i++)
    {
        if (strcmp(str,"岁"))
        {
            if (i<(M-1))
            {
                printf("差一点!你一定行的!\n ");
                printf("第1题:“多出各一半”,打一字!\n 答案是:");
                fgets(str,3,stdin);
                while (str[0]=='\n' || str[1]=='\n' )
                {
                    printf("Too short,please input again!\n");
                    fgets(str,3,stdin);
                }
                //    scanf("%s",str);
                str[2]='\0';
                while (((ch = getchar()) != '\n') && (ch != EOF));
                continue;
            }
            else
            {
                printf("真是无语了!\n");
            }
        }
        else
        {
            printf("你太有才了!\n ");
            n++;
            break;
        }
    }
}
void two()
{
    unsigned char str[3];
    char que,ch;
    int i;
    printf("第2题:“闭着口,却在笑”,打一字!\n答案是:");
    fgets(str,3,stdin);
    while (str[0]=='\n' || str[1]=='\n' )
    {
        printf("Too short,please input again!\n");
        fgets(str,3,stdin);
    }
    //scanf("%s",str);
    str[2]='\0';
    while (((ch = getchar()) != '\n') && (ch != EOF));
    for(i=0;i<M;i++)
    {
        if (strcmp(str,"哈"))
        {
            if (i<(M-1))
            {
                printf("努力!请再试一次 !\n ");
                printf("第2题:“闭着口,却在笑”,打一字!\n 答案是:");
                fgets(str,3,stdin);
                while (str[0]=='\n' || str[1]=='\n' )
                {
                    printf("Too short,please input again!\n");
                    fgets(str,3,stdin);
                }
                //scanf("%s",str);   
                str[2]='\0';
                while (((ch = getchar()) != '\n') && (ch != EOF));
                continue;
            }
            else
            {
                printf("又错了,是否继续? Y/N:\n");
                que=fgetchar();
                //    scanf("%c",&que);
                while (((ch = getchar()) != '\n') && (ch != EOF));               
                if(que=='Y')
                {
                    printf("进入第三题!\n");
                    three();
                    
                }
                else
                {
                    printf("答题结束!\n");
                }
               
            }
        }
        else
        {
            printf("还行,下一关!\n ");
            n++;
            three();
            break;/*如果不加break会继续没完成的循环,break 一定要放在调用韩素three()之后,如果放在函数前面不会继续执行three()这个函数,直接跳出函数。*/
        }
    }
}
void three()
{
    unsigned char str[3];
    int i;
    char ch;
    printf("第3题:“五口人说话”,打一字!\n答案是:");
    fgets(str,3,stdin);
    while (str[0]=='\n' || str[1]=='\n' )
    {
        printf("Too short,please input again!\n");
        fgets(str,3,stdin);
    }
    //scanf("%s",str);
    str[2]='\0';
    while (((ch = getchar()) != '\n') && (ch != EOF));
    for(i=0;i<M;i++)
    {
        if (strcmp(str,"语"))
        {
            if (i<(M-1))
            {
                printf("努力!请再试一次 !\n ");
                printf("第3题:“五口人说话”,打一字!\n 答案是:");
                fgets(str,3,stdin);
                while (str[0]=='\n' || str[1]=='\n' )
                {
                    printf("Too short,please input again!\n");
                    fgets(str,3,stdin);
                }
                //scanf("%s",str);
                str[2]='\0';
                while (((ch = getchar()) != '\n') && (ch != EOF));
                continue;
            }
            else
            {
                printf("你又错了,回去再修炼几年吧!\n");
            }
        }
        else
        {
            printf("你真聪明!\n ");
            n++;
            break;
        }
    }
}

void main()
{
    one();
    two();
    printf("您一共答对了 %d 道题!\n",n);
   
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-5-22 14:56:01 | 显示全部楼层
我在网上找到一个用来替代scanf函数的getline函数。解决scanf可能导致缓冲区溢出的问题;
相关网址如下:
http://stackoverflow.com/questions/2430303/disadvantages-of-scanf

prmpt 指向 提示信息内容,可无;buff 指向 用来存放输入信息的数组;sz 数组长度
调用格式:getline(“请输入:”,str,sizeof(str))
函数定义:
#define OK       0
#define NO_INPUT 1
#define TOO_LONG 2
static int getLine (char *prmpt, char *buff, size_t sz) {
    int ch, extra;

    // Get line with buffer overrun protection.
    if (prmpt != NULL) {
        printf ("%s", prmpt);
        fflush (stdout);
    }
    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;

    // If it was too long, there'll be no newline. In that case, we flush
    // to end of line so that excess doesn't affect the next call.
    if (buff[strlen(buff)-1] != '\n') {
        extra = 0;
        while (((ch = getchar()) != '\n') && (ch != EOF))
            extra = 1;
        return (extra == 1) ? TOO_LONG : OK;
    }

    // Otherwise remove newline and give string back to caller.
    buff[strlen(buff)-1] = '\0';
    return OK;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-6 04:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表