Description: Friend’s finder. You load a data file first. Then you answer 10 questions. The answers will be saved in an array. The data in file are several arrays answered by other users. You need to compare the answers with other users and calculate the similarity. After that it will show your best 1Emphasized Text Here0 matches on the monitor.

I am able to read the file but I am having major issues with assigning the values in the file into several arrays. Every time I run the program it fails it may be a loop problem but I'm not completely sure.

This is my data file: Answers2.txt

Howard Lacrosse 19 Engineering LTU Belleville Green Pizza African Male
Zaid Tennis 19 Engineering LTU Dearborn Pink Soup Arab Male
Yat Basketball 19 Civil LTU Belleville Green Rice Chinese Male
Stephanie Yoga 24 Architect Central southfield Blue Chicken Asian Female
Adam Soccer 19 GameArt LTU Ypsilanti Red Chicken Caucasian Male
Ashley Karate 18 Media LTU Westland Magenta Oranges Caucasian Female
Hentrell Basketball 19 GameArt LTU Chicago Black Chicken African Male
Corey Baseball 24 PreDental LTU Chicago Black Pizza Caucasian Male
Topanga Golf 25 Physics LTU Dearborn Pink Soup Caucasian Female
Katelyn Softball 20 History GrandValley Warren Blue Oranges Caucasian Female

The 10 questions if you cannot tell:
What is the person's name?
What is their favorite Sport?
How old is said person?
What Major is said person?
What School does said person attend?
Where is said person From?
What is said person's favorite color?
What is said person's favorite food?
What race is said person?
What sex is said person

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

void outputLine(string [], string [], int [], string [],  string [],  string [],  string [],  string [], string [], string []);

int main()
{
    ifstream inClientFile("Answers2.txt", ios::in);

    if(!inClientFile)
    {
        cerr <<"FIle could not be opened" <<endl;
        exit(1);
    }

    string name[9];
    string sports[9];
    int age[9];
    string major[9];
    string school[9];
    string city[9];
    string color[9];
    string food[9];
    string race[9];
    string sex[9];

    for(int i = 0; i <= 9; i++)
    {
    inClientFile >> name[i] >> sports[i] >> age[i] >> major[i] >> school[i] >> city[i] >> color[i] >> food[i] >> race[i] >> sex[i];

    outputLine(name, sports, age, major, school, city, color, food, race, sex);
    }
}

void outputLine(string name[9], string sports[9], int age[9], string major[9],  string school[9], string city[9], string color[9], string food[9], string race[9], string sex[9])
{   
        cout <<left<< setw(7) << name[9] << setw(7) << sports[9] << setw(7) << age[9] << setw(7) << major[9] << setw(7) << school[9] << setw(7) << city[9] << setw(7) << color[9] << setw(7) << food[9] << setw(7) << race[9] << setw(7) << setprecision(2) << right << sex[9]<<endl;
}

Dani AI

Generated

Two quick facts first: the crash comes from out-of-bounds indexing and confusing a single element with an array. was right — printing name[9] (or declaring string name[9] and then looping i <= 9) will walk off the end of the array. Stop making one fixed array per person (Howard[], Zaid[], ...) — that pattern is painful and brittle. Use a single struct for a person and a dynamic container for all people.

Try this pattern: define a Person struct, read each file line into a Person with an istringstream, and push_back into a vector<Person>. That way you never hard-code record counts and you avoid index mistakes.

struct Person {
    std::string name, sport, major, school, city, color, food, race, sex;
    int age = 0;
};

std::vector<Person> loadFile(std::istream &in) {
    std::vector<Person> out;
    std::string line;
    while (std::getline(in, line)) {
        if (line.empty()) continue;
        std::istringstream iss(line);
        Person p;
        if (iss >> p.name >> p.sport >> p.age >> p.major >> p.school
                >> p.city >> p.color >> p.food >> p.race >> p.sex)
            out.push_back(std::move(p));
        else
            std::cerr << "parse error: " << line << "\n";
    }
    return out;
}

Collect the user's answers into a single Person me (use getline for strings, parse age with stoi) and score matches field-by-field. A simple, robust comparator is case-insensitive string equality for most fields and exact/evaluated match for age. Example scoring helper:

bool ieq(...) { /* case-insensitive compare */ }
int score(const Person &a,const Person &b) { int s=0; s+=ieq(a.sport,b.sport); s+=(a.age==b.age); /*...*/ return s; }

Run the score against every loaded person, track the highest score, and report that index. Debug tips: print parsed token counts, run with AddressSanitizer or a debugger if it segfaults, and watch for fields that may contain spaces (then use quoted/CSV parsing). This approach fixes the off-by-one bugs, simplifies the logic, and makes similarity calculations straightforward.

Recommended Answers

All 2 Replies

You need to start over from scratch. Lines 39 to 42 show that you don't understand the difference between a single object and an array. You should also be using structures if you know them already. This function is a segmentation fault waiting to happen since you are using illegal array indexes. You shouldn't be trying to print an array of people in this function anyway. You should be printing a single person in this function. The function should not take any arrays as parameters.

I have no clue what this means...

You need to compare the answers with other users and calculate the similarity. After that it will show your best 1Emphasized Text Here0 matches on the monitor.

I was able to assign an array for each person from the file.

Sorry What I meant was the program should ask 10 questions and put the answers to those questions into an array.Now the File has Data which include answers from other users(which will be in individual arrays). After you answer the 10 questions it should compare your answers with the answers in the file.

Now my next questions is How do I ask the questions and put the answers into a single array.

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

void outputLine();

int main()
{
    ifstream inClientFile("Answers2.txt");

    if(!inClientFile)
    {
        cerr <<"FIle could not be opened" <<endl;
        exit(1);
    }

    string Howard[10];
    string Zaid[10];
    string Yat[10];
    string Stephanie[10];
    string Adam[10];
    string Ashley[10];
    string Hentrell[10];
    string Corey[10];
    string Topanga[10];
    string Katelyn[10];
    string spaceholder[10];

    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Howard[i];
    cout << Howard[i] <<" ";


    //outputLine();

    }

    cout<< endl;

    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Zaid[i];
    cout << Zaid[i]<<" ";
    //outputLine();

    }
    cout<< endl;

    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Yat[i];
    cout <<Yat[i]<<" ";
    //outputLine();

    }
    cout<< endl;

    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Stephanie[i];
    cout <<Stephanie[i]<<" ";
    //outputLine();

    }
    cout<< endl;

    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Adam[i];
    cout <<Adam[i]<<" ";
    //outputLine();

    }

    cout<< endl;
    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Hentrell[i];
    cout <<Hentrell[i]<<" ";
    //outputLine();

    }
    cout<< endl;
for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Corey[i];
    cout <<Corey[i]<<" ";
    //outputLine();

    }
    cout<< endl;
for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Topanga[i];
    cout <<Topanga[i]<<" ";
    //outputLine();

    }
    cout<< endl;

    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> Katelyn[i];
    cout <<Katelyn[i]<<" ";
    //outputLine();

    }
    cout<< endl;

    for(int i= 0; i <= 9; i++)
    {
    inClientFile >> spaceholder[i];
    cout << spaceholder[i] <<" ";


    //outputLine();

    }

}
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.