GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #41  
Old 05-May-2005, 14:28
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
Well if it's being used to change the areacode, and areacode is and int, my guess would be an int value that you will assign to the areacode. But if you are going to take user input within the overloaded operator (to fit "what your professor says"), then you would probably merely need to dummy it up.

dummy it up?

do you think you know what's wrong with my menu?
it'll be nice if at least my code would open the menu... but
i havent been able to see my program 'cause its errors and errors...
  #42  
Old 05-May-2005, 14:43
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by marita
dummy it up?
CPP / C++ / C Code:
void operator[] (int dummy)
{
   std::cout << "new area? ";
   if ( std::cin  >> dummy )
   {
      areacode = dummy;
   }
}

Quote:
Originally Posted by marita
do you think you know what's wrong with my menu?
it'll be nice if at least my code would open the menu... but
i havent been able to see my program 'cause its errors and errors...
No idea about the menu, all I see is errors and errors too. That's why it's best to start with smaller chunks compiling frequently as new things are added. Going the other way, you get to wade through the error messages and fix things up until you finally get a clean compile.
  #43  
Old 06-May-2005, 08:58
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
marita,

It seems that perhaps I missed the boat with some of my explanations to you. Good thing Dave picked up the slack.

I do want to add that you have been making good progress, keep at it. From wayyyyy back at your first post:

Quote:
Originally Posted by marita
read from a file phonebook.txt into a dynamic array of Phinebook objects and asign data to each. Using a loop display each object data. Prompt the user to select a number on the array to modify a Phinebook entry. When the user has chosen a valid entry, asks the user if he wants to alter the entire entry or just the phone number. Accepts new data values accordingly. If the user wants to modify the entire entry, create a temporary object to correct location in the array. If the user wants to change the area code and phone number, or change the phone number only, prompt the values, then use the [] or () operator to asign the new values to the proper existing object within the array. After update has taken effect, redisplay the Phonebook entries. If the user exists the program the array info must be save in the file.

So you have your list of requirements
  1. read from a file phonebook.txt into a dynamic array of Phinebook objects and asign data to each
  2. Using a loop display each object data. Prompt the user to select a number on the array to modify a Phinebook entry
  3. When the user has chosen a valid entry, asks the user if he wants to alter the entire entry or just the phone number. Accepts new data values accordingly.
  4. If the user wants to modify the entire entry, create a temporary object to correct location in the array.
  5. If the user wants to change the area code and phone number, or change the phone number only, prompt the values, then use the [] or () operator to asign the new values to the proper existing object within the array.
  6. After update has taken effect, redisplay the Phonebook entries.
  7. If the user exists the program the array info must be save in the file.

That is your blueprint for your program. Don't get caught up in trying to complete everything at once. Take each step one at a time until you have them all done. First off though, you need to really read your errors. They seem for the most part to point directly to the offending lines. You have a few choices. My method is to comment out all the extra problems and get a compiled version (even if it doesn't do much to start) and slowly (like one at a time) add back in the pieces. To start with, your operator overloading is not going like you want. Do you really understand what you are trying to do with each. When you overload an operator it is so you can define how your program handles things like indexing ( the [] operator ) and assignment (the = operator). Quite frankly, you are jumping back and forth changing and adding but never having had anything to work with.

Right now your class looks like this:
Quote:
Originally Posted by marita
CPP / C++ / C Code:
class PhoneBook    // class definition
{
public:
  PhoneBook();  // default constructor
  fileCount();  // function supposed to count each row in the file
  void operator[] ();  // operator overloading for [] - changes area code
  void operator() ();  // operator overloading for () - changes phone number
  void operator = ();  // operator overloading for = - changes area code & phone number


private:
  char name[15];
  char lastname[15];
  int areacode;
  int phone;
  
};
Back when you started, you had something a little different.
Quote:
Originally Posted by marita
CPP / C++ / C Code:
class Phonebook
{
private:
  char FirstName [12];
  char LastName [12];
  char AreaCode [4];
  char PhoneNumber[8];


public:
  Phonebook();
  void operator [] (const Phonebook &PhoneNumber);
  void operator () (const Phonebook &AreaCode, &PhoneNumber);
  void operator = (const Phonebook &FirstName, const Phonebook &LastName, const Phonebook &AreaCode, const Phonebook &PhoneNumber);

