Nero-One
Nero-One

Reputation: 127

Program stops working at first scanf

I have been working on this for a while and can't figure it out. What it is supposed to do is to determine the cost of auto insurance for each family member based on their ages. What I need to do is to be able to pass the AgeAdd function the family members age and possibly count (to keep track of which member it is), and it will calculate the cost and print it out for me.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

void AgeAdd(int);

int main()
{

int age, i, o, count;   
count = 1;
printf("Enter the number of family members: ");
scanf("%d", o);

for (i = 0; i < o; i++)
{

    printf("Enter the age for family member %d: ", count);

    scanf("%d", &age); \\program crashes here

    AgeAdd(age);

    count++;
}



return 0;


}

once I reach that first scanf the program stops working, I'm confused as to why. Can I use a getchar there instead?

void AgeAdd(int a)
{
int sum1, sum2, sum3;


if (a >= 16 && a <= 19)
{
    sum1 = 1000 + (1000 * 15 / 100);
    printf("The cost for the family member is %d \n", sum1);

}
if (a >= 20 && a <= 24)
{
    sum2 = 1000 + (1000 * 5 / 100);
    printf("The cost for the family member is %d \n", sum2);

}
if (a >= 25)
{
    sum3 = 1000 - (1000 * 5 / 100);
    printf("The cost for the family member is %d \n", sum3);
}


}

Here is the AgeAdd method but I doubt there's much of a problem here.

Upvotes: 0

Views: 5745

Answers (1)

Bathsheba
Bathsheba

Reputation: 234685

Fix your first scanf call (you need to pass a pointer):

scanf("%d", &o);

Put a space between subsequent scanf calls:

scanf(" %d", &age);

This passes over the newline character entered on a previous iteration.

Upvotes: 5

Related Questions