Hey there, I'm currently writing code for a small "Mock internet dating service", Everything work's perfectly for reading in the names from file, except that I keep getting the last name in the file twice.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//FUNCTION PROTOTYPES
void printHeader();
int main()
{
struct Member//Database Member structure. Stores name and the scores as an array.
{
string name;
int scores[10];
} male[15], female[15];
char ch;
int maleCount = 0, femaleCount = 0;
printHeader();//Print header
ifstream inputFile;//Loads file into code
inputFile.open("client.txt");
while(inputFile)
{
inputFile.get(ch);
if(ch == 'M')
{
cout << ch << " ";
for(int count = 0; count < 10; count++)
{
inputFile >> male[maleCount].scores[count];
cout << male[maleCount].scores[count] << ' ';
}
getline(inputFile,male[maleCount].name);
cout << " ";
cout << male[maleCount].name;
}
else if(ch == 'F')
{
cout << ch << " ";
for(int count = 0; count < 10; count++)
{
inputFile >> female[femaleCount].scores[count];
cout << female[femaleCount].scores[count] << ' ';
}
getline(inputFile,female[femaleCount].name);
cout << " ";
cout << female[femaleCount].name;
}
else
{
cout << "\t\tERROR IN FILE!!!";
inputFile.ignore(200,'\n');
}
cout << endl;
}
inputFile.clear();//Clear file buffer
return 0;
}
void printHeader()
{
cout << "SUPER AWESOME INTERNET DATING SERVICE!\n\n";
cout << "SEX ANSWERS NAME\n";
cout << "----------------------------------------------------\n";
}
|
Here's kinda what client.txt looks like:
M 1 2 3 4 5 5 4 3 2 1 Thom
F 1 1 2 2 3 3 4 4 5 5 Sally
M 2 4 1 3 5 4 2 3 1 1 Alfred
M 1 1 1 2 2 2 3 3 3 4 James
F 2 3 4 5 4 3 2 1 3 2 Veronica
M 1 1 1 1 1 1 1 1 1 1 Moe
Any tips to prevent reading the last line twice?