Thread: Segmentation Fault (core dumped)

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Segmentation Fault (core dumped)

    I am currently working on a simple program and was wondering how this segmentation fault is crashing my it. I can't find where exactly this error is taking place and any assistance would be appreciated.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    
    
    float hw1,hw2,hw3,hw4;
    
    
    float proj1,proj2,proj3;
    
    
    float exam1,exam2,exam3;
    
    
       float total,totalhw,totalproj,totalexam;
    
    
    
    
    
    printf("Welcome to the CMSC104 grade calculation program. This program will calc\
    ulate the final everage and course grade for a student in CMSC104.Each homework is\
     worth 4%,each project is worth 8% and each exam worth 20%.\n");
    
    
       printf("Enter the homework 1 score:\n");
       scanf("%f",&hw1);
    
    
       printf("Enter the homework 2 score:\n");
       scanf("%f",&hw2);
    
    
       printf("Enter the homework 3 score:\n");
       scanf("%d",&hw3);
    
    
       printf("Enter the homework 4 score:\n");
    
    
       printf("Enter project 3 score;\n");
       scanf("%f",&proj3);
    
    
       printf("Enter exam 1 score:\n");
       scanf("%f",&exam1);
    
    
       printf("Enter exam 2 score:\n");
       scanf("%f",&exam2);
    
    
       printf("Enter exam 3 score:\n");
       scanf("%f",&exam3);
    
    
          }
          if(total < 80)(total > 70){
             printf("With an average of %f, your course grade is a C\n",total);
          }
          if(total < 70)(total > 60){
             printf("With an average of %f,your course grade is a D\n",total);
          }
          if(total > 60){
             printf("With an average of %f,your course grade is a F\n",total);
          }
    
    
          printf("Thank you for using the grade calculation program!");
    
    
          return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You have an extra curly brace before the if statements


    Code:
    if(total < 80)(total > 70)
    
    
    //should be
    if(total < 80 && total > 70)
    
    
    
    
    
    
    if(total > 60)
    //should be
    if(total < 60)
    Fact - Beethoven wrote his first symphony in C

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    printf("Welcome to the CMSC104 grade calculation program. This program will calc\
    ulate the final everage and course grade for a student in CMSC104.Each homework is\
    worth 4%,each project is worth 8% and each exam worth 20%.\n");
    Two things.
    1. Using \ to fold a long string is not the correct way.
    2. To print a literal %, you need to write %%. A single % will trigger some kind of conversion in printf.
    Code:
    printf("Welcome to the CMSC104 grade calculation program. This program will"
           "calculate the final everage and course grade for a student in CMSC104."
           "Each homework is worth 4%%, each project is worth 8%% and each exam "
           "worth 20%%.\n");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you are not using any format specifiers and you are printing a new line at the end, you might as well use puts instead of printf, e.g.,
    Code:
    puts("Welcome to the CMSC104 grade calculation program. This program will "
         "calculate the final everage and course grade for a student in CMSC104. "
         "Each homework is worth 4%, each project is worth 8% and each exam "
         "worth 20%.");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    float hw1,hw2,hw3,hw4;
    
    ...
    
    printf("Enter the homework 3 score:\n");
    scanf("%d",&hw3);
    Also...
    • You are not reading in the 4th homework's score.
    • You are not reading in the score for projects 1 & 2.
    • You need to calculate a total based on the scores as weighted according to your description.



    I can't find where exactly this error is taking place and any assistance would be appreciated.
    Well, does it immediately segfault? Before you can enter any data at all? Does it only segfault after you've reached a certain point?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault (core dumped)
    By Juan Cervantes in forum C Programming
    Replies: 9
    Last Post: 10-29-2012, 05:58 PM
  2. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  3. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

Tags for this Thread