Thread: Seg fault issues

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Seg fault issues

    hey again.

    I am getting a seg-fault when I run my program but my debugger is not telling me where it is. Usually it highlights the line in blue where the code broke. I have highlighted below where the code does crash, can anyone see what I have done wrong to cause this?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    // function prototypes
    const int getArraySize();
    void getEmployeeNames ( const int& );
    void getSalaryAmounts ( std::string[], const int& );
    void displayResults ( std::string[], double[], double[], const int& );
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       std::cout << std::setprecision( 2 ) << std::fixed;
       
       const int ARRAY_SIZE = getArraySize();
       
       getEmployeeNames ( ARRAY_SIZE );
          
       std::cin.get(); // freeze console output window
          
       return 0; // return value from int main
    }
    
    // function to find out how many employees will be calculated
    const int getArraySize()
    {
       int totalEmployee = 0;
       
       while ( totalEmployee == 0 )
       {
          std::cout << "Enter total employees: ";
          std::cin >> totalEmployee;
       }
       
       const int ARRAY_SIZE = totalEmployee;
       
       return ARRAY_SIZE;
    }
    
    // function to gather the names of the employees
    void getEmployeeNames ( const int &rSIZE )
    {
       std::cin.ignore();
       
       std::string employeeNames[ rSIZE ];
       
       for ( int i = 0; i < rSIZE; i++ )
       {
          std::cout << "Enter employee name " << i << ": ";
          std::getline ( std::cin, employeeNames[ i ] );
          std::cout << std::endl;
       }
       
       getSalaryAmounts ( employeeNames, rSIZE );
    }
    
    // function to calculate employee salaries
    void getSalaryAmounts ( std::string empNames[], const int &rSIZE )
    {
       double totalProffit = 0;
       double salary = 0;
       double proffit[ rSIZE ];
       double salaryArray[ rSIZE ];
       
       for ( int i = 0; i < rSIZE; i++ )
       {
          std::cout << "Enter total proffit for " << empNames[ 0 ] << ": ";
          std::cin >> totalProffit;
          
          proffit[ i ] += totalProffit;
             
          salary = totalProffit * 0.09 + 200;
             
          salaryArray[ i ] += salary;
          empNames++;  
        }      
        
        // THE CODE BREAKS HERE ///////////////////////////////////
    
        displayResults ( empNames, proffit, salaryArray, rSIZE );
    }
    
    // function to display the results
    void displayResults ( std::string empNames[], double proff[], double sal[],
                          const int &rSIZE )
    {
       std::cout << "\nEMPLOYEE NAME" << std::setw( 12 ) << "PROFFIT" 
                 << std::setw( 12 ) << "SALARY\n\n";
                 
       for ( int i = 0; i < rSIZE; i++ )
       {
          std::cout << empNames[ i ] << std::setw( 14 ) << proff[ i ] 
                    << std::setw( 14 ) << sal[ i ] << std::endl;
       } 
    }
    Double Helix STL

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    proffit[ i ] += totalProffit;
    proffit[i] is not initialized, why are you using +=

    salaryArray[ i ] += salary;
    same here

    also your size is not constant - I failed even to compile this code
    You should use vector to create array with the size unknown at the compilation time
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thank you Vart. I guess Im making my life to harder not using vectors. Il make the changes you suggested.
    Double Helix STL

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I couldn't see what was wrong, but after some debugging:
    Code:
          empNames++;  
    ...
        displayResults ( empNames, proffit, salaryArray, rSIZE );
    Your empNames is now pointing at garbage employee[rSize].

    By the way, passing const int &rSize is meaningless - it passes a 4 byte value as a reference, when it really just needs to pass a 4 byte value in the first place.

    Code:
          proffit[ i ] += totalProffit;
    Doesn't look right - I think you just want an assignment, not an increment by operator.
    And profit is spelled with one 'f'.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM