Board logo

主題: [資訊電機] [求助]C++的問題....最後出了點錯還請求解... [打印本頁]

發表人: heroine    時間: 2006-10-22 03:20 PM     主題: [求助]C++的問題....最後出了點錯還請求解...

我程式行都沒錯 不過最後輸入的地方有問題...
http://tw.knowledge.yahoo.com/question/?qid=1206102206679

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


struct
{
int hw;
int mid;
int term;
int sum;
char name[10];   
float average;
}student[4],temp;

void file_error(void)
{
printf("****load file error****\n");
exit(1);
}

int main(void)
{
FILE *fp1,*fp2;
char ip;
int i=0;
do
        {
        system("cls");
        fp1=fopen("score.txt","r");
        fp2=fopen("avg.txt","w");
        if(fp1==NULL)
                {
                file_error();
                }
        while(fscanf(fp1,"%s%d%d%d",&student.name,&student.hw,&student.mid,&student.term) !=EOF)
                {
                        i++;
                }
        printf("\name    hw      mid       term\n");
        printf("===========================================\n");
        for(i=0;i<10;i++)
                {
                student.sum=student.hw+student.mid+student.term;
                student.average=(float)student.sum/3;
                printf("%s\t%d\t%d\t%d\t%d\t%f\n",student.name,student.hw,student.mid,student.term,student.sum,student.average);
                fprintf(fp2,"%-10s %d %d %d %d %f\n",student.name,student.hw,student.mid,student.term,student.sum,student.average);
                printf("\n");
                }
        fclose(fp1);
        fclose(fp2);
        printf("restart?(y/n)\n");
        ip=getch();
}
while(ip=='y');
exit(1);
return 0;
}

如果輸入y以外的東西 會發生記憶體xxxx的問題....
想請問一下要怎麼改正勒?

[heroine 在  2006-10-22 04:50 PM 作了最後編輯]
發表人: Acute    時間: 2006-10-22 04:20 PM

while(fscanf(fp1,"%s%d%d%d",&student.name,&student.hw,&student.mid,&student.term) !=EOF)

大致看了一下... 應該這兒有問題, 紅色是不需要的

Acute.
發表人: heroine    時間: 2006-10-22 04:56 PM

回稟大大....
那個&的確可以不要- -a

不過最後的問題還是存在就是...

剛剛發現了問題所在 原來是陣列宣告有問題.... 設太少...

另外想補問一下 有辦法把程式改成每次do loop中間改.txt黨的數值

會使下個loop的結果不同的話...

可以做啥修改呢?

[heroine 在  2006-10-22 05:22 PM 作了最後編輯]




歡迎光臨 TWed2k (http://twed2k.org/) Powered by Discuz! 4.1.0