Thread: Program stopping working

  1. #16
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by SterlingM View Post
    ?

    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().
    You shouldn't need the get call at all. Just push_back the x value you've already read.

    Code:
    while(theFile >> x) {
                 numArray.push_back(x);
             }
    "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

  2. #17
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Code:
             int x;
             while(theFile >> x) {
                 numArray.push_back(x);
             }
    With this, the program again "stops working" and the outputted numbers are
    100000000
    0
    0

    and it doesn't give one for the average. This tells me nothing is being put into the vector or my other functions are wrong.

    Code:
    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;
    }
    Is that function correct? Can you get to specific elements of the vector using [] like arrays?

  3. #18
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    At this point in your learning, you should be coding one section of code and making sure it works before coding the next.
    Comment out everything else. See if you can read the file into the vector and print it out before doing anything else.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> With this, the program again "stops working" and the outputted numbers are

    The code works perfectly for me.

    Sorry to make you do this, but please post the whole code again, with the push_back part fixed. Also, please post the contents of your input file. Also, make sure you are completely rebuilding your program and testing the new executable. You might just be running the old one.


    >> Can you get to specific elements of the vector using [] like arrays?
    Yes. You coded that, didn't you? Wouldn't you already know?
    Last edited by Daved; 10-16-2009 at 01:27 PM.

  5. #20
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    //function prototypes
    int getLowest(vector<int> theVector);   
    int getHighest(vector<int> theVector);
    int getTotal(vector<int> theVector);
    int getAverage(vector<int> theVector);
    
    
    vector <int> numArray;     //the vector to hold the numbers
    
    int main(int argc, char *argv[]) {
        string filepath;
        cout<<"Please enter a file path."<<endl;
        cin>>filepath;
        ifstream theFile(filepath.c_str());     //make an ifstream for the given path
        if(!theFile.is_open())                  //if it couldn't be opened...
             cout<<"File could not be opened!"<<endl;  
        else {
             int x;
             while(theFile >> x) {    //while there is a next int in theFile
                 numArray.push_back(x);       //push_back next int onto numArray
             }
             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;
             theFile.close();
        }    
        cin.get();
    }
    
    //go through vector and find smaller values
    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;
    }
    
    //go through vector and find larger values
    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;
    }
    
    //get total of all the numbers in the vector
    int getTotal(vector<int> theVector) {
        int total = 0;
        for(int i=0;i < theVector.size();i++) 
                total += theVector[i];
        return total;    
    }
    
    //get the average of all numbers in the vector
    int getAverage(vector<int> theVector) {
        return (getTotal(theVector) / theVector.size());      
    }
    This is all that is in the file I give it --

    5
    10
    56
    17
    18
    9


    Just some random numbers separated by a line break. Tried not separating them with a line break and got the same results. Also tried just one giant number and got the same results. Whenever it shows the numbers
    10000000
    0
    0
    it doesn't give me one for getAverage(). So is something wrong with getAverage()? I also tried putting a cout in the while loop like this --

    Code:
             while(theFile >> x) {    //while there is a next int in theFile
                 cout<<x;
                 numArray.push_back(x);       //push_back next int onto numArray
             }
    And nothing was shown for cout<<x; It just did the same routine of
    100000000
    0
    0
    And the program stopped working box.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you're getting zero entries. It should complain if it can't open the file, but I would double-check the filename that you are using (maybe print it out) -- for sure, if your filename or path contains spaces, you are not going to be able to read it in with >>.

  7. #22
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    The filename doesn't contain any spaces, it is just C:\Users\Sterling\Testing.txt.
    I made some slight changes...

    When I made the vector, I didn't have a size for it.

    Code:
    vector <int> numArray;     //the vector to hold the numbers
    I changed it to hold 6 (that's how many numbers are in my file)
    Code:
    vector <int> numArray(6);     //the vector to hold the numbers
    When I did this, the "program stopped working" box did not come up. But then I got four zeroes for numbers.


    I added a cout into the while loop to see if it is actually looping
    Code:
             while(theFile >> x) {    //while there is a next int in theFile
                 cout<<"test"<<endl;
                 numArray.push_back(x);       //push_back next int onto numArray
             }
    And "test" didn't show one time. So I figured theFile >> x wasn't reading any numbers. So I added an if statement after it to check.
    Code:
        if(numArray.empty())  {
             cout<<"numArray is empty!!"<<endl;
             }
    When I ran the program, this message did not show. Just gave me 4 zeroes. So the while loop isn't looping, but the vector isn't empty?

    This is the main function now with these little tests(and the change in declaring the vector)
    Code:
    vector <int> numArray(6);     //the vector to hold the numbers
    
    int main(int argc, char *argv[]) {
        string filepath;
        cout<<"Please enter a file path."<<endl;
        cin>>filepath;
        ifstream theFile(filepath.c_str());     //make an ifstream for the given path
        if(!theFile.is_open())                  //if it couldn't be opened...
             cout<<"File could not be opened!"<<endl;  
        else {
             int x;
             while(theFile >> x) {    //while there is a next int in theFile
                 cout<<"test"<<endl;
                 numArray.push_back(x);       //push_back next int onto numArray
             }
             if(numArray.empty())  {
                      cout<<"numArray is empty!!"<<endl;
                  }
             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;
             theFile.close();
        }    
        cin.get();
        cin.ignore();
        
    }
    Everything else is the same.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, numArray won't be empty, since you have 6 numbers in it.

    Is there something (other than a number) at the start of the file? Once a read error happens, your program will stop.

  9. #24
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    There are just numbers and line breaks. Not even spaces before the line break. It reads
    5
    10
    56
    17
    18
    9

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then you need to check the path you give your program and compare it to where the file really lives. (The path you told us in the thread could be the right one, although it is somewhat strange for windows.) I will add on to the chorus of "it works here, if you type a file name without spaces".

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