C++ reading data into array of structures -


I have found a way to read data in my array strokes but I can not control how much it reads. It will teach all the 14 entries in the input file but after that 14 will be read, it continues for some lines and then closes, however, if I keep the code to read the data inside any loop, So much of the printing starts in place of this file instead of joining it. Does anyone know why this happens? There is no way around it

The input file looks as follows (skip the dot): The file has 14 sets of data. So, as shown below, 1 set will include Georgia 76.4 for 10, then North Carolina 95.5, 20 and 14 sets.

The data in the input file looks like this:

  Georgia 76.4 10 North Carolina 95.5 20 and so on.   

The data needs to be printed on the screen like this:

  Georgia 76.4 10 North Carolina 95.5 20 etc.   

The problem is that in my code I was trying and failed to read in the input file.

  #include & lt; Iostream & gt; # Include & lt; Fstream & gt; # Include & lt; Cstdlib & gt; Struct ATDATA {string status; Double mile; Intestinal shelters; }; Int readData (ifstream & input, struct ataxa data []); Int main () {ifstream input; Four file names [256]; Atadata data [14]; Int i; Cout & lt; & Lt; "Enter input filename:"; Cin & gt; & Gt; file name; Input.open (filename); If (input.fail ()) {cout & lt; & Lt; "Input file does not exist." & Lt; & Lt; Endl; Exit (1); } ReadData (input, data); Cout & lt; & Lt; I & lt; & Lt; Endl; Return (0); } Int readData (ifstream and input, struct atadadata data []) {int i, j; Gateline (input, data [i] .state, '\ t'); Input.ignore (14, '\ n'); Input & gt; & Gt; Data [i] .miles; Input.ignore (1, ''); Input & gt; & Gt; Data [i] .shelters; For (J = 0; J & lt; 14; J ++) {cout & lt; & Lt; Data [ja] .state & lt; & Lt; Data [j] Miles & lt; & Lt; Data [j] Shelter & lt; & Lt; Endl; }}   

For all the information, I apologize, as clarified as can be seen, as it is not looking more silly, thanks is a good day.

The best way to get a full line is std :: getline . However, if you try to mix incompatible input with formatted input, then it becomes a bit difficult. You need to ignore many calls and other puzzling I think it is always clear to read the entire line at a time, and then a string stream to parse that line Use

Try it:

  size_t readData (ifstream & input, structure atatata data [size], size_t max_items) {size_t i; (I = 0; i & lt; max_items;) {ATDATA item; Meet (input, item.state); String line_2; Break if (Getline (Input, Line_2)); Issuingstream release (line_2); If (continue (! (<(>> <> Item.miles> item.shelters)} {// leave a malformed entry (or you can cancel instead); } Data [i ++] = item; } Came back; }   

Call it on:

  size_t num_items = readData (data, size, data / size data [0]);    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -