简体   繁体   English

程序在第一次 scanf 时停止工作

[英]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.我需要做的是能够传递家庭成员年龄和可能计数的AgeAdd函数(以跟踪它是哪个成员),它会计算成本并为我打印出来。

#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.一旦我到达第一个scanf程序停止工作,我对为什么感到困惑。 Can I use a getchar there instead?我可以在那里使用getchar吗?

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.这是AgeAdd方法,但我怀疑这里有很多问题。

Fix your first scanf call (you need to pass a pointer):修复你的第一个scanf调用(你需要传递一个指针):

scanf("%d", &o);

Put a space between subsequent scanf calls:在随后的scanf调用之间放置一个空格:

scanf(" %d", &age);

This passes over the newline character entered on a previous iteration.这会跳过在前一次迭代中输入的换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM