Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,155 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,116 people online right now. Registration is fast and FREE... Join Now!




2 parts of attention required

 
Reply to this topicStart new topic

2 parts of attention required, header file not working, and selecting an entry on a file

IainAndRichie
13 Dec, 2007 - 06:17 PM
Post #1

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 5


My Contributions
cpp--
CODE


#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include <date.h>
using namespace std;

struct Contact{
    string name;
    string address;
    string HomeNumber;
    string WorkNumber;
    string MobileNumber;
    string email;
    string dobYear;
    string dobMonth;
    string dobDay;
    int age
};

const int MAX = 101;
Contact listContact[MAX];
int ContactCounter;

int checkEmail (Contact c)
{
    c.email.find("@");
    if (c.email.find("@")>c.email.length())
        return 0;
    else return 1;
}

Contact newContact()
{
    Contact c;
    do {
        cout << endl << "Enter name : ";
    getline (cin, c.name);
    if (c.name == "")
        cout << "Invalid Name." << endl;
    else cout << endl;
    } while(c.name == "");
    
    do {
        cout << endl << "Enter address : ";
    getline (cin, c.address );
    if (c.address == "")
        cout << "Invalid Address." << endl;
    else cout << endl;
    } while (c.address == "");
    
    do {
        cout << endl << "Enter home number : ";
    getline (cin, c.HomeNumber );
    if (c.HomeNumber == "")
        cout << "Invalid Number." << endl;
    else cout << endl;
    } while (c.HomeNumber == "");
    
    do {
        cout << endl << "Enter work number : ";
    getline (cin, c.WorkNumber );
    if (c.WorkNumber == "")
        cout << "Invalid Number." << endl;
    else cout << endl;
    } while (c.WorkNumber == "");
    
    do {
        cout << endl << "Enter mobile number : ";
    getline (cin, c.MobileNumber );
    if (c.MobileNumber == "")
        cout << "Invalid Number." << endl;
    else cout << endl;
    } while(c.MobileNumber == "");
    
    do {
        cout << endl << "Enter email : ";
    getline (cin, c.email );
    if (checkEmail (c)==0)
        cout << "Invalid Email Address." << endl;
    else cout << endl;
    } while (checkEmail (c) ==0);
    
    do {
        cout << endl << "Enter year of birth(4 digits) : ";
    getline (cin, c.dobYear );
    } while (c.dobYear == "");
    
    do {
        cout << endl << "Enter month of birth(2 digits) : ";
    getline (cin, c.dobMonth);
    if (c.name == "")
        cout << "Invalid name." << endl;
    else cout << endl;
    } while(c.dobMonth == "");
    
    do {
        cout << endl << "Enter day of birth(2 digits) : ";
    getline (cin, c.dobDay);
    } while (c.dobDay == "");

    int numdobDay;
    stringstream dobDaystream(c.dobDay);
    dobDaystream >> numdobDay;
    
    int numdobMonth;
    stringstream dobMonthstream(c.dobMonth);
    dobMonthstream >> numdobMonth;

    int numdobYear;
    stringstream dobYearstream(c.dobYear);
    dobYearstream >> numdobYear;


    date today;
    cout << today;
    cout << endl << "The year is " << today.year();
    cout << endl << "The month is " << today.month();
    cout << endl << "The day is " << today.day();
    
    
    c.age = today.year - numdobYear;
    if (numdobMonth > today.month)
    --c.age;
    else if (numdobMonth == today.month && numdobDay > today.day )
    --c.age;
    else if (numdobMonth == today.month && numdobDay == today.day )
    cout << "^_^ Happy Birthday To You happy.gif";

    return c;
}

void writeContacts(Contact &c, ofstream &os)
{
    os << c.name << endl
        << c.address << endl
        << c.HomeNumber << endl
        << c.WorkNumber << endl
        << c.MobileNumber << endl
        << c.email << endl
        << c.dobYear << endl
        << c.dobMonth << endl
        << c.dobDay << endl;
}

void readContacts(Contact &c, ifstream &is)
{
    getline(is, c.name);
    getline(is, c.address);
    getline(is, c.HomeNumber);
    getline(is, c.WorkNumber);
    getline(is, c.MobileNumber);
    getline(is, c.email);
    getline(is, c.dobYear);
    getline(is, c.dobMonth);
    getline(is, c.dobDay);
}

void addContact()
{
    cout << endl << "Input new contact data: " << endl;
    int ContactCounter = 0;
    listContact[ContactCounter] = newContact();
    ContactCounter++;
}

void display(Contact &c, ifstream &is)
{
    cout << endl << "Contact Data:" << endl
        << "Name          - " << c.name << endl
        << "Address       - " << c.address << endl
        << "Home Number   - " << c.HomeNumber << endl
        << "Work Number   - " << c.WorkNumber << endl
        << "Mobile Number - " << c.MobileNumber << endl
        << "Email         - " << c.email << endl
        << "Date of Birth - " << c.dobDay <<"/"<< c.dobMonth <<"/"<< c.dobYear << endl;
}

