简体   繁体   English

如何修复c中while循环的分段错误

[英]How to fix segmentation fault for while loop in c

I'm working on an assignment and trying to write the code to answer this question: 我正在做一个作业,并试图编写代码来回答这个问题:

Write a program that computes the total weight of a cargo. 编写一个程序来计算货物的总重量。 The user has many types of boxes (numbered 1 to n). 用户具有许多类型的框(编号从1到n)。 For each box type, the program asks the user about the weight and quantity. 对于每种盒子类型,程序都会询问用户重量和数量。 The program thencomputes and prints the total cargo weight. 然后程序计算并打印总货物重量。 In the output sample below, the user has three box types. 在下面的输出示例中,用户具有三种框类型。 For box type 2, the user enters the sentinel -1 to indicate they're done with the input. 对于框类型2,用户输入标记-1,以指示输入已完成。 Your program should print Type 1, 2, 3, etc. as shown in the output below. 您的程序应打印Type 1、2、3等,如下面的输出所示。

Enter weight (lbs) of Type 1 box: 4
Enter quantity: 2
Enter weight (lbs) of Type 2 box: -1 
The total weight is 8 lbs.

When i run this code it runs the first line to input weight but then gives me a segmentation fault and says (core dumped). 当我运行此代码时,它将运行第一行以输入权重,但随后给我一个分段错误并说(核心已转储)。 -1 is the sentinel and even when the enter weight is inside the while loop the result is the same. -1是前哨,即使输入权重在while循环内,结果也相同。 What am I doing wrong? 我究竟做错了什么? I'm sorry I'm new to C 对不起,我是C新手

#include <stdio.h>

int main()
{
    int weight; //weight of boxes
    int quantity; //number of boxes 
    int total_weight; //total weight
    int n = 1;

    printf("Enter weight (lbs) of Type %d box: ", n);
    scanf("%d", weight);
    while(weight!=-1) //Iterate loop until w=-1 
    {
        printf("Enter quantity: \n");
        scanf("%d", quantity);

        total_weight= total_weight + (quantity*weight);
        n++;
    }

    printf("The total weight is %0.2d", total_weight);
    return 0;


}

This is not how you use scanf 这不是您使用scanf

scanf("%d", weight);
scanf("%d", quantity);

You should pass the address of the variable, not the value of the variable. 您应该传递变量的地址,而不是变量的值。

That would look like this: 看起来像这样:

scanf("%d", &weight);
scanf("%d", &quantity);

Your while loop depends on value weight . 您的while循环取决于价值weight The value of weight never changes in your loop, so the loop can never exit. weight的值在您的循环中永远不会改变,因此循环永远不会退出。


This line: 这行:

total_weight= total_weight + (quantity*weight);

uses the value of total_weight , which was never initialized. 使用从未初始化的total_weight值。
You should initialize your variables. 您应该初始化变量。


All in all, I think your fixed code should look like: 总而言之,我认为您的固定代码应如下所示:

 #include <stdio.h> int main() { int weight = 0; //weight of boxes int quantity = 0; //number of boxes int total_weight = 0; //total weight int n = 1; while(weight!=-1) { printf("Enter weight (lbs) of Type %d box: ", n); scanf("%d", &weight); // Update weight **inside** the loop printf("Enter quantity: \\n"); scanf("%d", &quantity); total_weight= total_weight + (quantity*weight); n++; } printf("The total weight is %0.2d", total_weight); return 0; } 
while(weight!=-1) //Iterate loop until w=-1 
{
printf("Enter quantity: \n");
scanf("%d", quantity);

total_weight= total_weight + (quantity*weight);
n++;
}

Problem is in while condition, weight never change it's value so the condition always true so infinite loop. 问题在于条件,而权重永远不会改变其值,因此条件始终为true,因此无限循环。

Fix this statement scanf("%d", &weight); 修复以下语句scanf("%d", &weight);

Your problem is you are trying to assign a value to the pointer of quantity and weight , instead you need to put &quantity and &weight also you do not have another input for the weight, you should also use a do while instead of a while loop. 您的问题是您正在尝试为quantityweight的指针分配一个值,相反,您需要输入&quantity&weight而且您没有重量的另一个输入,还应该使用do while而不是while循环。 It should look like this 它应该看起来像这样

#include <stdio.h>

int main()
{
int weight; //weight of boxes
int quantity; //number of boxes 
int total_weight; //total weight
int n = 1;
    do
    {
    printf("Enter weight (lbs) of Type %d box: ", n);
    scanf("%d", &weight);
    printf("Enter quantity: \n");
    scanf("%d", &quantity);

    total_weight= total_weight + (quantity*weight);
    n++;
}while (weight != -1);

printf("The total weight is %0.2d", total_weight);
return 0;

} }

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

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