  void print()const;


};

As you can see, there are some changes based on the type of data (changing areacode and phone to ints) and some that may be a problem. Look at the operator[]. It is supposed to take a constant reference to a Phonebook object. Since your class name is now PhoneBook change references to Phonebook to match that (PhoneBook). Now the operator[] takes one argument, which is a reference to a constant object of type PhoneBook. The body of your function will handle some change to the normally defined operator. In your case this refers to item number 5 in the above list. Way down there. So I suggest just some basic changes to your class to get rid of the errors and work on your main a bit before tackling some of the more complicated parts. You can do this in a number of ways. For your example you can define everything (remembering the const PhoneBook&) and leave the body empty or comment out the actual code until you are ready to work on it.

CPP / C++ / C Code:
class PhoneBook    // class definition
{
public:
  PhoneBook(){};  // default constructor note empty braces
  void fileCount(){};  // function supposed to count each row in the file
  void operator[] (int){};  // operator overloading for [] - changes area code
  void operator() (int){};  // operator overloading for () - changes phone number
  void operator = (const PhoneBook&){};  // operator overloading for = - changes area code & phone number


private:
  char name[15];
  char lastname[15];
  int areacode;
  int phone;
  
};

Note the empty braces. This will require you to comment out your implementations (as they implement an empty method in the class definition) until you are ready to add them. As has been pointed out by both Dave and I, do these a little bit at a time from something that compiles. Here is a cut-away version of your code just so you can compile it. Then (and only then) you can add (one at a time) the parts of the program that make it actually do something.

CPP / C++ / C Code:
// May 5, 2005

#include <iostream>    // libraries
#include <fstream>
#include <string>
using std::cout;
using std::endl;
using std::cerr;
using std::ifstream;
using std::ofstream;
using std::ios;
using std::cin;


class PhoneBook    // class definition
{
public:
  PhoneBook(){};  // default constructor
  int fileCount();  // function supposed to count each row in the file
  void operator[] (int){};  // operator overloading for [] - changes area code
  void operator() (int){};  // operator overloading for () - changes phone number
  void operator = (PhoneBook&){};  // operator overloading for = - changes area code & phone number


private:
  char name[15];
  char lastname[15];
  int areacode;
  int phone;
  
};


//FUNCION FILECOUNT
// This function will open the .txt file, count the number of lines and
// return the number of enteries.
// This function uses ifstream, ios, cerr, getline() and endl.
// It depends on a text file in the same directory named phonebook.txt
int PhoneBook::fileCount()
{
  char line[80];
  int  count = 0;

//  fstream file;

  ifstream file;
  file.open("phonebook.txt", ios::in);
  if(!file)
    cerr << "File phonebook.txt could not be opened" << endl;

  while ( file.getline(line, sizeof line) )
  {
    cout << line << endl;
    ++count;
  }
  file.close();
  return count;
}


int main()
{
  char option;
  int howmany = 0;

  PhoneBook p;  // local class object

  howmany = p.fileCount();
  cout << "howmany = " << howmany << endl;

//Menu
  do
  {
    cout << "***************************************";      //print menu
    cout << "****************************************" << endl;
    cout<<"                                     MENU                                   " << endl;
    cout << "**************************************";
    cout << "******************************************" << endl;
    cout << "What would you like to do? " << endl;
    cout << "(1) Change Area Code" << endl;            // print option (1) to change the Area Code
    cout<<"(2) Change Phone Number"<<endl;             // print option (2) to change the Phone Number
    cout<<"(3) Change Area Code & Phone Number"<<endl; // print option (3) to change Area Code & Phone Number
    cout<<"(4) Print"<<endl;                           // print option (4) for printing
    cout<<"(5) Exit"<<endl;                            // print option (5) to exit
  
    cin >>option;

    switch (option)   // begins switch structure
    {
      case '1':
        cout << "This is case 1:" << endl;
        break;

      case '2':
        cout << "This is case 2:" << endl;
        break;

      case '3':
        cout << "This is case 3:" << endl;
        break;

      case '4':
        cout << "This is case 4:" << endl;
          // Imprime el archivo
        break;

      case '5':
        cout << "This is case 5:" << endl;
        cout << "Exiting...." << endl;  //exits
        break;

      default:
        // If none of the above cases are matched you will end up here
        cout<<"Select an option from 1 -> 5"<< endl;
        break;
    }                                        //end swicth structure
  }while(option != '5');    //end do/while structure

  return 0;
}

  /* Falta:
      - tengo que buscar donde esta el cursor
      - escribir en el archivo
      - darle delete a la memoria dinamica
      - cerrar el archivo
  */
