Help me make this look more appealing

When I output this code it is always all over the place. How do i make it look like:

Joe Garcia 858-343-2009
Amelia Perez 617-255-0987
Bob Haynes 858-765-1122
Tim Ducrest 760-877-1654
Kevin Garcia 760-543-5622
Suzy Walter 858-190-2307
Fang Yi 619-677-1212
Robert James 619-909-3476
Mary Palmer 760-435-2086

For the Display function i had to put v.size()-1 because it outputted an extra phone number. But I feel like it's wrong and I wasnt supposed to do that?

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
 #include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

void Display(vector<string> v);

int main()
{
  string fileName, count;
  vector <string> v;
  ifstream inputFile;

  cout << "Enter the name of the file: ";
  cin >> fileName;

  inputFile.open(fileName.c_str());

  if (!inputFile)
    {
      cout << "Error opening the file!"<< endl;
    }
  else
    {
      while (inputFile)
        {
          inputFile>>count;
          v.push_back(count);
        }
      inputFile.close();
    }
  Display(v);
  return 0;
}
void Display(vector<string> v)
{
  for(int i=0; i<v.size()-1; i++)
    {
      cout << v[i] << " ";
    }
  cout << endl;
}

Hello sesslit805,

gunnerfunner has a nice solution to your problem http://www.cplusplus.com/forum/beginner/208524/#msg982358

A quick fix for what you have might be to add this line after line 40 in the display function:

if ((i + 1) % 3 == 0) std::cout << std::endl;.

At least that will put name and phone number on one line.

Hope that helps,

Andy

Edit: if you compile to the C++11 standard or later you do not need the ".cstr()" on line 18.
Last edited on
Andy - how do you get the link to take you to a particular message within a thread, for e.g #msg982358 above. Quite useful
Hello gunnerfunner,

Look for the button to the left of the date in the upper right corner of the message box and right click on it. Choose the "Copy Link Location" in the drop down list and then paste it where you want.

Kind of found that by accident.

Andy

P.S. be sure to leave a space before and after the link.
Last edited on
Handy Andy - you've lived up to your name, many thanks
Thank you that did help!!!
Topic archived. No new replies allowed.