希奇的问题,代码是Turbo c2.0有错,在VC 6.0下正常

时间:2008-05-10 09:59:15   来源:论坛整理  作者:  编辑:chinaitzhe
错误描述:为什么我在Turbo 2.0的环境下执行这段代码时,当下面这段执行完后,就不会再执行下面的代码了。假如用VC 6.0的环境来运行这段代码又正常,请大家帮忙看看。
引用 do
{
printf("Please enter student age:");
fflush(stdin);
scanf("%d",&s[i].sage);
}while(check_age(s[i].sage)==0);




int check_age(int age)
{
if(age <10 ¦ ¦age>20)
{
printf("error:the data is wrong, please enter data again(10~20)\n");
return 0;
}
return 1;
}

void addinfo(struct student s[])
{
int i,n;
printf("Please enter student number of people(n):");
scanf("%d",&n);
for(i=0;i <n;i )
{
autoget(i,s); //一个自动生成编号的函数
printf("The student's NO. is: %s\n",s[i].sno);

do
{
printf("Please enter student name:");
fflush(stdin);
gets(s[i].sname);
}while(check_name(s[i].sname)==0); //check_name()一个用来检查输入是否合法的函数

do
{
printf("Please enter student age:");
fflush(stdin);
scanf("%d",&s[i].sage);
}while(check_age(s[i].sage)==0); //check_age()一个用来检查输入是否合法的函数

do
{
printf("Please enter chinese score:");
scanf("%f",&s[i].chinese_score);
}while(check_score(s[i].chinese_score)==0); //check_score()一个用来检查输入是否合法的函数

do
{
printf("Please enter english score:");
fflush(stdin);
scanf("%f",&s[i].english_score);
}while(check_score(s[i].english_score)==0);

do
{
printf("Please enter math score:");
fflush(stdin);
scanf("%f",&s[i].math_score);
}while(check_score(s[i].math_score)==0);

do
{
printf("Please enter physics score:");
fflush(stdin);
scanf("%f",&s[i].physics_score);
}while(check_score(s[i].physics_score)==0);

do
{
printf("Please enter chemistry score:");
fflush(stdin);
scanf("%f",&s[i].chemistry_score);
}while(check_score(s[i].chemistry_score)==0);
}
}
网友回复:有人知道吗?
网友回复:TC不要搞了,就是搞明白对你也没有任何好处。好好搞VC吧
至于问题原因,为什么不单步跟踪一下呐?
网友回复:跟踪过了。就是执行到那里age输入合法后直接退出了
网友回复:这边是对的,肯定是其它地方有问题。把整个代码全贴上来。i值怎么变化的?
网友回复:检查你的checkage函数
网友回复:测试代码通过
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include <stdio.h>



int check_age(int age)

{

    if(age > 20 || age <10)

    {

        printf("error:the data is wrong, please enter data again(10~20)\n");

        return 0;

    }

    return 1;

} 





int main()

{

    struct student

    {

        char name[15];

        int sage;

    }s[10];

    int i = 0;

    do

    {

        printf("Please enter student age:");

        fflush(stdin);

        scanf("%d",&s[i].sage);

    }while(check_age(s[i  ].sage)==0);    //check_age()一个用来检查输入是否合法的函数

    return 0;

}






网友回复:全部代码:

引用
struct student
{
char sno[3];
char sname[20];
int sage;
float chinese_score;
float english_score;
float math_score;
float physics_score;
float chemistry_score;
}stu[100];

void autoget(int no,struct student s[])
{
char t[3];
if(no <10)
{
strcpy(s[no].sno,"00");
itoa(no 1,t,10);
strcat(s[no].sno,t);
}
else if(no <100)
{
strcpy(s[no].sno,"0");
itoa(no 1,t,10);
strcat(s[no].sno,t);
}
else
strcpy(s[no].sno,"100");
}

int check_name(char name[])
{
int i;
for(i=0;i <strlen(name);i )
{
if((name[i]==' ') ¦ ¦(name[i]>='a'&&name[i] <='z') ¦ ¦(name[i]>='A'&&name[i] <='Z'))
{
continue;
}
else
{
printf("error:the data is wrong, please enter data again(a~z or A~Z or space)\n");
return 0;
}

}
return 1;
}

int check_age(int age)
{
if(age <10 ¦ ¦age>20)
{
printf("error:the data is wrong, please enter data again(10~20)\n");
return 0;
}
return 1;
}

int check_score(float score)
{
if(score <0 ¦ ¦score>100)
{
printf("error:the data is wrong, please enter data again(0~100)\n");
return 0;
}
return 1;
}