Notice the things that are corrected. The single quotes around your case numbers is so you will compare a char to a char, not a char to a constant integer. This piece of code is more or less what you had. Use this to start from and add the list of requirements. Right now, all this does is to create an object of type PhoneBook named p, a char for menu choices, and an integer that will hold the number of entries in your text file.
CPP / C++ / C Code:
  char option;
  int howmany = 0;

  PhoneBook p;  // local class object

  howmany = p.fileCount();
  cout << "howmany = " << howmany << endl;
When p.fileCount() is called it opens the text file, reads a line, outputs the line to the console, continues until the end, closes the file, and returns the number of lines it found. Because of this, the name of your textfile seems like it should be part of the class but just hardwiring it in works for now. Then the program enters into its menu loop until the user chooses to quit with '5'.

If you can do that part and get something you can run I can help you with the rest. You have actually been pretty close a number of times but then made drastic changes and ended up with some of your original errors again. One thing you really need to pay attention to is the include and using portions. If you want to use a function or variable it either has to be defined in your source code, or defined in code that you include. If you include something you than have to using it.

This code of yours that I just posted is ready to dynamically allocate an array of PhoneBook objects for your project. That is your first item on the list and should be the first thing you implement. This will be done by creating an array of pointers for your PhoneBook type using new. They will have to be destroyed using delete.

I do admire the fact that you have stuck with this. It is not simple what you are trying to do but it is not terribly difficult either. Somewhere in the middle (or a double blue if you are a skiier). If you really understand all this when you are done your future coding will be the better for it. Note the slight changes I have made. Read over the replies by Dave as they do a good job of expaining some concepts.

Mark

Here is the output from running the above:
Code:
$ PhoneBook Marie Nieves (555) 555-5555 Juanita Rivera (777) 777-7777 Pepito Conde (888) 888-8888 Panchita Cruz (999) 999-9999 howmany = 4 ******************************************************************************* MENU ******************************************************************************** What would you like to do? (1) Change Area Code (2) Change Phone Number (3) Change Area Code & Phone Number (4) Print (5) Exit 5 This is case 5: Exiting....
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #44  
Old 27-May-2005, 11:14
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Hi!
Thank you very much for all your help!

I really appreciate it.
Sadly, i read this code TODAY! But anyways, i already sent my code to the professor, and i had a pretty nice grade, having in mind that my code did not compile.
Anyways, now i have to make, mostly the same program but using a friend function.
Im still working on it, but if i get stuck; i'll scream at ya!
Thanks a lot!
  #45  
Old 27-May-2005, 11:22
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Ermm.....
is there a way that i can write this line doing the same but WITHOUT using sizeof and getline? :

CPP / C++ / C Code:
  while ( file.getline(line, sizeof line) )
  #46  
Old 27-May-2005, 12:16
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
This is the code i have right now:

CPP / C++ / C Code:

#include <iostream>    // libraries
#include <fstream.h>
#include <string>


class PhoneBook    
{
private:
  char name[15];
  char lastname[15];
  int areacode;
  int phone;


public:
  PhoneBook() {};  // default constructor
  int fileCount();  // function supposed to count each row in the file
  friend PhoneBook getData(PhoneBook);
  void getName();
  void getLName();
  
};


//FUNCION FILECOUNT
// This function will open the .txt file, count the number of lines and
// return the number of enteries.
// This function uses ifstream, ios, cerr, getline() and endl.
// It depends on a text file in the same directory named phonebook.txt

