Reading In from File/ Array

Hello, I am trying to read in doubles from a file and when I try to display nothing displays, but the file is in the same directory. This is my code.

( this is the form of the data in the .dat file
123
3.5
5.5 6.0 7.0 6.5 6.5
)

this is my code the only reason I am displaying the array in this function is because I want to make sure it works before I create the rest of my program.

void readScores(int rdDiverNumber[],double rdDifficultyFactor[],double rdScores[][6])
{

ifstream file;
string fileName;

cout<< "Enter data file name: ";
cin >> fileName;

file.open(fileName.c_str());

if (file.fail())
cout << "No Data" << endl;
exit(0);

for (int r=0; r<10; r++)
{ file >> rdDiverNumber[r];

for(int d=0; d<5; d++)
{
file >> rdDifficultyFactor[d];
double max=0;
double min= 100;
double sum=0;
double score=0;

for (int c=0 ; c<5; c++)
{
file >> score;
sum = score + sum;
if ( score > max)
max= score;
if ( score < min)
min= score;
}
sum= sum - max;
sum= sum - min;
sum= sum * rdDifficultyFactor[d];
rdScores[r][d]= sum;

}
for( int row=0;row<10;row++)
{
for(int column=0;column<5; column++)
cout << rdScores[row][column] << "";
}
cout << endl;
}
}
Use this example below that i've made. it is using the c++11. Anyway, it does basically the same thing as yours.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <vector>
#include <stdexcept>





int main()
{
    std::ifstream file;
    std::string filename,line;
    std::vector<double> array;

    std::cout << "Path:" << std::endl;
    std::cin >> filename;    
    file.open(filename);
    if(!file.is_open())
    {
        std::cout << "couldn't open" << std::endl;
        exit (0);
    }

    while(std::getline(file,line))
    {
        if(line.empty()) continue;
        try
        {
            array.push_back(std::stod(line));
        }
        catch (std::exception& e)
        {
            std::cerr << "exception caught: " << e.what() << std::endl;
        }
    }

    double max = 0, min = 0 , sum = 0;

    bool used = false;
    for(auto &it:array)
    {
        if(!used)
        {
            max = it;
            min = it;
            sum += it;
            used = !used;
        }
        else
        {
            if(it > max) max = it;
            if(it < min) min = it;
            sum += it;
        }
    }

    std::cout << "max=" << max << std::endl;
    std::cout << "min=" << min << std::endl;
    std::cout << "sum=" << sum << std::endl;
    return 0;
}
Topic archived. No new replies allowed.