reading line by line from txt file

I am trying to read data from a .txt doc line by line with delimiters. we also have to populate a vector with the info. I have tried everything I know to do, but no matter what I do it says "ERROR: Cannot open file". I saved the info into a new txt file and now my output is showing only the zipcodes of the people.Can someone please help me? I have been trying to do this for 3 days. all help is appreciated. here is my code.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

class C_People
{
public:
string firstName;
string lastName;
string address;
string city;
string state;
string zipcode;
};

int main()
{
C_People person;

string input;

fstream dataFile("LabtwoNames.txt", ios::in);

if (dataFile)
{
getline(dataFile, input, '$');

while (dataFile)
{
cout << input << endl;

getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');
getline(dataFile, input, '$');

}

dataFile.close();
}
else
{
cout << "ERROR: Cannot open file.\n";
}
system("Pause");
return 0;
}

and here is my txt file:
Start File
$Tommy$Gunn$22 Winchester Drive$Calamar$AL$38880
$Bobby$Smither$117 Drover Drive$Budville$NC$28711
$William$Hammet$45 East West Street$Springfield$NC$28881
$Shenna$North$10 Tenth Street$Canopener$AL$31110
$Robert$Stillskin$300 Square Circle$Picklestown$NC$27887
$Walter$Mellon$99 Main Street$Blueville$AL$33990
$
Insert everywhere the statement cout << input << endl; and you will see all data


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
int main()
{
   C_People person;

   string input;

   fstream dataFile("LabtwoNames.txt", ios::in);

   if ( !dataFile )
   {
      cout << "ERROR: Cannot open file.\n";
   }
   else
   {
      while (dataFile)
      {
         getline(dataFile, input, '$');
         getline(dataFile, input, '$');
         cout << input << endl;
         getline(dataFile, input, '$');
         cout << input << endl;
         getline(dataFile, input, '$');
         cout << input << endl;
         getline(dataFile, input, '$');
         cout << input << endl;
         getline(dataFile, input, '$');
         cout << input << endl;
         getline(dataFile, input, '$');
         cout << input << endl;
      }
   }

   system("Pause");
   return 0;
} 
Last edited on
THANK YOU! I was confused there. How about if I want to get the first/last name on same line and city/state/zip on same line? so instead of it giving me 6 lines per address, only 3? and I won't even lie, populating a vector, no idea how to do that.
I do not see where these 6 lines per address?!
When I compile this is what I get:

Tommy
Gunn
22 Winchester Drive
Calamar
AL
38880

etc... with the rest of the addresses.
Remove endl and all will be in the same line. What is the problem?!
instead of

Tommy
Gunn
22 Winchester Drive
Calamar
AL
38880

I want

Tommy Gunn
22 Winchester Drive
Calamar AL 38880
Do as you want. What is the problem?!
the problem is I do not know exactly how to do it that way(3 lines instead of 6). everything I try is not going right.
Do it as you are already doing that is use endl to control the number of output lines.
Topic archived. No new replies allowed.