int PhoneBook::fileCount()
{
  char line[80];
  int  count = 0;

//  fstream file;

  ifstream file;
  file.open("phonebook.txt", ios::in);
  if(!file)
    cerr << "File phonebook.txt could not be opened" << endl;

  while ( file.getline(line, sizeof line) )
  {
    cout << line << endl;
    ++count;
  }
  file.close();
  return count;
}


void PhoneBook::getName()
{
	strcpy (this ->name, name);
	
}

void PhoneBook::getLName()
{
	strcpy (this->lastname, lastname);

}

void main()
{
  char option;
  int howmany = 0;
  fstream file;

  PhoneBook p;  // local class object

  howmany = p.fileCount();
  cout << "howmany = " << howmany << endl;

//Menu
  do
  {
    cout << "***************************************";      //print menu
    cout << "****************************************" << endl;
    cout<<"                                     MENU                                   " << endl;
    cout << "**************************************";
    cout << "******************************************" << endl;
    cout << "What would you like to do? " << endl;
    cout << "(1) Search By Name" << endl;            
    cout << "(2) Search By Last Name"<<endl;            
    cout << "(3) Exit"<<endl << endl;            

    cin >>option;

    switch (option)   // begins switch structure
    {
      case '1':
				cout << "Searching By Name.... "<< endl;

					if (name, arreglo[i].getName = = 0)
				

				break;
				

			case '2':
				cout << "Searching By Last Name.... "<< endl;

					if ( lastname, arreglo[i].getLName = = 0)


				break;


			case '3':
				cout << "Exiting... "<< endl;
				file.close();


				break;


			default:
				cout << "ERROR!"<< endl;
		}
	}
	while (option != '3');
}



i need to have a friend function.
That program is supposed to open the file phonebook.txt. Load ll that i have on the file into a class.
The menu is supposed to search either by Name or by last Namr.

A class is supposed to search in the file and what it has, save it on a new friend class and prints it into a new file.
Having in mind that if the user enters one name (i.e. George and the file has more than one George, it is supposed to prints them all)

Im kinda stcuk on what i have so far....
Some help please....?
  #47  
Old 12-Jun-2005, 13:10
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
I must have dropped this thread somewhere along the way. I didn't realize you were back at it.

If you are still interested:
  • How far along are you?
  • What are your errors with this current code?

Using g++ 3.4.1 with CygWin I get a number of warnings and errors.
Code:
$ g++ -Wall maritaPB.cpp -s -o maritaPB In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.1/../../../../include/c++/3.4.1/backward/fstream.h:31, from maritaPB.cpp:2: /usr/lib/gcc/i686-pc-cygwin/3.4.1/../../../../include/c++/3.4.1/backward/backward_warning.h:32:2: warning: #wa rning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 hea ders found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

The line it refers to:
CPP / C++ / C Code:
#include <fstream.h>
Read back over some of the posts in this thread (or others in the forum) about including and using. The only real reason to do it this way is to avoid namespace std;. Since you are using <iostream> not <iostream.h> I would have to guess that this was just a typo.

Code:
maritaPB.cpp: In member function `int PhoneBook::fileCount()': maritaPB.cpp:39: error: `ios' has not been declared maritaPB.cpp:39: error: `in' undeclared (first use this function) maritaPB.cpp:39: error: (Each undeclared identifier is reported only once for each function it appears in.) maritaPB.cpp:41: error: `cerr' undeclared (first use this function) maritaPB.cpp:41: error: `endl' undeclared (first use this function) maritaPB.cpp:45: error: `cout' undeclared (first use this function)
Once again, look back a few posts. I'm pretty sure Dave went over this with you. These are all std:: fixes.

Code:
maritaPB.cpp: In member function `int PhoneBook::fileCount()': maritaPB.cpp:42: error: `ifstream' undeclared (first use this function) maritaPB.cpp:42: error: (Each undeclared identifier is reported only once for each function it appears in.) maritaPB.cpp:42: error: expected `;' before "file" maritaPB.cpp:43: error: `file' undeclared (first use this function)
Same here, one std:: will take care of these.

Code:
maritaPB.cpp: In member function `void PhoneBook::getName()': maritaPB.cpp:60: warning: statement has no effect maritaPB.cpp: In member function `void PhoneBook::getLName()': maritaPB.cpp:66: warning: statement has no effect
CPP / C++ / C Code:
void PhoneBook::getName()
{
  strcpy (this ->name, name);
  
}

