CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Vector issues

  1. #1
    Join Date
    Oct 2009
    Posts
    56

    Vector issues

    Hi,

    I'm trying to make a program that reads numbers from a file into a vector. Then find the highest, lowest, average, and total of the numbers. The problem I am having is reading the numbers into the vector. My while loop doesn't return true and I'm not sure why. I also tried using the get() function, but that didn't work either. I'm not sure what kind of expression I could use to say "while there are more numbers in the file" Also, when I run the program a box comes up saying "Program stopped working, checking for solutions" It gives me three (which means my getAverage() never runs) WRONG numbers and tells me the program stopped working.

    Here is my whole code --

    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
                 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();
        
    }
    
    //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());      
    }
    Can anyone help me out here? It would be very appreciated

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Vector issues

    what does your file look like?
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Vector issues

    Why aren't you using std::min_element(), std::max_element(), and std::accumulate()?

    Also, your average computation will be done in integer arithmetic. If you want a real-valued output, you'll need to cast one of the operands of the division to double and make the getAverage() function return double.

    Oh, and your functions should be taking the vector by const reference. Passing it by value is needlessly inefficient.

  4. #4
    Join Date
    Oct 2009
    Posts
    56

    Re: Vector issues

    Well my main problem is with the vector. The file I always pass it is just some numbers separated by line breaks like this --
    5
    324
    187
    440

    And the filepath doesn't have any spaces in it.

    I just learned about the min_element, max_element, and accumulate() functions. I tried putting them in, but I was having trouble compiling. I'm not sure what to put in for parameters. I tried doing
    Code:
    return return max_element(theVector[0], theVector.size());
    and a couple other attempts but none compiled. I'm not sure what I use for a "Forward Iterator"

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Vector issues

    Use the .begin() and .end() methods of the vector. Same for most STL containers/algorithms.

    The code looks fine to me otherwise.

  6. #6
    Join Date
    Oct 2009
    Posts
    56

    Re: Vector issues

    Whenever I run the program I get no couts for "test". I also get the message "numArray is empty!!" So apparently my vector is not having values put in it? This is what has been killing me for this program. Is there any other kind of expression I could use for my while loop? I have been told that theFile>>x is probably best, but it doesn't seem to be working.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Vector issues

    The only possible explanation for that behavior is that the file contains bytes which represent something other than whitespace and digits.

    Are you positive that it's a plain-text file, or might it be something like .doc format? Open it in Notepad or something equivalently simple and see what it looks like there.

  8. #8
    Join Date
    Oct 2009
    Posts
    56

    Re: Vector issues

    Okay it is working now. I originally made the file in Wordpad and saved it as a .txt instead of a .rtf. However when I opened it in notepad it displayed this --

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
    {\*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\f0\fs20 "5\par
    10\par
    56\par
    17\par
    18\par
    9"\par
    }


    So I deleted it, remade one in notepad and it worked fine. Do you know what would make the original file display all the extra stuff?

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vector issues

    Quote Originally Posted by SterlingM View Post
    Okay it is working now. I originally made the file in Wordpad and saved it as a .txt instead of a .rtf. However when I opened it in notepad it displayed this --
    Which is an RTF format.

    You either didn't save it as a text file, or some other error. Regardless, you should use "true" text editors to save text files (i.e. NotePad), so that there is no chance of some other format being used to save or edit the file..

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured