读书人

哥们~看看这段C++程序出啥毛病了?该怎

发布时间: 2012-02-23 22:01:35 作者: rapoo

哥们~~看看这段C++程序出啥毛病了?
是用C写的,部分程序如下: 其实只用看add_data()那个函数,输入number字段时输入字母不会出现错误,输入数字就报 "Unhandled exception FILE.exe:0xC0000005:Access Violation. "
/* includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>

#define STDNT_NUM 13

void add_data();

/* define the type of student */
typedef struct student
{
char number[16];
char name[16];
char age[3];
} STUDENT; /* stud[STDNT_NUM] is global */

typedef struct grade
{
char number[16];
char coursename[16];
char grades[5];
} GRADE; /* grade[STDNT_NUM] is global */

char order;

void main()
{
printf( "Order: " );
while{
order = getche(); // Accept user 's order
printf( "\n " );
switch( order )
{
case '1 ' : add_data(); break;
default : printf( "Exit...............\n "); system( "PAUSE " ); exit(1);
}
}
}
void add_data()
{
STUDENTstud[STDNT_NUM];
GRADE grade[STDNT_NUM];
FILE *fp1, *fp2;
int i, length, count_blank=0, count = 0;
char *str = " ", *str1 = " ", *str2 = " ";
char *newline = "\n ";
char stud_item[35];

if( (fp1=fopen( "student.txt ", "a " )) == NULL || (fp2=fopen( "grade.txt ", "a " )) == NULL)
{
printf ( "Can 't write the two file!\n " );
exit (1);
}

printf( "\nPlease input the data:\n ");

for ( ; count <STDNT_NUM ; count++ )
{
printf( "--------Input information of student--------\n " );


printf( "---Input the number:\n ");

strcpy( stud[count].number, str );
scanf( "%s ", stud[count].number );
length = strlen(stud[count].number);
for( count_blank = 0; count_blank < 16-length; count_blank++ )
{
strcat( stud[count].number, " " );
} // make it matched with the format number

while( atol(stud[count].number) == 0 )
{
strcpy( stud[count].number, str );
printf( "The format of number is wrong! Please input it again:\n ");
scanf( "%s ", stud[count].number );
}

while( match( stud[count].number, fp1, "student.txt ", 35, stud_item ) == 1 )
{
strcpy( stud[count].number, str ); /* reset the number */
printf( "The file has the same item! Please input it again:\n " );
scanf( "%s ", stud[count].number );
}

strcpy( stud[count].name, str );
printf( "---Input the name:\n ");
strcpy( stud[count].name, str );
scanf( "%s ", stud[count].name );

while( atoi(stud[count].name) != 0 )
{
strcpy( stud[count].name, str ); /* reset the number */
printf( "The format of name is wrong! Please input it again:\n ");
scanf( "%s ", stud[count].name );
}

printf( "---Input the age:\n ");
strcpy( stud[count].age, str1 );
scanf( "%s ", stud[count].age );

while( atoi(stud[count].age) == 0 || atoi(stud[count].age) > = 100 )
{

printf( "The format of age is wrong! Please input it again:\n " );


scanf( "%s ", stud[count].age );
}

printf( "--------Input information of grade--------\n " );
/* Just copy the student number */
strcpy( grade[count].number, stud[count].number );

printf( "---Input the coursename:\n " );
strcpy( grade[count].coursename, str );
scanf( "%s ", grade[count].coursename );

while( atoi(grade[count].coursename) != 0 )
{
strcpy( grade[count].coursename, str ); /* reset the coursename */
printf( "The format of coursename is wrong! Please input it again:\n ");
scanf( "%s ", grade[count].coursename );
}

printf( "---Input the grade:\n " );
strcpy( grade[count].grades, str2 );
scanf( "%s ", grade[count].grades );

while( atoi(grade[count].grades) == 0 || atoi(grade[count].grades) > = 100 )
{

printf( "The format of grade is wrong! Please input it again:\n " );
scanf( "%s ", grade[count].grades );
}

if( count <STDNT_NUM )
{
printf( "Press Y to continue adding data or press any other key to stop adding:\n ");
if( getche()== 'y ' || getche()== 'Y ' )
{
printf( "\n " );
continue;
}
else
break;
}
}

printf( "\n ");

for( i=0; i <=count; i++)
{
if( fwrite( &stud[i], sizeof(struct student), 1, fp1 ) != 1 || fwrite( newline, 1, 1, fp1 ) !=1 )
printf ( "Error happens when writing data to the file of student!\n " );



if( fwrite( &grade[i], sizeof(struct grade), 1, fp2 ) != 1 || fwrite( newline, 1, 1, fp2 ) !=1 )
printf ( "Error happens when writing data to the file of grade!\n " );
}

fclose ( fp1 );
fclose ( fp2 );
printf( "Add data successfully!\n ");
return;
}

[解决办法]
你试试,我这里可以了,我用的dev,还是分配空间的问题(还有那个初始化,用空格是你的需求么?我用0来初始化的,因为substr只是剪且字符,不会帮你加 '\0 '的,所以容易导致越界),随便改了一下,不行了,饿死了,吃饭去~~~~

读书人网 >C++

热点推荐