void PhoneBook::getLName()
{
  strcpy (this->lastname, lastname);

}
What you are doing is the same as a_string = a_string. In other words, you are trying to copy itself into itself. I think what you want to do is return a char* to the calling file. That way you can simplify to:
CPP / C++ / C Code:
char* PhoneBook::getLName()
{
  return lastname;
}
Remember to change them everywhere they appear. Since this is such a simple little bit of code I tend to leave them in the class and hope they get inlined. That makes them look like:
CPP / C++ / C Code:
char* getName() { return name; };
Inlining just means that the code within the braces replaces a function call. Don't worry about the details. It will act the same to your calling file.

Code:
maritaPB.cpp:59: error: `main' must return `int' maritaPB.cpp:59: error: return type for `main' changed to `int'
This has been discussed to death here. There is a thread in the tutorial section that explains as well as a number of other threads. Even if your compiler allows you to get away with it you should not. Just my humble opinion on the matter.

Code:
maritaPB.cpp: In function `int main()': maritaPB.cpp:62: error: `fstream' undeclared (first use this function) maritaPB.cpp:62: error: (Each undeclared identifier is reported only once for each function it appears in.) maritaPB.cpp:62: error: expected `;' before "file" maritaPB.cpp:82: error: `cin' undeclared (first use this function) maritaPB.cpp:89: error: `name' undeclared (first use this function) maritaPB.cpp:89: error: `arreglo' undeclared (first use this function) maritaPB.cpp:89: error: `i' undeclared (first use this function) maritaPB.cpp:89: error: expected primary-expression before '=' token maritaPB.cpp:98: error: `lastname' undeclared (first use this function) maritaPB.cpp:98: error: expected primary-expression before '=' token maritaPB.cpp:106: error: `file' undeclared (first use this function)
Once again, most of these will go away with the std:: fix you have been applying to the rest of the file.

Once you get to there you should have something like:
Code:
maritaPB.cpp: In function `int main()': maritaPB.cpp:90: error: `name' undeclared (first use this function) maritaPB.cpp:90: error: (Each undeclared identifier is reported only once for each function it appears in.) maritaPB.cpp:90: error: `arreglo' undeclared (first use this function) maritaPB.cpp:90: error: `i' undeclared (first use this function) maritaPB.cpp:90: error: expected primary-expression before '=' token maritaPB.cpp:99: error: `lastname' undeclared (first use this function) maritaPB.cpp:99: error: expected primary-expression before '=' token

These are all easy to fix if you look hard at it. I leave that to you. Try and fiqure out what you want to do with these lines:
CPP / C++ / C Code:
if (name, arreglo[i].getName = = 0) // error line 90
// and 
if ( lastname, arreglo[i].getLName = = 0)  // error line 99

Those are not correct. In fact, to show where their real problem lies I commented out the conditional statement and replaced it with a 0 so it would always fail. Because of the way they are written it creates an interesting problem.
CPP / C++ / C Code:
if (/* lastname, arreglo[i].getLName = = 0*/ 0)
What happens when the if fails? Well, it looks for the next statement (remember, whitespace doesn't count) and assumes that is the line to skip. Then it continues execution with the statement after that. Since the next statement (the one that will be skipped) is a break statement choosing the first option in your menu will produce this:
Code:
howmany = 27 ******************************************************************************* MENU ******************************************************************************** What would you like to do? (1) Search By Name (2) Search By Last Name (3) Exit 1 Searching By Name.... Searching By Last Name.... Exiting... ******************************************************************************* MENU ******************************************************************************** What would you like to do? (1) Search By Name (2) Search By Last Name (3) Exit 2 Searching By Last Name.... Exiting... ******************************************************************************* MENU ******************************************************************************** What would you like to do? (1) Search By Name (2) Search By Last Name (3) Exit 3 Exiting...
Not quite what you had in mind I bet. Well, if you are still working on this I wish you the best of luck. If not, perhaps this will help someone else.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:47.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.