read values in a file into an array

I'm a student taking an intro C++ course. For a project, I must read values in a file into an array, the file looks like this:
1
2
3
12 56 78.1 80 10
56 78 1 32.4 20 45 68 34 
12 87 50 49 23


Since the number of lines and the number of values on each line will change (the program must work with several different data files), I'm not sure how to read in the values to an array. The array will be two dimensional, with the first line of the file being the first line of the array and so on. Any help will be much appreciated.
What do these values represent? Do different lines represent different students and different values in a line grades of that particular student? A two dimensional array is not the best container you could choose to store that kind of data. Are you allowed to use structs? Vectors (or dynamic arrays)?
Last edited on
The values represent sales numbers, with each line being a different department. My professor specifically requires a two-dimensional array, because that's what we've been learning about and other containers are not covered in the intro course. I think I've figured out how I'm going to read the values in to my array, but I'm encountering another problem. For some reason, my loop to read the values in to the array seems not to be running (the code compiles with no errors). Here's what I've got so far :
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
ifstream infile;
  char filename[30];
  int numDepartments=-1;
  string line;

  /*Counters for the loop that
    reads values in to the array */
  int count;
  int countDepartment=0;

/*Ask the user for a file name
    and check that the file opens */
  cout << "Please enter the name of the data file: ";
  cin >> filename;
  infile.open(filename);
  if(!infile)
    { cout << "Error opening file. \n";
      cout << "Terminating Program \n";
      exit(1);
    }

  /* count the number of lines in the data file */
while(!infile.eof())
  {   getline(infile,line);
    numDepartments++;
  }
/*reset the pointer to the beginning of the file */
 infile.seekg(0, ios::beg);

/*Array to store sales */
 float sales [numDepartments] [12];

 /*read in sales numbers from file */
 while(!infile.eof())
   {if (count<11)
   {infile >> sales [countDepartment][count];
     count++;
     cout << count;}
   else
     {count=0;
       countDepartment++;}
   }
}

Last edited on
Try putting a infile.clear() call before attempting to get your values. It resets some flags (including the eof flag) and allows you to use the infile object for input again. There's something else I want to ask. Does the number of values per line remain the same throughout a single file? It doesn't look like it does in your first example, but you seem to assume it's 12 in your code.
Last edited on
I was initially confused; there will always be 12 values per line because each value represents the sales for one month - that's why I'm assuming 12. The infile.clear() worked perfectly.. now on to another issue. I've got the loop to read the values in to the array, but it stops after the first line for some reason :
1
2
3
4
5
6
7
8
9
10
11
12
 /*read in sales numbers from file */
 while(!infile.eof())
/* this loop should read 12 values in to the array */
   { while (count<12)
   {infile >> sales [countDepartment][count];
     count++;
     cout << count;}
/* after the loop, the counter is reset so that the loop will start over on the next run of the outer loop */
       count=0;
       countDepartment++;}

}


The comments describe how I want it to work - it should get 12 values from the first line, then move on to the next line for 12 more values. The countDepartment variable is incremented to track which line number is currently being processed, and sets the first value of the array. When I run the program, however, only the first line of the file is processed and the program doesn't move on to the second line..
What's the initial value of count? It should be zero. Is it?
Last edited on
yeah, it begins as 0
What happens? Does the program end? Does it hang? Does it crash?
Could you post the whole code and the contents of your file?
Last edited on
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
63
64
65
66
/* Project 6 - James Bagenstose */
#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

float getSales();
float averageSales();
void compareSales();
void aboveStandard();
void output();

int main ()
{
  getSales();



  return 0;
}

