Segmentation fault with array?

This is a discussion on Segmentation fault with array? within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am getting a segmentation fault when trying to access an array... I know this is silly, am ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    1

    Lightbulb Segmentation fault with array?

    Hello all, I am getting a segmentation fault when trying to access an array... I know this is silly, am I overlooking something? What the hell? The problem seems to occur when accessing an array from the bigFish function. Please teach me..

    Code:
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    struct fish{
    int contestant[99999];
    int fishNumber[99999];
    float fishWeight[99999];};
    void readFiles(int contest[20], string name[20], int fishNum[5], 
    string fishType[5], fish fishStruct);
    void bigFish(int contest[20], string name[20], int fishNum[5], 
    string fishType[5], fish fishStruct);
    
    int main()
    {
     fish fishStruct;
     int choice = 0; //choice in menu
     cout.setf(ios::fixed, ios::floatfield);
     cout.setf(ios::showpoint);
     cout.precision(2);
     
     int contest[20];
     string name[20];
     int fishNum[5];
     string fishType[5];
     readFiles(contest, name, fishNum, fishType, fishStruct);
     
     cout<<"MENU: PLEASE MAKE A SELECTION"<<endl<<endl;
     cout<<"1. Display all data for a contestant"<<endl;
     cout<<"2. Display total catch weight for fish type"<<endl;
     cout<<"3. Display largest fish caught"<<endl<<endl;
     cout<<"Choose and option between 1-3"<<endl;
     cin>>choice;
     if (choice == 3)
     bigFish(contest, name, fishNum, fishType, fishStruct);
     return 0;
    }
    
    void readFiles(int contest[20], string name[20], int fishNum[5], string fishType[5], fish fishStruct)
    {
    
     
     ifstream inFile;
     inFile.open("contestantname.data");
     if (!inFile)
     {
     cout<<"contestantname.data not found"<<endl;
     }
     //clearing out the array
     for(int i = 0; i < 20; i++)
     {
     contest[i]=0;
     name[i] = " ";
     }
     int count1 = 0;
     while(inFile)
     {
     inFile>>contest[count1];
     inFile>>name[count1];
     cout<<name[count1]<<endl;
     count1++;
     }
     inFile.close();
     
     inFile.open("fishtype.data");
     if (!inFile)
     {
     cout<<"fishtype.data not found"<<endl;
     }
     //clearing out the array
     for(int i = 0; i < 20; i++)
     {
     fishNum[i]=0;
     }
     int count2 = 0;
     while(inFile)
     {
     inFile>>fishNum[count2];
     inFile>>fishType[count2];
     count2++;
     }
     inFile.close();
    
     inFile.open("catch.data");
     if (!inFile)
     {
     cout<<"catch.data not found"<<endl;
     }
     //clearing out the array
     for(int i = 0; i < 20; i++)
     {
     fishStruct.contestant[i]=0;
     fishStruct.fishNumber[i]=0;
     fishStruct.fishWeight[i]=0;
     }
     int count3 = 0;
     while(inFile)
     {
     inFile>>fishStruct.contestant[count3];
     inFile>>fishStruct.fishNumber[count3];
     inFile>>fishStruct.fishWeight[count3];
     count3++;
     }
     inFile.close();
     
    
    }
    void bigFish(int contest[20], string name[20], int fishNum[5], 
    string fishType[5], fish fishStruct)
    {
     int num = 0; //temp contestant number
     int totalWeight = 0; //weight for each contestant
     string tempName; //temp contestant name
     cout<<"Please enter contestant number"<<endl;
     cin>>num;
     for(int i = 0; i < 20; i++)
     {
     cout<<name[i]; //THIS IS WHERE //I AM TROUBLED 
     
    
     }
     
    }
    Last edited by Salem; 09-12-2004 at 03:45 PM. Reason: Tagging - though code should be reposted to fix the includes

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Welcome to the board.

    With that said..
    Please use code tags. (See top of forum)

    [edit]
    I notice your arrays are quite large.

    You might want to try and allocate the memory for them into a try block.

    Code:
    int *variable;
    
    try {
    
        variable = new int [99999];
    
    }
    
    You might be having trouble there.
    
    catch(std::bad_alloc &b) {
    
         std::cout << b.what;
    
    }
    Last edited by Vicious; 09-12-2004 at 02:54 PM.
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (pointer to an array)
    By Lang in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 01:22 AM
  2. Basic array segmentation fault
    By Argentius in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 05:50 AM
  3. strcat segmentation fault
    By captain-cat in forum C Programming
    Replies: 3
    Last Post: 07-20-2004, 11:29 AM
  4. Replies: 3
    Last Post: 04-19-2004, 07:42 PM
  5. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21