Why My C program got error???

no3l2001 2006-10-07 09:40:30
这是题目:
---------------------------------------------------------------------
Write an interactive C program which will process test, assignment and final exam marks for the Java programming course. Full marks for test is 20%, assignment 20% and final exam is 60%. Enter the students name and their marks for the continuous assessment above repetitively. The marks’ grade are shown in the table below:
Grade marks
A 80-100
B+ 75-79
B 70-74
B- 65-69
C+ 60-64
C 55-59
C- 50-54
D 40-49
E 35-39
F 0-34

(a) Count and display total number of students
(b) For each assignment, test and final exam component, which have been obtained by the class, determine:
• Total marks for assignment, total marks for test and total marks for final exam
• Average marks for assignment, total marks for test and total marks for final exam

For every student, display the following information:
(i) Student name, total marks for assignment, test and final exam for every student (marks over hundred)
(ii) Mark’s grade Use array concept to solve the problem above.. Make sure the labels for output are clear.

---------------------------------------------------------------------

这是我的答案:

#include<stdio.h>
#include<string.h>
#define no 2
main()
{
int i,test[no],assign[no],exam[no],total_marks[no];
char stud_name[no],grade[no];
int total_test=0;
int total_assign=0;
int total_exam=0;
float ave_assign,ave_test,ave_exam;

clrscr();
for(i=0;i<no;i++)
{ printf("\nStudent's Name > ");
gets(stud_name[i]);
printf("Assignment Marks (20%) > ");
scanf("%d", &assign[i]);
printf("Test Marks (20%) > ");
scanf("%d", &test[i]);
printf("Final Exam Marks (60%) > ");
scanf("%d", &exam[i]);

total_test+=test[i];
total_assign+=assign[i];
total_exam+=exam[i];
total_marks[i]=test[i]+assign[i]+exam[i];

if(total_marks[i]>=80 && total_marks[i]<=100)
strcpy(grade[i],"A");
else if(total_marks[i]>=75 && total_marks[i]<=79)
strcpy(grade[i],"B+");
else if(total_marks[i]>=70 && total_marks[i]<=74)
strcpy(grade[i],"B");
else if(total_marks[i]>=65 && total_marks[i]<=69)
strcpy(grade[i],"B-");
else if(total_marks[i]>=60 && total_marks[i]<=64)
strcpy(grade[i],"C+");
else if(total_marks[i]>=55 && total_marks[i]<=59)
strcpy(grade[i],"C");
else if(total_marks[i]>=50 && total_marks[i]<=54)
strcpy(grade[i],"C-");
else if(total_marks[i]>=40 && total_marks[i]<=49)
strcpy(grade[i],"D");
else if(total_marks[i]>=35 && total_marks[i]<=39)
strcpy(grade[i],"E");
else if(total_marks[i]>=0 && total_marks[i]<=34)
strcpy(grade[i],"F");
else
strcpy(grade[i],"Error");

}
ave_test=(float)total_test/no;
ave_assign=(float)total_assign/no;
ave_exam=(float)total_exam/no;

printf("\nTotal Number of Students >> %d",no);
printf("\nTotal Marks for Assignment >> %d",total_assign);
printf("\nTotal Marks for Test >> %d",total_test);
printf("\nTotal Marks for Final Exam >> %d",total_exam);
printf("\n\nAverage Marks for Assignment >> %.2f",ave_assign);
printf("\nAverage Marks for Test >> %.2f",ave_test);
printf("\nAverage Marks for Final Exam >> %.2f",ave_exam);

printf("\n---------------------------------------------------------");
printf("\n no Student Name Assignment Test Exam Total Grade");
printf("\n---------------------------------------------------------");
for(i=0;i<no;i++)
{
printf("\n %d %s %d %d %d %d %s",i+1,stud_name[i],assign[i],test[i],exam[i],total_marks[i],grade[i]);
}
getch();

}




为何当我RUN时,输入第一人的资料后就有问题了,直接就打印答案?
...全文
641 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
2.gets(stud_name[i]); stud_name is a "char" but gets() function need a "char *"
and so on ...
=======================================================

how to implement it?

i am sorry to trouble you..... i will work harder...

i have object oriented question to ask and it is about Use Case Diagram... but i think here cannot upload picture rite?
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
i didn't ,because you problem still have other problems
for example
1 you should use include a file "#include<stdlib.h>"
2.gets(stud_name[i]); stud_name is a "char" but gets() function need a "char *"
and so on ...

