Strings and Sorting

I am trying to write code for a program that reads from a text file that has 50 phone numbers and names. The format is number, first name(s), last name.
ex)
2942893
Bill Scott
Jones
3823920578
Mark Thomas
Sorensen
2931093489
John
Hansen

I am supposed to store the phone number and full name each in their own string (no using c-strings). I am then supposed to print the information to the screen in the following format: last name (in alphabetical order using selectionSort), first name(s), phone number (with hyphens).
ex)
Hansen, John 293-109-3489
Jones, Bill Scott 294-2893
Sorensen, Mark Thomas 382-392-0578

I have successfully opened the file but am confused on how to do the rest of the code. I know it is supposed to be simple, but I a missed the last couple of days of class because I am sick so I have no idea what I am doing.
Thanks for your help.
std::getline to read a string.
store them in std::vector. It would be good if you wrote a struct to hold the three strings.
cout << str to print a string.
I'll leave the dashes for you to figure out yourself..

reference:
http://www.cplusplus.com/doc/tutorial/files/
http://www.cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/stl/vector/
We are only allowed to use code that we learned in class, and we haven't talked about vectors or structs. So I can't use those. If I named two strings (string name; and string phoneNumber;) would I use getline to read both the number and name in? And how do I read in both lines of the name?

Thanks
This is what I have so far. But when I try to print the numbers to the screen, it is just printing out every other line. I need the name string to have the second and third lines while the phoneNumber string has all of the numbers.

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

using namespace std;

const int ROWS=93;

int main () {
ifstream fin;
fin.open("phoneData.txt");
if(fin.fail()){
cout << "Error opening file.\nShutting down." << endl;
return 0;
}

string phoneNumbers[ROWS];
string name[ROWS];

for(int index=0; index<ROWS; index++){
getline(fin, phoneNumbers[index]);
fin.ignore();
getline(fin, name[index]);
}

for (int i=0; i<ROWS; i++) {
cout << phoneNumbers[i] << endl;
}


return 0;
}
one entry takes 3 lines, so you need to have 3 arrays and 3 getline()s.
by the way, getline() removes then '\n' from the stream, so you don't need to ingore() anything.
My teacher told me that I have to save the full name in one string and the phone number in another.
I figured some out. Now I just need to know how to alphabetize by the last name and put hyphens in the phone numbers.

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

using namespace std;

const int ROWS=50;

void bubbleSort(int[], int);
int main () {
ifstream fin;
fin.open("phoneData.txt");
if(fin.fail()){
cout << "Error opening file.\nShutting down." << endl;
return 0;
}

string phoneNumbers[ROWS];
string firstName[ROWS];
string lastName[ROWS];
string fullName[ROWS];

for(int index=0; index<ROWS; index++){
fin >> phoneNumbers[index];
fin.ignore();
getline(fin, firstName[index], '\n');
getline(fin, lastName[index], '\n');

fullName[index]=lastName[index]+", "+firstName[index];
}

bubbleSort(fullName, ROWS);

for (int i=0; i<ROWS; i++) {
cout << setw(24) << fullName[i] << right << setw(12) << phoneNumbers[i] << endl;
}

return 0;
}

void bubbleSort(int fullName[], int ROWS){
bool didSwap=true;
for (int pass=1; pass<ROWS && didSwap; pass++) {
didSwap=false;
for (int index=0; index<ROWS-pass; index++) {
if (fullName[index] > fullName[index+1]){
didSwap=true;
mySwap (fullName[index] , fullName[index+1]);
}
}
}
}
Topic archived. No new replies allowed.