What's wrong with this code: inputting from a file into an array

Hi everyone! So I need to write a code that takes an input file(I've included what the input file looks like below) and then sorts each different component into different arrays. The first number in the file is how many people there is data for, the second row is the first and last name of the person, next is 5 x-coordinates, and finally 5 y-coordinates. This is then repeated for however many people there are. I need to create a code that sorts the first names into one array and outputs them to a file, last names to a different array and outputs to a file, x coordinates to a different array and outputs to a file and finally y coordinates to a different array and outputs to a file. I thought my code looked pretty good and it compiles, but whenever I run it, it crashes. Any suggestions? Or techniques that could be helpful? Very much appreciated!

*Note: I am starting off easy, so this code just has the full names being put into one array, but if anyone can advise on how to separate them would be helpful!




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
#include <iostream>  /* Allows ouputes to the screen */
#include <fstream>  /* Allows files to be input and ouput */
#include <string>   /* Allows file strings to be used */
#include <iomanip>

 using namespace std; /* Allows certain parts of the code to be written without the prefix stating it is from the standard library */

 int main()  /* This signals the start of the main function */
 {
     
     ifstream facedata; 
     ofstream names, xcoor, ycoor;
     int a;
     double xcor[a][5], ycor[a][5];
     string name[a];

	facedata.open("facedata.txt");
	names.open("names.txt");
	xcoor.open("xcoor.txt");
	ycoor.open("ycoor.txt");


	facedata >> a; 
  		

     for (int c=0; c<a; c++)
     {
         
         for (int b=1; b<=2; b++)
         {
         names << getline(facedata, name[c]);
         
         }
         
         
         for (int w=1; w<=5; w++)
         {
             facedata >> xcor[c][w]; 
             xcoor << xcor[c][w];
         }


         for (int p=1; p<=5; p++)
         {
             facedata >> ycor[c][p];
             ycoor << ycor[c][p];
         }

}






     facedata.close();
     names.close();;
     xcoor.close();
     ycoor.close();


     system("pause");
     return(0);
 }  





Text file:

25
Alexis Flores
2.41 4.05 3.32 1.52 0.74
3.51 3.05 4.02 4.03 3.05
Jason Brown
2.41 3.45 2.72 2.12 1.34
3.55 3.25 3.82 3.83 3.25
William Bishop
2.41 3.49 2.76 2.08 1.30
3.55 3.23 3.84 3.85 3.23
Glen Godwin
2.41 3.53 2.80 2.04 1.26
3.55 3.21 3.86 3.87 3.21
John Rigas
2.41 3.57 2.84 2.00 1.22
3.55 3.19 3.88 3.89 3.19
Semion Mogilevich
2.41 3.61 2.88 1.96 1.18
3.55 3.17 3.90 3.91 3.17
Eduardo Ravelo
2.41 3.65 2.92 1.92 1.14
3.55 3.15 3.92 3.93 3.15
Rita Crundwell
2.41 3.69 2.96 1.88 1.10
3.55 3.13 3.94 3.95 3.13
Yaser Said
2.41 3.73 3.00 1.84 1.06
3.55 3.11 3.96 3.97 3.11
Sun Kailiang
2.41 3.77 3.04 1.80 1.02
3.55 3.09 3.98 3.99 3.09
Alexsey Belan
2.41 3.81 3.08 1.76 0.98
3.55 3.07 4.00 4.01 3.07
Rebecca Adams
2.41 3.85 3.12 1.72 0.94
3.55 3.05 4.02 4.03 3.05
Evgeniy Bogachev
2.41 3.89 3.16 1.68 0.90
3.55 3.03 4.04 4.05 3.03
Lea Fastow
2.41 3.93 3.20 1.64 0.86
3.55 3.01 4.06 4.07 3.01
Victor Wolf
2.41 3.97 3.24 1.60 0.82
3.55 2.99 4.08 4.09 2.99
Julieanne Dimitrion
2.41 4.01 3.28 1.56 0.78
3.55 2.97 4.10 4.11 2.97
Aviv Mizrahi
2.41 4.05 3.32 1.52 0.74
3.55 2.95 4.12 4.13 2.95
Boniface Ozoemena
2.41 4.09 3.36 1.48 0.70
3.55 2.93 4.14 4.15 2.93
Patricia Saa
2.41 4.13 3.40 1.44 0.66
3.55 2.91 4.16 4.17 2.91
Amy Gilligan
2.41 4.17 3.44 1.40 0.62
3.55 2.89 4.18 4.19 2.89
Victor Gerena
2.41 4.21 3.48 1.36 0.58
3.55 2.87 4.20 4.21 2.87
Harriette Walters
2.41 4.25 3.52 1.32 0.54
3.55 2.85 4.22 4.23 2.85
Nicolae Popescu
2.41 4.29 3.56 1.28 0.50
3.55 2.83 4.24 4.25 2.83
Viet Nguyen
2.41 4.33 3.60 1.24 0.46
3.55 2.81 4.26 4.27 2.81
Robert Fisher
2.41 4.37 3.64 1.20 0.42
3.55 2.79 4.28 4.29 2.79
Think about line 13 of your code and the 2 following lines after it. At that point, what is the value of the integer 'a'? It has no value. Therefore, you would need to put what is currently on line 23 just before where you declare those 3 variables 'xcor', 'ycor' and 'name'. Also, in most C++ compilers, lines 14 and 15 wouldn't even compile because using a variable as a bound of an array without dynamically allocating it is illegal; but you are using a compiler that allows this "extension" so it allows you to do so, but my advice is not to worry too much about it now and focus on the rest of the code first.

The answer for this question explains a bit more about a variable sized array: http://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number
Last edited on
Topic archived. No new replies allowed.