"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
With this, the program again "stops working" and the outputted numbers areCode:int x; while(theFile >> x) { numArray.push_back(x); }
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.
Is that function correct? Can you get to specific elements of the vector using [] like arrays?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; }
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
>> 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.
This is all that is in the file I give it --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()); }
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 --
And nothing was shown for cout<<x; It just did the same routine ofCode:while(theFile >> x) { //while there is a next int in theFile cout<<x; numArray.push_back(x); //push_back next int onto numArray }
100000000
0
0
And the program stopped working box.
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 >>.
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.
I changed it to hold 6 (that's how many numbers are in my file)Code:vector <int> numArray; //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.Code:vector <int> numArray(6); //the vector to hold the numbers
I added a cout into the while loop to see if it is actually looping
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:while(theFile >> x) { //while there is a next int in theFile cout<<"test"<<endl; numArray.push_back(x); //push_back next int onto numArray }
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?Code:if(numArray.empty()) { cout<<"numArray is empty!!"<<endl; }
This is the main function now with these little tests(and the change in declaring the vector)
Everything else is the same.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(); }
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.
There are just numbers and line breaks. Not even spaces before the line break. It reads
5
10
56
17
18
9
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".