English 中文(简体)
倾销的碎片核心吗?
原标题:Segmentation fault core dumped...?

我试图做到这一点:

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

这些投入为类型浮动,数值为0至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("
Enter midterm mark:");
    scanf("%f",&mark);

    checkMark(mark);
    midMark=mark;

    printf("
Enter 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.
",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);

您需要通过变数的地址<>m>,光谱仪应为%f。 Try:

scanf("%f", &mark);

顺便提一句:C FAQ

关于你的法典,还有几点意见:

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;
}

而应当:

#define MIN_GRADE 0
#define MAX_GRADE 100

int checkMark(int x)
{
    while(x<MIN_GRADE || x>MAX_GRADE)
    {
        printf("Invalid entry.Try again:");
        scanf("%d",&x);
    }

    return x;
}

通知一:

  • 删除的无用评论

  • (0和100)

  • 取消了无用试验(如果你在休息期间离开,则X在{0、......、100})。

  • 允许<代码>x

  • removed an idempotent assignment (x=x doesn t do anything useful...)

然后:

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

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

^ 如其他发言者所述,您应使用<代码>%f和&mark,载于scanf

    checkMark(mark);

^ Here, it should be mark = checkMark(mark); if you want the mark variable in this code to be updated. This is because the value of mark is passed to the function, not a reference to the variable mark. Another solution is to pass such a reference, but I don t think you re ready for this yet :)

    assignMark=mark;

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

    checkMark(mark);
    midMark=mark;

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

    checkMark(mark);
    examMark=mark;

^ Same remarks for all these lines

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

^ 通过<条码>限值/代码>作为此处的参数是完全无用的。 更正应只对原文提出。 Mark,年中期市场,考试;,你不认为?

    letterGrade=computeLetterGrade(finalMark);

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

    return 0;
}

As mentioned just earlier, you don t need to pass some finalGrade value to computeFinalGrade, you should be okay with just:

float computeFinalGrade(float assign, float midterm, float finalExam)
{
    return 0.2*assign + 0.3*midterm + 0.4*finalExam; // this could even be a macro
}

最后,在<代码>computeLetter Grade中,你正在做大量多余的测试,你的控制流动是用这一可变的任务加以衡量的,你应当使用<代码><>char/code>。 而不是<代码>int。 相反:

char computeLetterGrade(float finalGrade)
{
    if(finalGrade>=80) return  A ;
    // Since we did not return, necessarily finalGrade < 80
    if(finalGrade>=70) return  B ;
    if(finalGrade>=60) return  C ;
    if(finalGrade>=50) return  D ;
    // All other ifs failed, therefore finalGrade < 50
    return  E ;
}

You will probably need to fix some things I didn t mention, but at least you should be closer to your goals with some of these corrections.

If you are on linux, take a look at my answer here. It s about valgrind, a tool that helps debugging segfaults, memory leaks etc.

当贵方案试图进入分配给你的方案之外的记忆区时,会发生分裂。 <>代码>scanf(>预期该变量的地址。 在您的法典中,你将变数的数值转至scanf(),而不是像公诉人指出的那样。

scanf("%d",mark);

标记变量没有初步化,有些随机数据,用扫描仪作为记忆地点处理,在试图写到这个地址时。 由于你获得的记忆有限,因此出现分离错误。 此外,标记是浮动变量,因此,请使用<>%fspecifier。

scanf("%f", &mark);




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...