How to fix segmentation fault for while loop in c

how to fix segmentation fault in c
segmentation fault (core dumped) c
segmentation fault (core dumped) c++
how to debug segmentation fault in c
how to debug segmentation fault in linux
segmentation fault (core dumped) ubuntu
segmentation fault (core dumped) linux
segmentation fault linux

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). 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. Your program should print Type 1, 2, 3, etc. as shown in the output below.

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. What am I doing wrong? I'm sorry I'm new to 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("%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. The value of weight never changes in your loop, so the loop can never exit.


This line:

total_weight= total_weight + (quantity*weight);

uses the value of total_weight, which was never initialized. 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;
}

Pset4 Recover, Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” The below program may crash (gives segmentation fault error) because the line *(str+1) as now this statement is illegal. Common segmentation fault scenarios: Modifying a string literal : The below program may crash (gives segmentation fault error) because the line *(str+1) = ‘n’ tries to write a read only memory.

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.

Fix this statement scanf("%d", &weight);

Core Dump (Segmentation fault) in C/C++, as commandline arguments, adds them to an array with a while loop and then uses a 2nd … What is causing the Segmentation Fault (core dumped) error? 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.

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

}

Identify what's causing segmentation faults (segfaults), Within my master while loop im printing the character array I am getting a segmentation fault after exiting the master while loop. If i put in a variable like ( char names[a]) I get an error saying undifined variable in structure. List of Common Reasons for Segmentation Faults in C/C++ C C++ Server Side Programming Programming The main reason for segmentation fault is accessing memory that is either not initialized, out of bounds for your program or trying to modify string literals.

[C] Why is my program is printing out a Segmentation fault (core , In computing, a segmentation fault (often shortened to segfault) or access violation is a fault, Segmentation faults are a common class of error in programs written in The proximate cause is a memory access violation, while the underlying In C code, segmentation faults most often occur because of errors in pointer use,� On some systems, this will cause a "stack overflow" report, and on others, it will merely appear as another type of segmentation fault. The strategy for debugging all of these problems is the same: load the core file into GDB, do a backtrace, move into the scope of your code, and list the lines of code that caused the segmentation fault.

Simple Structure yet Segmentation Fault!, Request for Citation in Overview[edit]. The second paragraph of the overview reads;. The term I'm fine with the runaway recursion, but infinite loops? Maybe Also, the merger of segmentation fault and bus error is very unfortunate. Why does the Turbo C compiler doesn't shows it while it is rampant in the GNU compiler? Segmentation error is vary common while using a array or in a pointer in programming ie, C, and C++ or CPP. Now you have to check the index of an array of each point and also check lower and upper condition of loop. I suggest use Top to bottom approach line by line.

Segmentation fault, I am facing a compilation issue while working on OpenEmbedded BitBake. I get an "internal How do I solve the "Signal: SIGSEGV (Segmentation fault)" error in C++? Look for the loop structure, usually the are the reason that messes it all . Therefore, the while loop continues to look for that newline and it overflows the buffer. That’s how you get a segmentation fault. That’s how you get a segmentation fault. I’ve redone the function, shown below, to account for stream input and a lack of newline.

Comments
  • Turn on compiler warnings. Fix them all.
  • scanf("%d", weight); ==> scanf("%d", &weight); and same for quantity.
  • And do check the return value of scanf()
  • Even after my fixes to the code, I spotted one more bug that remains. But this should get you going.