Thread: Program stopping working

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Program stopping working

    Whenever I run this program, I input a path to the file. Once I hit Enter a box comes up saying
    "NumberAnalysis.exe has stopping working
    A problem caused the program to stop working correctly.
    Windows will close the program and notify you if a solution is available."

    I give it a simple .txt file with some numbers separated by a line break.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    int getLowest(vector<int> theVector);
    int getHighest(vector<int> theVector);
    int getTotal(vector<int> theVector);
    int getAverage(vector<int> theVector);
    
    
    vector <int> numArray;
    
    int main(int argc, char *argv[]) {
        string filepath;
        cout<<"Please enter a file path."<<endl;
        cin>>filepath;
        ifstream theFile(filepath.c_str());
        if(!theFile.is_open())
             cout<<"File could not be opened!"<<endl;
        else {
             char x;
             int count = 0;
             while(theFile.get(x)) {
                 numArray[count] = (int)x;
                 count++;
             }
             cout<<"The lowest number here is "<<getLowest(numArray)<<"."<<endl;
             cout<<"The highest numer here is "<<getHighest(numArray)<<"."<<endl;
             cout<<"The total of the numbers are "<<getTotal(numArray)<<"."<<endl;
             cout<<"The average of the numbers is "<<getAverage(numArray)<<"."<<endl;
        }    
        cin.get();
        
    }
    
    
    int getLowest(vector<int> theVector) {
        int lowest = 100000000;
        for(int i=0;i < theVector.size();i++) {
                if(lowest > theVector[i]) 
                          lowest = theVector[i];
                }
        return lowest;
    }
    
    int getHighest(vector<int> theVector) {
        int highest = 0;
        for(int i=0;i < theVector.size();i++) {
                if(highest < theVector[i])
                           highest = theVector[i];
                }    
        return highest;
    }
    
    int getTotal(vector<int> theVector) {
        int total = 0;
        for(int i=0;i < theVector.size();i++) 
                total += theVector[i];
        return total;    
    }
    
    int getAverage(vector<int> theVector) {
        return (getTotal(theVector) / theVector.size());      
    }
    Any idea as to why this is happening? Or any website I can check out to see the problem?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Also, is there a function for ifstream that I could use to return ints instead of using .get(x) and typecasting to int?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(theFile.get(x)) {
        numArray[count] = (int)x;
        count++;
    }
    The index's don't magically exist or get created simply because you say so. You need to push_back values onto the vector.
    "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

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Try debugging (or at least printing hard-coded debug messages) to find out where the program fails.

    If you're opening the file as a stream, you should be able to use >> and << to read and write from it.
    ...or perhaps you should try using numArray.push_back(x); instead of numArray[count] = (int)x;

    EDIT: oops, I took forever to write my post, looks like someone beat me :-)

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Okay I used the push_back function, but my problem now is how to put ints into the vector.

    Code:
             while(theFile.get()) {
                 numArray.push_back(theFile.get());
    I tried that but when I input the path file everything just stops until I alt+f4.

    Code:
             char x;
             while(theFile.get(x)) {
                 numArray.push_back((int)x);
    When I tried that the vector doesn't get filled with the actual numbers in the file. I'm guessing typecasting chars into ints doesn't work so well?

    Code:
             int x;
             while(theFile.get(x)) {
                 numArray.push_back(x);
    And that doesn't even compile.

    I'm looking at the get reference get - C++ Reference
    but it isn't making much sense to me.


    int get();
    Extracts a character from the stream and returns its value (casted to an integer).

    I also read that get() returns false if there is nothing to extract which is why I used it for the while loop expression. So now I'm kinda confused on why this says it returns an int and not a bool. Can anyone point me in the right direction here? Any help is appreciated.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use operator>> to read an int.
    Code:
    while(theFile >> x)

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Okay I changed the while expression but it's back to telling me the program stopped and is giving me the wrong numbers.

    Code:
            int x;
             while(theFile >> x) {
                 numArray.push_back(x);
             }

    Code:
            int x;
             while(theFile >> x) {
                 numArray.push_back(theFile.get());
             }
    I tried both of these and they give me the same output.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    try

    Code:
    while(!theFile.eof())
    {
      theFile>>x;
      numArray.push_back(x);
    }

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That method shouldn't work any better, m37h0d, and will usually be worse (by adding a duplicate value at the end). You don't want to use eof() to control the loop.

    Your first loop with while(theFile >> x) is correct, SterlingM. The problem is somewhere else in the code. I don't see it at the moment, but I wouldn't change that part right there any more.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, the program works fine for me just by making that change. Maybe you changed something else? Post your latest full code and maybe even the input file text you are using.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the reference says that >> returns ifstream&; does implementing the ! operator provide an implicit conversion to bool?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> does implementing the ! operator provide an implicit conversion to bool?

    There is an implicit conversion to bool, but it's not because of the implementation of operator!. It's because there is a conversion to void* made available by the stream classes and void* converts to bool.

    It's not an obvious solution, but it works best when reading from streams.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    .

    This is the full code

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    int getLowest(vector<int> theVector);
    int getHighest(vector<int> theVector);
    int getTotal(vector<int> theVector);
    int getAverage(vector<int> theVector);
    
    
    vector <int> numArray;
    
    int main(int argc, char *argv[]) {
        string filepath;
        cout<<"Please enter a file path."<<endl;
        cin>>filepath;
        ifstream theFile(filepath.c_str());
        if(!theFile.is_open())
             cout<<"File could not be opened!"<<endl;
        else {
             int x;
             while(theFile >> x) {
                 numArray.push_back(theFile.get());
             }
             cout<<"The lowest number here is "<<getLowest(numArray)<<"."<<endl;
             cout<<"The highest numer here is "<<getHighest(numArray)<<"."<<endl;
             cout<<"The total of the numbers are "<<getTotal(numArray)<<"."<<endl;
             cout<<"The average of the numbers is "<<getAverage(numArray)<<"."<<endl;
        }    
        system("PAUSE");
    }
    
    
    int getLowest(vector<int> theVector) {
        int lowest = 100000000;
        for(int i=0;i < theVector.size();i++) {
                if(lowest > theVector[i]) 
                          lowest = theVector[i];
                }
        return lowest;
    }
    
    int getHighest(vector<int> theVector) {
        int highest = 0;
        for(int i=0;i < theVector.size();i++) {
                if(highest < theVector[i])
                           highest = theVector[i];
                }    
        return highest;
    }
    
    int getTotal(vector<int> theVector) {
        int total = 0;
        for(int i=0;i < theVector.size();i++) 
                total += theVector[i];
        return total;    
    }
    
    int getAverage(vector<int> theVector) {
        return (getTotal(theVector) / theVector.size());      
    }
    I was out for a bit so I haven't looked at the while since I left. The input string I give it for the file is always -
    C:\Users\Sterling\Testing.txt

    When you said it worked after making that change, do you mean the while loop being while(theFile >> x) ? Or are you talking about something else?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> numArray.push_back(theFile.get());

    Oops! You're using the wrong loop. Your first loop with while(theFile >> x) in post #7 was the correct one.

    And yes, making that small change makes the program work for me.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Quote Originally Posted by Daved View Post
    >> numArray.push_back(theFile.get());

    Oops! You're using the wrong loop. Your first loop with while(theFile >> x) in post #7 was the correct one.

    And yes, making that small change makes the program work for me.
    ?

    My while loop expression is still the same as in post 7, it is still
    while(theFile >> x)

    Or are you saying what's inside the loop is wrong? Doesn't get() return an int? I tried doing both assignments that I had in post 7 for inside the while loop and they give me the same output just like they did earlier. I apologize if what I'm missing is obvious, but I'm confused about your post and get().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  3. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  4. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM
  5. One error stopping me run program.
    By andy bee in forum C Programming
    Replies: 1
    Last Post: 08-28-2001, 06:57 PM