Thread: .exe file has stopped working. keep getting this error when I run it!

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    3

    .exe file has stopped working. keep getting this error when I run it!

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    
    //global constants
    int numberOfNames;
    int const MAXLINE = 100;
    int const MAXARRAY = 100;
    int const TOPS = 20;
    void readArray();
    struct SurnameInfo
    {
        char*name;
        int count;
        float pctrace[6];
    }
    SurnameInfo * surname[MAXARRAY];
    
    
    void processLine(char *line, int n)
    {
        surname[n] = new SurnameInfo; //allocate memory
        char * pch = strtok(line, ","); //start tokenizing
        int len = strlen(pch); //name lenght
        surname[n] -> name = new char[len + 1]; //allocate memory
        strcpy(surname[n]->name,pch); //copy name
        surname[n]->count = atoi(strtok(NULL, ",")); //get count
        for (int i = 0; i < 6; i++) //get six values of not "(S)"
        {
            pch = strtok(NULL, ",");
            surname[n]->pctrace[i] = pch[0] == '(' ? -1: atof(pch);
        }
    }
    
    
    void readArray()
    {
        int i = 0;
    
    
    char line [MAXLINE];
    ifstream inputfile;
    inputfile.open("names.csv");
    if (!inputfile)
        {
            cout <<"No file"<< endl;
        }
        return; //can't open
    inputfile.getline(line, MAXLINE); //skip title
    inputfile.getline(line, MAXLINE);
    numberOfNames = 0; //global variable
    while (!inputfile.eof()) //not end of file
    {
        if (i++ == MAXARRAY)
            break;
        processLine(line, numberOfNames++);
        inputfile.getline(line, MAXLINE);
    }
    inputfile.close();
    }
    // Binary search
    SurnameInfo*binarySearch(char*key)
    {
        int first = 0, last = MAXARRAY - 1;
        while (first <= last)
        {
            int middle = (first + last)/2;
            if (strcmp(surname[middle]->name, key)==0)
                return surname[middle];
            else if (strcmp(surname[middle]->name, key)<0)
                first = middle + 1;
            else last = middle -1;
        }
        return NULL;
    }
    
    
    void MostPopular()
    {
        for (int r = 0; r < 6; r++)
        {
            SurnameInfo * tops[TOPS];
    
    
            for (int i = 0; i < numberOfNames; i++)
            {
                int j = i<TOPS? i-1: TOPS - 1;
                for (; j >= 0; j--)
                    if (surname[i]->pctrace[r]>tops[j]->pctrace[r])
                        tops[j+1] = tops[j];
                else break;
                if (j + 1<TOPS)tops[j+1] < surname[i];
            }
        }
    }
    void printout()
    {
        ofstream outfile;
        outfile.open("output.txt");
    
    
        for (int i = 0; i < MAXARRAY; i++)
        {
            cout<<"name " << surname[i]-> name <<"." << surname[i]->count;
            for (int r = 0; r < 6; r++)
                cout <<surname[i]->pctrace[r]<<
                endl;
        }
    }
        int main()
        {
            readArray();
            SurnameInfo*a = binarySearch("AABY");
            printout();
        }
    When I build it shows no error but as soon as i run it it says that .exe file has stopped working.
    Can some one help me out please thanks

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    817
    I haven't looked at this closely yet, but have you considered using C++ Standard Library classes and algorithms more extensively or are there constraints you have to follow? For starters, it would seem you could make this more compact and easier to debug by employing std::vector and std::string.
    Last edited by -Adrian; 03-21-2015 at 07:10 PM.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Try commenting out the last line and/or the last 2 lines in main(). Does it still crash?

  4. #4
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Maybe you should reread your readArray() function. Take a good look at the part where you check if the file is opened.
    Also it is a good idea to give a function a return value when it could fail.

    Cheers

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    3
    Thank you, it worked! it was the read array function.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    3
    Quote Originally Posted by -Adrian View Post
    I haven't looked at this closely yet, but have you considered using C++ Standard Library classes and algorithms more extensively or are there constraints you have to follow? For starters, it would seem you could make this more compact and easier to debug by employing std::vector and std::string.
    Since this is intro c++ i had to work with whats given! thank you for your input.

  7. #7
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    817
    Oh I see, that makes sense. I started with C++ by searching the web (chaotic ), which was somewhat different I think. I eventually had to go back to better familiarize myself with the core language (pointers etc.) which I had managed to circumvented in many cases. I guess both approaches have their advantages, just be aware of features introduced at a later stage that may allow you to code more tersely.
    Last edited by -Adrian; 03-22-2015 at 05:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The compiler stopped working
    By akif13 in forum C++ Programming
    Replies: 4
    Last Post: 08-03-2013, 07:08 PM
  2. File I/O stopped working.
    By Andersonsacanno in forum C Programming
    Replies: 10
    Last Post: 11-14-2012, 07:45 AM
  3. stopped working run time error.. help plz... ;(
    By fredsilvester93 in forum C Programming
    Replies: 10
    Last Post: 01-04-2012, 01:15 AM
  4. ****.exe has stopped working
    By kawaikx15 in forum C Programming
    Replies: 10
    Last Post: 11-19-2011, 07:38 AM
  5. .exe has stopped working
    By bluesky16 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2011, 12:58 PM