Hola,

Just like to say hi to everyone, i just joined the forums.


Ok, I have been playing with C++ now for about 3 or 4 months and have now hit a brick wall and need your help.

I am trying to write a program that will read in data from an input text file in the form of names and birthdates. After the data has been read in, the user willl be prompted to enter the month in which a person was born. After the user inputs the requested data, the program should output all persons in the text file with this birth month.

This is the input text file:

Snow White	2/26/1971

Peter Pan	10/1/1922

Miss Piggy	12/25/1966

Tish TheDish	3/22/1910

Mark Twain	11/11/1924

Poopsie Mugglesworth	4/10/1999

Lilly BenYoda	10/10/2003

Oscar deLaCruz	8/15/2003

Felix deLaCruz	8/15/2003

Espy deGuadelupe	5/13/2002

Amanda Jane	6/9/2004

Louise Allen	1/10/1960

Ida Red		7/4/1961

What i have been trying:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <conio>
#include <cstring>

using namespace std;

struct B_Days
{
        string names[2];

};

ifstream fin;
ofstream fout;

#define AND &&
#define OR ||
#define NOT !=

void headerfn( );

void inputfn(B_Days friends[51], int &count);

int main( )
{
        headerfn( );
        int count;
        B_Days friends[51];
        char filename[51];

        cout<<"Please enter the name and path of the input text file: ";
        cin>>filename;

        cout<<endl<<"Thank you! You entered: "<<filename<<endl<<endl;

        fin.open(filename);

        if(!fin)
        {
                cout<<"Input error\n\n"
                    <<"Could not open text file\n\n"
                    <<"Press any key to end\n\n"<<endl;
                getch( );
                return 1;
        }

        inputfn(friends, count);

        cout<<"Please press any key to exit the program"<<endl;
        getch( );
        return 0;
}
void headerfn( )
{
        cout<<setfill('*')<<setw(70)<<'*'<<endl<<endl;
        cout<<setw(5)<<' '<<"Electronic Address Book:  This program reads names"
            <<" and birthdays from"<<endl;
        cout<<setw(5)<<' '<<"an input text file into an array of structs"
            <<" then outputs the names"<<endl;
        cout<<setw(5)<<' '<<"and birthdays of those born in the month of your"
            <<" choice."<<endl;
        cout<<setfill('*')<<setw(70)<<'*'<<endl<<endl;

}
void inputfn(B_Days friends[51], int &count)
{
        count=0;

        while(!fin AND count < 51)
        {
                fin.get(friends[count].names, 20+1);
                count++;
        }
}

Now, my question is: how do i go about reading this data in and then compare the birth month with the one entered? After i have compared the data, how do i return the result and output it? Do i use cstrings strcmp... or what?

Thanks,
:?:

#define AND &&

and is already a keyword to that effect (in C++; and C too if you include a header file).

void inputfn(B_Days friends[51], int &count)
{
        count=0;

->

void inputfn(B_Days friends[51], int &count)
{
        int count=0;

count was declared in main and passed by reference to the input function.

Why so it was. Sorry, I missed that. It looked like a declaration.

Now, my question is: how do i go about reading this data in and then compare the birth month with the one entered? After i have compared the data, how do i return the result and output it?

You can use stringstreams to separate the input string into tokens. And you don't need to return the data; it's a global variable. And printing it is easy. Use a for loop.

I really appreciate your help on this matter, but that really makes no sense to me. :D It isn't because your idea is unfounded or wrong, rather it’s quite possible that it’s over my head. I would really appreciate it if you could elaborate a little on this… maybe show me an example.

I have it reading the names into the array of structs. If someone could give me an example of how to read the dates in and then compare the date in the text file to the date input by the user and output the matching results, I would greatly appreciate it.

#include <iostream>
#include <conio>
#include <iomanip>
#include <fstream>

using namespace std;


ifstream fin;
ofstream fout;

struct B_Days
{
        string name[2];
        char month[2];
        char day[2];
        char year[4];

}friends[50+1];

void header();

void inputfn(int &count);


int main()
{
        header();

        fin.open("input3.txt");

        if(!fin)
        {
                cout<<"Input error\n"
                    <<"Press any key to end"<<endl;
                getch();
                return 1;
        }

        int count;

        inputfn(count);

        /*for(int i=0; i < count; i++)
        {
                for(int n=0; n < 2; n++)
                {
                        cout<<friends[i].name[0]+' '+friends[i].name[1]<<endl;
                }
        }*/
        //cout<<endl<<endl<<count<<endl<<endl;




        cout<<"Press any key to continue...";
        getch();
        return 0;

}

void header()
{
       cout<<setfill('*')<<setw(70)<<'*'<<endl<<endl;
        cout<<setfill(' ')<<setw(5)<<' '<<"Electronic Address Book:  This program reads names"
            <<" and birthdays from"<<endl;
        cout<<setw(5)<<' '<<"an input text file into an array of structs"
            <<" then outputs the names"<<endl;
        cout<<setw(5)<<' '<<"and birthdays of those born in the month of your"
            <<" choice."<<endl;
        cout<<setfill('*')<<setw(70)<<'*'<<endl<<endl;
}
void inputfn(int &count)
{
        count=0;

        while(fin && count < 50)
        {

                for(int i=0;i < 2; i++)fin>>friends[count].name[i];
                fin.ignore(100, '\n');


                count++;

                if(fin.peek()=='\n')fin.ignore();

        }
        cout<<friends[3].name[0]+' '+friends[3].name[1];
}

Thank you for your help. :mrgreen:

This article has been dead for over six months. Start a new discussion instead.