HELP, IM STUCK!

closed account (y6f36Up4)
Can anyone tell me why my program is outputting an extra line at the end of my file? Also I am not sure how to sort this data alphabetically and keep the phone number with the correct name, cany anyone help me?


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;


int main()
{
ifstream fin("phoneData.txt");
if (!fin){
cout<<"Error opening file...Shutting down!\n";
return 0;
}
const int SIZE=50;
int count=0;
string phoneNumber[SIZE];
string firstName[SIZE];
string lastName[SIZE];


while (count<SIZE && getline(fin,phoneNumber[count])){

getline(fin,firstName[count]);
getline(fin,lastName[count]);

count++;

}
string wholeName[SIZE];
for(int i=0; i<count;i++){
wholeName[i]=lastName[i]+", "+firstName[i];
cout<<phoneNumber[i]<<" "<<wholeName[i]<<endl<<endl;
}
cout<<endl;
return 0;

}
In the line right before the return 0, you are outputting a newline.

To sort alphabetically, you should create a data structure to hold the phone number, first name, and last name. Then, create an array of those data structures and fill it with data. Sort the array by name, and output the data.

In the future, please, please indent your code.
Topic archived. No new replies allowed.