I need some real serious help. I have been trying to get this program to work for the last past week and everytime I get one part to work and try to complet the next section the previous section craps out. I have to write a program that reads from a txt file the sex of the client then 10 intergers that represent answer then the clients name. Some of the clients have first and last names others don't. After the program reads through the file it prints out the number of Female and Male. Next it will match the clients based on the totals from the answers given. If there are any duplicate client names it will not reprint them or count them in the number of clients again. Here is what I have so far, I wasn't sure which version to place up but this is the one that I am currently trying to get to work.

#include <iostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std;

int main()
{
  int i = 0;
  int r = 0;
  char sex[3];
  int answer = 0;
  int a[11];
  int max_read = 40;
  int amountRead = 0;
  int inFileloop = 0;
  char firstname[41];
  char lastname[41];

cout << " Sex        Answers                  Name      " << endl;
cout << " ---     --------------        --------------- " << endl; 
  //string line;
  std::ifstream in("clients.txt",std::ios::in |std::ios::binary); 

if(!in)
{

std::cout<<"Could not open file"<<std::endl;

while (in)
for(i = 0; i < sex[3]; i++) //clear array
sex[i] = 0;

}

//in.read((char *) &sex, sizeof sex ); //read block of data
in >> sex[i] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7] >> a[8] >> a[9] >> a[10] >> firstname >> lastname; 
cout << "  " << sex[i] << "        " << a[1] << a[2] << a[3] << a[4] << a[5];
cout << a[6] << a[7] << a[8] << a[9] << a[10] << "           "  << firstname << " " << lastname <<endl;
cout << answer << endl;

// female array

in >> sex[i] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7] >> a[8] >> a[9] >> a[10] >> firstname >> lastname; 
cout << "  " << sex[i] << "        " << a[1] << a[2] << a[3] << a[4] << a[5];
cout << a[6] << a[7] << a[8] << a[9] << a[10] << "           "  << firstname << " " << lastname <<endl;
cout <<answer;
inFileloop++;  
// calculations


// prints out the most compatible dates for the female clients
cout << " <<< Most Compatible Dates For Female Clients >>> "<< endl << endl << endl;
cout << " Client Name             Most Compatible Date " << endl;
cout << " ------------------                     -------------------- " << endl << endl << endl;


// prints out the most compatible dates for the male clients
cout << " <<< Most Compatible Dates For Males Clients >>> " << endl << endl << endl;
cout <<endl << " Client Name                Most Compatible Date " << endl;
cout << " ------------------                     -------------------- " << endl;
in.close();

  return 0;  }

Please post a line or two from the data file.

This is a sample of the text file.

M 1234554321 Henry Kissenger
F 2543122451 Janet Jackson
F 5432112345 Madonna
M 1542213452 Tom Cruez

OK, according to your problem description I have a few comments to help...

But FIRST, please take the time to be careful about how you "format" your code. You've made some indentation errors (that is, because of the way you have indented your code you have made some errors!)

text file
OK, if the input file is a text file, why are you opening it in binary mode? Don't. std::ifstream in( "clients.txt" ); That's all you need.

Clearing the array should not be dependent on the file.

input
You cannot force input. According to the problem description, your input is guaranteed only to have the following form:

M|F 1 2 3 4 5 6 7 8 9 10 name ...

That is, the letter 'M' or 'F', followed by ten numbers, followed by one or more names. I would suggest that you don't need to care whether the client has both a first and last name, but you can always separate them later.

This input has a nice characteristic: all the fields at the beginning of the line are definite, while the last field is a free-form string. This means you can mix >> and getline() with impunity.

So input for each line (or client) might look something like this (needs more work):

// get the letter 'M' or 'F'.
std::cin >> sex;
// get the answers
for (i = 0; i < 10; i++) std::cin >> a[ i ];
// skip whitespace before name
std::cin >> std::ws;
// get the name
getline( cin, name );

data types
Finally, consider the types of things you are working with. Don't use char[] for strings, use the std::string class for strings. (Makes life easier.)
The "sex" variable should only be one character, since that is all you need from the file.

Well, that should be enough to get you working. Don't forget to watch your indentation and fix your while loop.

commented: great post! +6

I tried it with the corrections you suggested and know the program won't read the file or at least it won't cout any of the information.

I tried it with the corrections you suggested and know the program won't read the file or at least it won't cout any of the information.

Post code please -- I live only about 30 miles from you but my eyesight isn't that good :)

line 33: sex[3] does not exist. That array has elements numbered 0, 1 and 2. There is no element 3. And that loop isn't necessary anyway. The array can be initialized to 0 when declared int array[3] = {0}; . Do the same with all the other arrays.

line 32: why? it has no purpose, so just delete it.

If I try to cin the firstname i get an error and also I can't get the answer to print all 10.

