File writing code not working properly

Okay, so my issue is I have most of the following code written, but it isn't working as I need it to. I am supposed to check a list of states in a separate file for city and state_id, print those items to another file, then check to make sure that the next state_id on the list is NOT the same as the one that was just checked. In the end, I should have 52 entries on the second file.

Here is the code:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


  //Variables.
  string line; 
  string city;
  string state_id;
  string state_name;
  string county_name;
  string temp = "ZZ";

  int county_fips;
  int county_fips_all;
  int lat;
  int lng;
  int population;
  int density;
int main () 
{ 

  
fstream myfile, file2; 

//This opens both files
myfile.open("cities.txt"); 
file2.open("CityOut.txt");

{while (!myfile.eof()) 
getline(myfile, city, ',')&& getline(myfile,state_id,',');
getline(myfile, line);


if (state_id!=temp)

file2<< city ;
file2<<",";
file2<< state_id ;
file2<<line;
file2<< endl;
state_id=temp;




}
myfile.close();
file2.close();

return 0;
}


What do I need to do to make this work? ty.
Last edited on
Well start with some decent indentation.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


  //Variables.
string line;
string city;
string state_id;
string state_name;
string county_name;
string temp = "ZZ";

int county_fips;
int county_fips_all;
int lat;
int lng;
int population;
int density;

int main()
{
  fstream myfile, file2;

  //This opens both files
  myfile.open("cities.txt");
  file2.open("CityOut.txt");

  {
    while (!myfile.eof())
      getline(myfile, city, ',') && getline(myfile, state_id, ',');
    getline(myfile, line);


    if (state_id != temp)
      file2 << city;

    file2 << ",";
    file2 << state_id;
    file2 << line;
    file2 << endl;
    state_id = temp;
  }

  myfile.close();
  file2.close();

  return 0;
}

If you're using fstream directly, you need to be specific about opening for input or output.
Use ifstream and ofstream to implicitly specify the direction of information flow.

Neither your while nor if statements have braces, so whatever they're doing, it only applies to one statement.

To make sense of what you're trying to do, a sample input file would go a long way to helping you on the right path.


Hi Salem, thank you so much for responding. =)
Okay, I will have to give you a small sample. the original "cities.txt" file is massive.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
city,state_id,state_name,county_fips,county_name,county_fips_all,lat,lng,population,density
South Creek,WA,Washington,53053,Pierce,53053,46.9994,-122.3921,2500,125
Roslyn,WA,Washington,53037,Kittitas,53037,47.2507,-121.0989,947,84
Sprague,WA,Washington,53043,Lincoln,53043,47.3048,-117.9713,441,163
Gig Harbor,WA,Washington,53053,Pierce,53053,47.3352,-122.5968,9507,622
Lake Cassidy,WA,Washington,53061,Snohomish,53061,48.0639,-122.0920,3591,131
Tenino,WA,Washington,53067,Thurston,53067,46.8537,-122.8607,1830,491
Jamestown,WA,Washington,53009,Clallam,53009,48.1229,-123.0911,289,191
Three Lakes,WA,Washington,53061,Snohomish,53061,47.9420,-121.9924,3390,112
Curlew Lake,WA,Washington,53019,Ferry,53019,48.7311,-118.6663,573,50
Chain Lake,WA,Washington,53061,Snohomish,53061,47.9038,-121.9861,4280,156
Pateros,WA,Washington,53047,Okanogan,53047,48.0536,-119.8994,701,536
Rosburg,WA,Washington,53069,Wahkiakum,53069,46.3076,-123.6344,385,6
Parkland,WA,Washington,53053,Pierce,53053,47.1417,-122.4376,37019,1653
Birch Bay,WA,Washington,53073,Whatcom,53073,48.9230,-122.7543,8642,208


Additionally, and I am sorry for this, but I am not certain where to place the ifstream or ofstream. I am pretty basic at this, and this covid thing really messed up my ability to glean anything useful from my instructor. As I said, I am trying to read the city and state-id, then the rest of the line, copy (only) the data found under those two entries into a second file, then return to the top of the loop with the state-id as a new temp. This is so that the output file will only have one city and state-id per state. Make sense?
Last edited on
When you're stuck / confused - simplify!

Start with something that just copies the file
1
2
3
4
5
6
ifstream in("cities.txt");
ofstream out("out.txt");
string line;
while ( getline(in,line) ) {
  out << line;
}


You can then start to adapt in an incremental fashion.
1
2
3
4
5
string line,city;
while ( getline(in,city,',') ) {
  getline(in,line);
  out << city << ":" << line;
}


When you can reliably identify your city and state, then you can start recording previous values, making comparisons, adding decisions etc.
Well, thank you for your help.
Topic archived. No new replies allowed.