please read someting about "pointer"
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
did u run it?
i think cannot work.
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
int i,*test,*assign,*exam,*total_marks;
char *stud_name,*grade;
int total_test=0;
int total_assign=0;
int total_exam=0;
float ave_assign,ave_test,ave_exam;
char c;
int no;
clrscr();

printf(" how many students > ");
scanf("%d", no);
test=(int *)malloc(sizeof(int)*no);
assign=(int *)malloc(sizeof(int)*no);
...

no3l2001 2006-10-08
  • 打赏
  • 举报
回复
got problem after running:

#include<stdio.h>
#include<string.h>
/*#define no 2*/
main()
{
int i,*test,*assign,*exam,*total_marks;
char *stud_name,*grade;
int total_test=0;
int total_assign=0;
int total_exam=0;
float ave_assign,ave_test,ave_exam;
char c;
int *p;
int no;
clrscr();

printf(" how many students > ");
scanf("%d", no);
p=(int *)malloc(sizeof(int)*no);
for(i=0;i<no;i++)
{ printf("\nStudent's Name > ");
gets(stud_name[i]);
printf("Assignment Marks (20%) > ");
scanf("%d", &assign[i]);
while((c=getchar())!='\n');
printf("Test Marks (20%) > ");
scanf("%d", &test[i]);
while((c=getchar())!='\n');
printf("Final Exam Marks (60%) > ");
scanf("%d", &exam[i]);
while((c=getchar())!='\n');
total_test+=test[i];
total_assign+=assign[i];
total_exam+=exam[i];
total_marks[i]=test[i]+assign[i]+exam[i];

if(total_marks[i]>=80 && total_marks[i]<=100)
strcpy(grade[i],"A");
else if(total_marks[i]>=75 && total_marks[i]<=79)
strcpy(grade[i],"B+");
else if(total_marks[i]>=70 && total_marks[i]<=74)
strcpy(grade[i],"B");
else if(total_marks[i]>=65 && total_marks[i]<=69)
strcpy(grade[i],"B-");
else if(total_marks[i]>=60 && total_marks[i]<=64)
strcpy(grade[i],"C+");
else if(total_marks[i]>=55 && total_marks[i]<=59)
strcpy(grade[i],"C");
else if(total_marks[i]>=50 && total_marks[i]<=54)
strcpy(grade[i],"C-");
else if(total_marks[i]>=40 && total_marks[i]<=49)
strcpy(grade[i],"D");
else if(total_marks[i]>=35 && total_marks[i]<=39)
strcpy(grade[i],"E");
else if(total_marks[i]>=0 && total_marks[i]<=34)
strcpy(grade[i],"F");
else
strcpy(grade[i],"Error");

}
ave_test=(float)total_test/no;
ave_assign=(float)total_assign/no;
ave_exam=(float)total_exam/no;

printf("\nTotal Number of Students >> %d",no);
printf("\nTotal Marks for Assignment >> %d",total_assign);
printf("\nTotal Marks for Test >> %d",total_test);
printf("\nTotal Marks for Final Exam >> %d",total_exam);
printf("\n\nAverage Marks for Assignment >> %.2f",ave_assign);
printf("\nAverage Marks for Test >> %.2f",ave_test);
printf("\nAverage Marks for Final Exam >> %.2f",ave_exam);

printf("\n---------------------------------------------------------");
printf("\n no Student Name Assignment Test Exam Total Grade");
printf("\n---------------------------------------------------------");
for(i=0;i<no;i++)
{
printf("\n %d %s %d %d %d %d %s",i+1,stud_name[i],assign[i],test[i],exam[i],total_marks[i],grade[i]);
}
getch();

}
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
i try first
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
delete all
use int *test,*assign,....,instead
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
but the declaration up there eg: int i,test[no],assign[no],exam[no],total_marks[no];
got error
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
int total_assign=0;
int total_exam=0;
float ave_assign,ave_test,ave_exam;
char c;
int *p; <--------------------------i put here

