Dec 10, 2010 at 3:16am UTC
I am supposed to write a code that brings in phone numbers and names from a file and sorts them in alphabetical order by last name and adds hyphens to the numbers. For some reason that I can't figure out, I am getting a couple of errors that I can't figure out that have to do with the addHyphens function and the switchFunction function (header or prototype I think). If you could figure this out that would be great. Thanks.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE=50;
void addHyphens(string);
void sortData(string[], string[], int);
void switchFunction(string, string);
int main () {
ifstream fin;
fin.open("phoneData.txt");
if(fin.fail()){
cout << "Error opening file.\nShutting down." << endl;
return 0;
}
string phoneNumbers[SIZE];
string firstName[SIZE];
string lastName[SIZE];
string fullName[SIZE];
int size, count;
for(int size=0; size<SIZE && !fin.eof(); size++){
fin >> phoneNumbers[size];
fin.ignore();
getline(fin, firstName[size], '\n');
getline(fin, lastName[size], '\n');
fullName[count]=lastName[size]+", "+firstName[size];
}
sortData(fullName, phoneNumbers, size);
for(count=0; count<size-1; count++){
addHyphens(phoneNumbers[count]);
cout << left << setw(24) << fullName[count] << "\t" << right << setw(12) << phoneNumbers[count] << endl;
}
return 0;
}
void addHyphens(string &phoneNumbers){
if(phoneNumbers.length()==10){
phoneNumbers.insert(6, 1, '-');
}
phoneNumbers.insert(3, 1, '-');
}
void sortData(string fullName[], string phoneNumbers[], int size){
int minIndex, count;
string minValue;
for (count=0; count<(size-1); count++) {
minIndex=count;
minValue=fullName[count];
for (int index=count+1; index<(size-1); index++) {
if(fullName[index]<minValue){
minValue=fullName[index];
minIndex=index;
}
}
switchFunction(fullName[count], fullName[minIndex]);
switchFunction(phoneNumbers[count], phoneNumbers[minIndex]);
}
}
void switchFunction(string &switchVariable1, string &switchVariable2){
string placeholder;
placeholder=switchVariable1;
switchVariable1=switchVariable2;
switchVariable2=placeholder;
}