Jul 2, 2016 at 7:03pm UTC
Doing something wrong when storing numbers in dynamic array. I need to be able to store these numbers in the array and then find average. It works fine for now but I will eventually need to put it in sperate function and return the array and so I might have a problem then
#include <iostream>
#include<fstream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
//double* read_file(int& size, ifstream& infile)
//{
//}
int main(/*int arg, char* array[]*/)
{
int size;
ifstream infile;
//int size = 0;
string score;
int NumberScores = 0;
int number_lines = 0;
string file;
double TotalScore = 0;
infile.open("scores.txt");
string y;
while(getline(infile, file))
{
number_lines ++;
}
string *Score_array = new string[number_lines];
double *TestScores = new double[number_lines];
/*string Score_array[15];
double TestScores[15];*/
infile.clear();
infile.seekg(0,infile.beg);
while(getline(infile, file))
{
Score_array[NumberScores] = file;
int x = file.length();
int idx = file.find("\t");
y = file.substr(idx + 1, x -idx);
istringstream (y) >> TestScores[NumberScores];
TotalScore = TotalScore + TestScores[NumberScores];
NumberScores ++;
}
double AverageScore = TotalScore / NumberScores;
delete[] Score_array;
delete[] TestScores;
cout << AverageScore << endl;
system("Pause");
return 0;
}
Last edited on Jul 2, 2016 at 7:45pm UTC