void displayContact(Contact c)
{
        cout << endl
        << "Name          - " << c.name << endl
        << "Address       - " << c.address << endl
        << "Home Number   - " << c.HomeNumber << endl
        << "Work Number   - " << c.WorkNumber << endl
        << "Mobile Number - " << c.MobileNumber << endl
        << "Email         - " << c.email << endl
        << "Date of Birth - " << c.dobDay <<"/"<< c.dobMonth <<"/"<< c.dobYear << endl;
}

void readFile()
{
   try{
      ifstream inFile("C:\\Temp\\Contacts.dat", ios::in);
      inFile >> ContactCounter;
      inFile.ignore();
      for(int i=0; i<ContactCounter; i++)
       display(listContact[i], inFile);
      inFile.close();
   }
   catch(exception e) {
      cout << endl << "<-|Error|->" << endl << "Could not read file." << endl;
   }
}

void writeFile()
{
    try{
        ofstream outFile("C:\\Temp\\Contacts.dat", ios::out);
        outFile << ContactCounter << endl;
        for(int i=0; i<ContactCounter; i++)
            writeContacts(listContact[i], outFile);
        outFile.close();
    }
    catch(exception e) {
      cout << endl << "<-|Error|->" << endl << "Could not read file." << endl;
    }
}

void doMenu()
{
      int choice;
      int ContactNumber;
      do{
          cout << "Enter your selection." << endl;
         cout << "1. Load Contacts List" << endl;
         cout << "2. Save Contacts List" << endl;
         cout << "3. Add a Contact" << endl;
         cout << "4. Display Contact(s) Details" << endl;
         cout << "5. Quit" << endl;
         cin >> choice;
         cin.ignore();
         switch(choice)
         {
         case 1: readFile();
             break;
         case 2: writeFile();
             break;
         case 3: newContact();
             break;
         /*case 4: cout << "Enter the number of the contact : " << endl;
             cin << ContactNumber;
             for (int i; i < MAX; i++)
                displayContact (ContactNumber)
                 else cout << "Contact Not Found." <<endl;*/
         }
      } while(choice != 5);
}

int main ()
{
    readFile();
    doMenu();
    writeFile();
}




header --
CODE


#ifndef _DATE_TYPE
#define _DATE_TYPE
#include<ctime>
#include<iostream>
using namespace std;

class date{
private:
    int _day, _month, _year;
public:
    date(int d, int m, int y){
        _day = d;
        _month = m;
        _year = y;
    }
    int day(){return _day;}
    int month(){return _month;}
    int year(){return _year;}
    date(){
        time_t theTime;
        tm localTime;
        time(&theTime);
        localtime_s(&localTime, &theTime);
        _day = localTime.tm_mday;
        _month = localTime.tm_mon;
        _year = localTime.tm_year+1900;
    }
    friend ostream& operator<<(ostream& os, const date &d){
        return os << d._day << '/' << d._month << '/' << d._year;
    }
};
#endif




I have one bug just now, and its telling me that date.h is "no such file or directory". which makes me assume that its the only problem, untill i get to taking the case 4: out of a comment. Which im also unable to get to work... Help please?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: 2 Parts Of Attention Required
13 Dec, 2007 - 07:02 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
this indicates a problem with your compiler environment. Are you using an IDE or a command line compiler?

Basically make sure that the header file is in the same directory as the source file. If you are using an IDE make sure that it is included in the project and is in the same directory.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: 2 Parts Of Attention Required
13 Dec, 2007 - 07:27 PM
Post #3

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Is code.h in the same directory as your main c++ file?
If you're using a dev environ, just put them in the same project.

As for the case 4, it looks incomprehensible. Did you write it, or are you in a bug fixing exercise?

This should work:

CODE

case 4: cout << "Enter the number of the contact : " << endl;
  cin >> ContactNumber;
  if(ContactCounter > ContactNumber >= 0) {
    displayContact(listContact[ContactNumber]);
  } else {
    cout << "Contact Not Found." <<endl;
  }


And rest assured, there's a thousand other bugs in there.

For instance, your ContactCounter is never going to increment, among other issues.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: 2 Parts Of Attention Required
13 Dec, 2007 - 07:47 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
btw, in DevC++ you'll have to make sure that the .h file is in an include directory. Which means you'll either have to copy it to one, or add the directory where it resides to the include list.
User is offlineProfile CardPM
+Quote Post

IainAndRichie
RE: 2 Parts Of Attention Required
14 Dec, 2007 - 05:17 AM
Post #5

New D.I.C Head
*

Joined: 8 Dec, 2007
Posts: 5


My Contributions
Im using Visual C++. Its in the same project, and everything... T_T

Its just not reading it.
User is offlineProfile CardPM
+Quote Post

Bench
RE: 2 Parts Of Attention Required
14 Dec, 2007 - 11:02 AM
Post #6

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
are you sure that date.h is the exact name? For example, make sure that there's no additional extensions which windows has added invisibly, also, make sure that the letter case matches, and that you haven't got a space character anywhere in the file name

If all else fails, create a new project in MSVC, create two new files using MSVC (making sure that you name "date.h" carefully), and copy/paste the code in.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:18PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month