clrscr();
<--------------------------i put here
printf(" how many students > ") <--------------------------i put here
scanf("%d", no); <--------------------------i put here
p=(int *)malloc(sizeof(int)*no);
for(i=0;i<no;i++)
{ printf("\nStudent's Name > ");
gets(stud_name[i]);
printf("Assignment Marks (20%) > ");
scanf("%d", &assign[i]);
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
got error...

main()
{
int i,test[no],assign[no],exam[no],total_marks[no];
char stud_name[no],grade[no];
int total_test=0;
int total_assign=0;
int total_exam=0;
float ave_assign,ave_test,ave_exam;
char c;
int *p; <--------------------------i put here

clrscr();
p=(int *)malloc(sizeof(int)*no); <--------------------------i put here
printf(" how many students > ") <--------------------------i put here
scanf("%d", no); <--------------------------i put here
for(i=0;i<no;i++)
{ printf("\nStudent's Name > ");
gets(stud_name[i]);
printf("Assignment Marks (20%) > ");
scanf("%d", &assign[i]);
while((c=getchar())!='\n');
printf("Test Marks (20%) > ");
scanf("%d", &test[i]);
while((c=getchar())!='\n');
printf("Final Exam Marks (60%) > ");
scanf("%d", &exam[i]);
while((c=getchar())!='\n');
total_test+=test[i];
total_assign+=assign[i];
total_exam+=exam[i];
total_marks[i]=test[i]+assign[i]+exam[i];

if(total_marks[i]>=80 && total_marks[i]<=100)
strcpy(grade[i],"A");
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
first you should have a better compiler, i suggest "gcc" or "dev c++"
next you should got a better book like <c pointer on c> or<c_expert>
but i think you should read <c pointer on c> first
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
int *test;

/*key in code area */ <---------------- key in what code here?
test=(int *)malloc(sizeof(int)*no);

=====================================
printf(" how many students > ")
scanf("%d", no);
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
i have a data structure book. just sometimes, when i use the sample coding to run, it comes out with error...
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
a book called <pointer on c> may help you ,try
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
i am not very good in this:

int *test;

/*key in code area */ <---------------- key in what code here?
test=(int *)malloc(sizeof(int)*no);
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
so my program cannot changed to user key in no?
=====================================
you can make it by pointer as follows:


int *test;

/*key in code area */
test=(int *)malloc(sizeof(int)*no);
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
ic.
u r student or ?

here is a good place... i post the question and immediately got response. you ppl r good..
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
much more recognition
no3l2001 2006-10-08
  • 打赏
  • 举报
回复
oic...
so my program cannot changed to user key in no?
because of that, i put a define no 2 there...


你们获得得分有什么好处?
Arthur_ 2006-10-08
  • 打赏
  • 举报
回复
emit //ignore
加载更多回复(21)
au3反编译源码 myAut2Exe - The Open Source AutoIT Script Decompiler 2.9 ======================================================== *New* full support for AutoIT v3.2.6++ :) ... mmh here's what I merely missed in the 'public sources 3.1.0' This program is for studying the 'Compiled' AutoIt3 format. AutoHotKey was developed from AutoIT and so scripts are nearly the same. Drag the compiled *.exe or *.a3x into the AutoIT Script Decompiler textbox. To copy text or to enlarge the log window double click on it. Supported Obfuscators: 'Jos van der Zande AutoIt3 Source Obfuscator v1.0.14 [June 16, 2007]' , 'Jos van der Zande AutoIt3 Source Obfuscator v1.0.15 [July 1, 2007]' , 'Jos van der Zande AutoIt3 Source Obfuscator v1.0.20 [Sept 8, 2007]' , 'Jos van der Zande AutoIt3 Source Obfuscator v1.0.22 [Oct 18, 2007]' , 'Jos van der Zande AutoIt3 Source Obfuscator v1.0.24 [Feb 15, 2008]' , 'EncodeIt 2.0' and 'Chr() string encode' Tested with: AutoIT : v3. 3. 0.0 and AutoIT : v2.64. 0.0 and AutoHotKey: v1.0.48.5 The options: =========== 'Force Old Script Type' Grey means auto detect and is the best in most cases. However if auto detection fails or is fooled through modification try to enable/disable this setting 'Don't delete temp files (compressed script)' this will keep *.pak files you may try to unpack manually with'LZSS.exe' as well as *.tok DeTokeniser files, tidy backups and *.tbl (<-Used in van Zande obfucation). If enable it will keep AHK-Scripts as they are and doesn't remove the linebreaks at the beginning Default:OFF 'Verbose LogOutput' When checked you get verbose information when decompiling(DeTokenise) new 3.2.6+ compiled Exe Default:OFF 'Restore Includes' will separated/restore includes. requires ';

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