#include <iostream>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <iomanip>
using namespace std;
 
int main()
{
  int i = 0;
  int r = 0;
  char sex[1];
  int answer = 0;
  int a[10];
  int max_read = 40;
  int amountRead = 0;
  int inFileloop = 0;
  char firstname[41];
  char lastname[41];

cout << " Sex        Answers                  Name      " << endl;
cout << " ---     --------------        --------------- " << endl; 
  //string line;
  std::ifstream in("clients.txt"); 

if(!in)
{
 
std::cout<<"Could not open file"<<std::endl;

while (in)
for(i = 0; i < 10; i++) //clear array
a[i] = 0;
sex[i]=0;

}

//in.read((char *) &sex, sizeof sex ); //read block of data
//cin.getline( firstname);
in >> sex[i] >> a[i]; 
cout << " " << sex[i] << "       " << a[i] << endl;
//cout << firstname;
//std::cin >> std:: ws;
  
// calculations


// prints out the most compatible dates for the female clients
cout << " <<< Most Compatible Dates For Female Clients >>> "<< endl << endl << endl;
cout << " Client Name				Most Compatible Date " << endl;
cout << " ------------------                     -------------------- " << endl << endl << endl;


// prints out the most compatible dates for the male clients
cout << " <<< Most Compatible Dates For Males Clients >>> " << endl << endl << endl;
cout <<endl << " Client Name				Most Compatible Date " << endl;
cout << " ------------------                     -------------------- " << endl;
in.close();

  return 0;  

}

Try this program. It should print input from file to screen.

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

using namespace std;

int main()
{
  char gender;
  int answers[10];
  string name;
  int i;

  ifstream fin("clients.txt")

  if(!fin)
  {
     cout << "unable to open file" << endl;
     exit(1);
  }
 
  while(fin >> gender)  //if you can get a gender
  {
    //then get the answers
    for (i = 0; i < 10; i++) 
      fin >> a[ i ];

    // skip whitespace before name
    fin >> std::ws;

    // get the name
    getline(fin, name);

    //display information read from file to screen
    cout << gender;

    for(i = 0; i < 10; ++i)
      cout << answers[i] << ' ';

    cout << name;
  }
  fin.close();
  cin.get();
  return 0;
}

Now, if that works, make sure you compare it to your code to see where the errors are. Then you'll need to keep track of how many clients of each gender there are, total the scores for each client, and store the name and total score in separate containers for each client by gender.

If you know the maximum number of clients in the file by gender at compile time, then you could use sstatic arrays. If not, vectors or lists would probably be your best choices. The alternative would be to read the file once to determine the actual number of male vs female clients, declare arrays dynamically to hold the information, then reread the file populating the arrays. You'd need to clear the ifstream object and reset it to the front of the file between reads, of course.

Assuming the maximum number of clients in either gender is 2, and it's known at compile time, then you could do something like this for the variables needed.

int numMales = 0;
int numFemales = 0;
int totalScore;
string maleNames[2];
string femaleNames[2];
int maleScores[2];
int femaleScores[2];

Then you'll need to be able to determine best match. Presumably equal values are best, but what if there isn't an equal match between the genders? In any event the index of the best match by number can be used to get the best match by name.

Note: if you know about user defined structs/classes, then you wouldn't need the parallel arrays to hold names and scores. I've assumed you haven't been taught about that topic yet, though.

If you want to eliminate the lines where the name is duplicated then you will need to read the file into an array so that the program can check if the name has already been read. In c++ the vector class is the most convenient way to create the array. Below is one way to do it

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

struct student
{
    string sex;
    string grades;
    string name;
};


int main()
{
    vector<student> studentList;
    student stdnt;
    cout << " Sex        Answers                  Name      " << endl;
    cout << " ---     --------------        --------------- " << endl; 
    //string line;
    std::ifstream in("TextFile1.txt"); 

    if(!in)
    {
        std::cout<<"Could not open file\n\n";
        return 1;
    }
    // read the data file into an array of structures
    while( in >> stdnt.sex >> stdnt.grades )
    {
        getline(in, stdnt.name);
        vector<student>::iterator it = studentList.begin();
        bool found = false;
        // search the array for this student
        for( ; it != studentList.end(); it++)
        {
            if( it->name == stdnt.name)
            {
                found = true;
                break;
            }
        }
        // if not int the array, then add it
        if(found == false)
            studentList.push_back(stdnt);
    }
    // all rows in the file are now read, so just output the data
    vector<student>::iterator it = studentList.begin();
    for( ; it != studentList.end(); it++)
    {
        cout.setf(ios::left);
        cout << setw(10) << "  " + it->sex << setw(20) << it->grades << setw(15) << it->name << "\n";
    }

  return 0;  
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.