void addinfo(struct student s[])
{
int i,n;
printf("Please enter student number of people(n):");
scanf("%d",&n);
for(i=0;i <n;i )
{
autoget(i,s);
printf("The student's NO. is: %s\n",s[i].sno);

do
{
printf("Please enter student name:");
fflush(stdin);
gets(s[i].sname);
}while(check_name(s[i].sname)==0);

do
{
printf("Please enter student age:");
fflush(stdin);
scanf("%d",&s[i].sage);
}while(check_age(s[i].sage)==0);

do
{
printf("Please enter chinese score:");
scanf("%f",&s[i].chinese_score);
}while(check_score(s[i].chinese_score)==0);

do
{
printf("Please enter english score:");
fflush(stdin);
scanf("%f",&s[i].english_score);
}while(check_score(s[i].english_score)==0);

do
{
printf("Please enter math score:");
fflush(stdin);
scanf("%f",&s[i].math_score);
}while(check_score(s[i].math_score)==0);

do
{
printf("Please enter physics score:");
fflush(stdin);
scanf("%f",&s[i].physics_score);
}while(check_score(s[i].physics_score)==0);

do
{
printf("Please enter chemistry score:");
fflush(stdin);
scanf("%f",&s[i].chemistry_score);
}while(check_score(s[i].chemistry_score)==0);
}
}

网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/







struct student 

{ 

    char sno[3]; 

    char sname[20]; 

    int sage; 

    float chinese_score; 

    float english_score; 

    float math_score; 

    float physics_score; 

    float chemistry_score; 

}stu; 



void autoget(int no,struct student s[]) 

{ 

    char t[3]; 

    if(no <10) 

    { 

        strcpy(s[no].sno,"00"); 

        itoa(no 1,t,10); 

        strcat(s[no].sno,t); 

    } 

    else if(no <100) 

    { 

        strcpy(s[no].sno,"0"); 

        itoa(no 1,t,10); 

        strcat(s[no].sno,t); 

    } 

    else 

        strcpy(s[no].sno,"100"); 

} 



int check_name(char name[]) 

{ 

    int i; 

    for(i=0;i <strlen(name);i  ) 

    { 

        if((name[i]==' ') ||(name[i]>='a'&&name[i] <='z') ||(name[i]>='A'&&name[i] <='Z')) 

        { 

            continue; 

        } 

        else 

        { 

            printf("error:the data is wrong, please enter data again(a~z or A~Z or space)\n"); 

            return 0; 

        } 



    } 

    return 1; 

} 



int check_age(int age) 

{ 

    if(age <10 ||age>20) 

    { 

        printf("error:the data is wrong, please enter data again(10~20)\n"); 

        return 0; 

    } 

    return 1; 

} 



int check_score(float score) 

{ 

    if(score <0 ||score>100) 

    { 

        printf("error:the data is wrong, please enter data again(0~100)\n"); 

        return 0; 

    } 

    return 1; 

} 



void addinfo(struct student s[]) 

{ 

    int i,n; 

    printf("Please enter student number of people(n):"); 

    scanf("%d",&n); 

    for(i=0;i <n;i  ) 

    { 

        autoget(i,s); 

        printf("The student's NO. is: %s\n",s[i].sno); 



        do 

        { 

            printf("Please enter student name:"); 

            fflush(stdin); 

            gets(s[i].sname); 

        }while(check_name(s[i].sname)==0); 



        do 

        { 

            printf("Please enter student age:"); 

            fflush(stdin); 

            scanf("%d",&s[i].sage); 

        }while(check_age(s[i].sage)==0); 



        do 

        { 

            printf("Please enter chinese score:"); 

            scanf("%f",&s[i].chinese_score); 

        }while(check_score(s[i].chinese_score)==0); 



        do 

        { 

            printf("Please enter english score:"); 

            fflush(stdin); 

            scanf("%f",&s[i].english_score); 

        }while(check_score(s[i].english_score)==0); 



        do 

        { 

            printf("Please enter math score:"); 

            fflush(stdin); 

            scanf("%f",&s[i].math_score); 

        }while(check_score(s[i].math_score)==0); 



        do 

        { 

            printf("Please enter physics score:"); 

            fflush(stdin); 

            scanf("%f",&s[i].physics_score); 

        }while(check_score(s[i].physics_score)==0); 



        do 

        { 

            printf("Please enter chemistry score:"); 

            fflush(stdin); 

            scanf("%f",&s[i].chemistry_score); 

        }while(check_score(s[i].chemistry_score)==0); 

    } 

} 

void main()

{   

    struct student students[100];

    addinfo(students);

}




网友回复:lz结构定义的有问题
网友回复:struct student
{
char sno[3]; /* 三字节的空间 */
char sname[20];
int sage;
float chinese_score;
float english_score;
float math_score;
float physics_score;
float chemistry_score;
}stu;

void autoget(int no,struct student s[])
{
char t[3];
if(no <10)
{
strcpy(s[no].sno,"00");
itoa(no 1,t,10);
strcat(s[no].sno,t); /* 越界了 */
}
else if(no <100)
{
strcpy(s[no].sno,"0");
itoa(no 1,t,10);
strcat(s[no].sno,t); /* 越界了 */
}
else
strcpy(s[no].sno,"100"); /* 拷贝了4字节, 越界了 */
}
网友回复:没有吧。似乎每一个都是拷贝3位的学号进去吖!
关键字:希奇,问题,代码,Turbo,2.0,
上一篇:很急

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面