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.
Any idea as to why this is happening? Or any website I can check out to see the problem?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());
}