float getSales()
{ifstream infile;
  char filename[30];
  int numDepartments=-1;
  string line;

  /*Counters for the loop that
    reads values in to the array */
  int count=0;
  int countDepartment=0;

/*Ask the user for a file name
    and check that the file opens */
  cout << "Please enter the name of the data file: ";
  cin >> filename;
  infile.open(filename);
  if(!infile)
    { cout << "Error opening file. \n";
      cout << "Terminating Program \n";
      exit(1);
    }

  /* count the number of lines in the data file */
while(!infile.eof())
  {   getline(infile,line);
    numDepartments++;
  }
/*reset the pointer to the beginning of the file */
 infile.clear();

/*Array to store sales */
 float sales [numDepartments] [12];

 /*read in sales numbers from file */
 while(!infile.eof())
   { while (count<12)
 {infile >> sales [countDepartment][count];
     count++;
     cout << count;}

       count=0;
       countDepartment++;}

}

When I run the program, the loop that uses the 'count variable runs 12 times, but after those 12 runs, it should move on to the next line and run another 12 times, until it reaches the end of the file. It appears that it is only running the initial 12 times, then stopping.
Last edited on
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
63
64
65
66
67
68
69
70
71
72
/* Project 6 - James Bagenstose */
#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

float getSales();
float averageSales();
void compareSales();
void aboveStandard();
void output();

int main ()
{
  getSales();



  return 0;
}

float getSales()
{ifstream infile;
  char filename[30];
  int numDepartments=-1;
  string line;

  /*Counters for the loop that
    reads values in to the array */
  int count=0;
  int countDepartment=1;

/*Ask the user for a file name
 
   reads values in to the array */
  int count=0;
  int countDepartment=1;

/*Ask the user for a file name
    and check that the file opens */
  cout << "Please enter the name of the data file: ";
  cin >> filename;
  infile.open(filename);
  if(!infile)
    { cout << "Error opening file. \n";
      cout << "Terminating Program \n";
      exit(1);
    }

  /* count the number of lines in the data file */
while(!infile.eof())
  {   getline(infile,line);
    numDepartments++;
  }
/*reset the pointer to the beginning of the file */
 infile.clear();

/*Array to store sales */
 float sales [numDepartments] [12];

 /*read in sales numbers from file */
 for(numDepartments;numDepartments>0;numDepartments--)
   { for(count;count<13;count++)
   {infile >> sales [1][count];
     cout << count;
     if (count==12) break;}
     cout << "\n";
     count=0;
       countDepartment++;}
 cout << sales [1] [1];
}


figured out the nested loops issue; changed them both to for loops and everything's good; the updated code is posted above.
clear() doesn't move the file pointer to the beginning of the file, it clears any status bits. to move to the beginning of the file, you could use seekg(ios::beg)
1
2
3
4
5
6
7
23 33.5 21 23 25 56 54 43 34.2 35.4 34 69.5
24 35.2 24 26 43 56.7 54 32 43 34 34 57.9
24 42 43 35 52 56 67 54 56 45.3 32 32
20 32 45 72 45.4 63.2 45 56 52 65 53 65
34 35 37.5 32 23 45 31 43 52 43 76 65
35 56 63.4 45.2 45.6 56 67.3 45 56.3 67 78 76
34.2 45 62 19 45 39 38 37 82 74 45 58.4


this is the contents of my data file. Using the code above and this data file, I get "NaN" when I try to cout a value from the array (sales [1] [1]) - I've tried changing several things and this error persists; does anyone know what could be causing this?
There are many problems with your code as is:

1. Lines 37-38 is a repeat of Lines 31-32 (won't compile)
2. Line 60 is a little dangerous. This kind of construct is known as a VLA - variable length array or figuring out the size of your automatic array variable at runtime. Before C99, this wasn't even legal. Afterwards, some C++ compilers support it (eg gcc) while others don't (eg Visual Studio).
3. Rewrite and reformat your code to comply with the standard way of doing for loops:
1
2
3
int count, limit=12;
for ( count=0; count<limit; ++count ) {
}

The way you have it coded with break; and variables initialized all over the place obfuscates your code and makes it harder to debug.

Follow these suggestions, retry, and then repost your new code with questions.
Topic archived. No new replies allowed.