gpt4 book ai didi

c - 分段故障核心转储......?

转载 作者:太空宇宙 更新时间:2023-11-04 05:21:05 25 4
gpt4 key购买 nike

我正在努力做到这一点:

Write a program that accepts three user inputs: assignment mark, midterm mark, and final exam mark.

These inputs are of type float and should have values between 0 and 100.

The program should compute the final grade as a percentage and a letter grade.

• Final grades are computed as: Assignments 20%, Midterm 30%, Final exam 50%. Letter grades are computed as: A (final grade ≥ 80%), B (80 > final grade ≥ 70%), C (70 > final grade ≥ 60%), D (60 > final grade ≥ 50%), and F (50 > final grade).

The final grade and letter grade are displayed.

使用以下代码:

#include <stdio.h>

int checkMark(int);
float getMark();
float computeFinalGrade(float assign, float midterm, float finalExam, float finalGrade);
int computeLetterGrade(float);

int checkMark(int x)
{
while(x<0 || x>100) //While x is a negative number:
{
printf("Invalid entry.Try again:");
scanf("%d",&x);
}

if(x>0 && x<=100)//If x is a positive number:
x=x;

return x;
}

float getMark(void)
{
float mark=0,assignMark=0,midMark=0,examMark=0,finalMark=0;
char letterGrade;


printf("Enter assignment grade:");
scanf("%d",mark);

checkMark(mark);
assignMark=mark;

printf("\nEnter midterm mark:");
scanf("%f",&mark);

checkMark(mark);
midMark=mark;

printf("\nEnter exam mark:");
scanf("%f",&mark);

checkMark(mark);
examMark=mark;


finalMark=computeFinalGrade(assignMark,midMark,examMark,finalMark);
letterGrade=computeLetterGrade(finalMark);

printf("The final grade for the course is %.1f, and the letter grade is %c.\n",finalMark,letterGrade);

return 0;
}

float computeFinalGrade(float assign, float midterm, float finalExam, float finalGrade)
{
assign=(float)0.2*assign;
midterm=(float)0.3*midterm;
finalExam=(float)0.4*finalExam;

finalGrade=(float)assign+midterm+finalExam;
return finalGrade;
}

int computeLetterGrade(float finalGrade)
{
int grade;

if(finalGrade>=80)
grade=65;

if(finalGrade<80)
if(finalGrade>=70)
grade=66;

if(finalGrade<70)
if(finalGrade>=60)
grade=67;

if(finalGrade<60)
if(finalGrade>=50)
grade=68;

if(finalGrade<50)
grade=70;

return grade;
}

int main()
{
getMark();

return 0;
}

当我调试上面的程序并输入一个负数时,它会给我这个:

Enter assignment grade:123
Segmentation fault (core dumped)

我究竟做错了什么?

最佳答案

这是你的问题:

scanf("%d",mark);

您需要传递变量的地址,说明符应该是%f。尝试:

scanf("%f", &mark);

顺便说一句,有一个C FAQ关于这个。

关于c - 分段故障核心转储......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8025884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com