So I have this programming assignment I'm doing for my structured programming class (using the C language) that gives me a segmentation fault error. I rewrote the assignment in C++ and it worked fine, but since the class uses C, I must get to the bottom of this and submit this code.

#include <stdio.h>

int main()
{
    int numbPatients, numbFeverMeasurements, hasFever, hasSoreThroath, hasHeadache, hasStomachache, throwsUp, blurryVision, i, j;
    float fever, sumFever, averageFever;
    printf("Enter the number of patients: ");
    scanf("%d", &numbPatients);
    for(i = 1; i <= numbPatients; i++)
    {

        printf("1. Does patient #%d have fever? (Yes = 1, No = 0): ", i);
        scanf("%d", &hasFever);
        if(hasFever == 1)
        {

            printf("Enter the number of fever measurements: ");
            scanf("%d", &numbFeverMeasurements);
            for(j = 1; j <= numbFeverMeasurements; j++)
            {

                printf("Enter the fever measurement #%d: ", j);
                scanf("%f", fever);
                sumFever += fever;

            }
            averageFever = sumFever / numbFeverMeasurements;

        }
            printf("2. Does patient #%d have a sore throath? (Yes = 1, No = 0): ", i);
            scanf("%d", &hasSoreThroath);
            printf("3. Does patient #%d have a head ache? (Yes = 1, No = 0): ", i);
            scanf("%d", &hasHeadache);
            printf("4. Does patient #%d have a stomach ache? (Yes = 1, No = 0): ", i);
            scanf("%d", &hasStomachache);
            printf("5. Does patient #%d throw up? (Yes = 1, No = 0): ", i);
            scanf("%d", &throwsUp);
            printf("6. Does patient #%d have blurry vision? (Yes = 1, No = 0): ", i);
            scanf("%d", &blurryVision);
            if(hasStomachache == 1 && throwsUp == 1)
            {

                if(hasFever == 1)
                {

                    printf("Diagnosis for patient #%d: PREGNANCY.\n", i);
                    printf("Fever: %f C\n", averageFever);
                    continue;

                }
                else
                {

                    printf("Diagnosis for patient #%d: PREGNANCY.\n", i);
                    continue;

                }

            }
            if(throwsUp == 1 && (hasFever == 1 || hasSoreThroath == 1 || hasHeadache == 1))
            {

                if(hasFever == 1)
                {

                    printf("Diagnosis for patient #%d: FLU.\n", i);
                    printf("Fever: %f C\n", averageFever);
                    continue;

                }
                else
                {

                    printf("Diagnosis for patient #%d: FLU.\n", i);
                    continue;

                }

            }
            if(hasFever == 1 || hasSoreThroath == 1 || hasHeadache == 1 || hasStomachache == 1)
            {

                if(hasFever == 1)
                {

                    printf("Diagnosis for patient #%d: COMMON COLD.\n", i);
                    printf("Fever: %f C\n", averageFever);
                    continue;

                }
                else
                {

                    printf("Diagnosis for patient #%d: COMMON COLD.\n", i);
                    continue;

                }

            }

    }
    return 0;

}

When I start the program, it works as it should. It asks me for the number of patients and then starts to ask questions to gather information about symptoms. On the first question, if the user replies positively, he should be asked for the number of fever measurements made. The user enters the number of fever measurements and then has to input the fever value for all of the measurements. When I entered the value for the first fever measurement, the program stopped (Process returned 139 (0x8B)) and it printed Segmentation fault.

I had and still have (after reading what is segmentation fault and how does it occur) no clue why this is going on with my program. I tried to make some modifications with hopes of solving this issue, like changing the float variables to double, but that didn't work.

I would be glad if someone could tell me why this is happening and how to solve it.

Best regards,
bitwiseboredom.

I found the mistake. I seem to have forgotten to put an & before the variable name in the scanf call on line 23. How careless of me.. This thread can be deleted as it doesn't serve any purpose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.