output is not displaying correctly because of spaces in text file

I am having trouble outputting the data of my text file correctly. There are a few street addresses that have spaces in the street name, therefore it's causing the output to get all jumbled up.

Here is a look at two lines that are in my text file:
Note: Whenever I post this question... it doesn't do the text file justice on how well the text in it is evenly spread out.

Bob D Builder 123-456-7890 imacartoon@gmail.com 1234 N Wave Street, Atlanta, GA
Hump D Dumpty 098-654-3210 imanegg@gmail.com 300 Always St, Buckeye, AZ

Here is a look at my code that I currently have:


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;


int main()
{
ifstream file("ClientList.txt");
if(!file)
{
cout << "Error opening file." << endl;
}

string firstname;
string middleinitial;
string lastname;
string phone;
string email;
string address;
string city;
string state;

while(file >> firstname >> middleinitial >> lastname >> phone >> email >> address >> city >> state)
{
cout << firstname << " ";
cout << middleinitial << " ";
cout << lastname << " ";
cout << phone << " ";
cout << email << " ";
cout << address << " ";
cout << city << " ";
cout << state << endl;
}

return 0;
}
Last edited on
First, use code tags when posting code.
http://www.cplusplus.com/articles/jEywvCM9/

Similarly, use output tags [output][/output] to show your text file.

Bob D Builder 123-456-7890 imacartoon@gmail.com 1234 N Wave Street, Atlanta, GA
Hump D Dumpty 098-654-3210 imanegg@gmail.com 300 Always St, Buckeye, AZ


You can use >> just fine up to the email, it falls apart when trying to tell the difference between "1234 N Wave Street" (which has 4 words) and "300 Always St" (which has 3).

Fortunately, there is a comma there to tell you where each ends, and we can use getline with our choice of delimiter.
https://www.cplusplus.com/reference/string/basic_string/getline/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  while ( ( file >> firstname >> middleinitial >> lastname >> phone >> email )
          && getline(file,address,',')
          && getline(file,city,',')
          && ( file >> state ) ) {
    cout << firstname << " ";
    cout << middleinitial << " ";
    cout << lastname << " ";
    cout << phone << " ";
    cout << email << " ";
    cout << address << " ";
    cout << city << " ";
    cout << state << endl;
  }

Topic archived. No new replies allowed.