JEAN ELIE NYANGUILE
JEAN ELIE NYANGUILE

Reputation: 11

How to match data in the same file using C++

The program doesn't print the report in columns formatted exactly as shown in the sample output. Can anyone help?

enter image description here

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

int main()
{
    char user_gender, user_smoker;
    string user_eyecolor;
    int user_minAge, user_maxAge, user_minHeight, user_maxHeight;

    cout << "What is the gender of your ideal match(M, F, N) ? ";
    cin >> user_gender;

    cout << "What is the minimum age? ";
    cin >> user_minAge;

    cout << "What is the maximum age? ";
    cin >> user_maxAge;

    cout << "What is the minimum height (in inches)? ";
    cin >> user_minHeight;

    cout << "What is the maximum height (in inches)? ";
    cin >> user_maxHeight;

    cout << "Smoker (Y/N)? ";
    cin >> user_smoker;

    cout << "What is the eyecolor (Blue, Green, Grey, Brown)? ";
    cin >> user_eyecolor;

    cout << endl << endl;
    //Variables to check against the conditions
    int countGender = 0;
    int partialMatch = 0;
    int fullMatch = 0;

    cout << endl << left << setw(1) << "  Name" << fixed << right << setw(22) << "Age" << fixed << right << setw(12) << "Height" << fixed << right << setw(12) << "Smoker" << fixed << right << setw(15) << "Eye Color" << fixed << right << setw(22) << "Phone" << endl;
    cout << "-----------------------------------------------------------------------------------------------------------------" << endl;


    //Now read the file data.
    ifstream fin("matches.txt");

    if (fin.is_open())
    {
        while (!fin.eof())
        {
            string firstname, lastname, eyecolor, phoneno;
            char gender, smoker;
            int age, height;
            fin >> firstname >> lastname >> gender >> age >> height >> smoker >> eyecolor >> phoneno;

            if (gender == user_gender)
            {
                countGender++;

                //Now check to see if the age and height are between the maximum and minum preferences. 
                if ((age >= user_minAge && age <= user_maxAge) && (height >= user_minHeight && height <= user_maxHeight))
                {

                    //Then check to see if the smoking preference and eye color are also a match. 
                    if (user_smoker == smoker && user_eyecolor == eyecolor)
                    {
                        fullMatch++;

                        cout << "* " << firstname << "  " << lastname  << setw(25) << age << setw(11) << height << setw(11) << smoker << setw(11) << eyecolor << setw(11) << phoneno << endl;
                    }

                    else if (eyecolor == user_eyecolor)
                    {
                        partialMatch++;

                        cout << "  " << firstname << "  " << lastname << setw(24) << age << setw(11) << height << setw(11) << smoker << setw(11) << eyecolor<< setw(11) << phoneno << endl;
                    }
                }
            }
        }
        cout << "-----------------------------------------------------------------------------" << endl;
        cout << "There were " << fullMatch << " matches and " << partialMatch << " partial matches out of " << countGender << " records." << endl;
        cout << "-----------------------------------------------------------------------------" << endl;

        fin.close();
    }
    else {
        cout << "File did not open";
    }


    return 0;
}

****The program is working perfectly fine, but I am not getting the output printed in columns formatted as shown in the above sample output. **** Write a program that opens the file and reads the records one by one. The program will skip any records where the gender preference is not a match. Of those records that match the gender preference, check to see if the age and height are between the maximum and minimum preferences. Then check to see if the smoking preference and eye color are also a match. If at least 3 of the remaining fields match, consider the record a partial match, and print it in the report. If all 4 of the remaining fields match, the record is a perfect match and print it in the report with an asterisk next to it. At the end of the program, close the file and report how many total records there were of the specified gender, how many were a partial match, and how many were a perfect match.

Charlie Bradbury    F   42  65  N   Green   555-867-5309
Bobby Singer        M   70  69  Y   Brown   555-867-5309
Dean Winchester     M   43  72  N   Brown   555-867-5309
Sam Winchester      M   39  75  N   Brown   555-867-5309
Bela Talbot         F   39  69  Y   Blue    555-867-5309
James Novak         M   46  71  Y   Blue    555-867-5309

enter image description here

Upvotes: 0

Views: 99

Answers (1)

Thomas Weller
Thomas Weller

Reputation: 59303

  1. Use a proper IDE with debugging capabilities (e.g. Visual Studio Community Edition).

  2. Set a breakpoint at the first line of the method which is problematic, e.g. line 9 in your case

    Breakpoint in Visual Studio

  3. Step over your code line by line and see what it does.

    Step over

  4. Hover over variables or use the locals or watch window to see the values of variables

    Locals window

  5. Think about what you expected to see and compare it to what you actually see. In 99% of the cases, the computer is more right than you and it's a misunderstanding on your side.

Upvotes: 2

Related Questions