GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Apr-2005, 10:55
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road

C++ PhoneBook


I have to write a main program in which I read from a file phonebook.txt into a dynamic array of Phonebook objects and assign data to each.

Using a loop display each object data. Prompt the user to select a number on the array to modify a Phonebook 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 assign 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.

This is what i have so far...

CPP / C++ / C Code:

#include <fstream.h>	// libraries


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;


};


void main()
{
	fstream book;


	book.open("phonebook.txt", ios::in);
	if (!book)
		cout << "ERROR! opening phonebook.txt" << endl;
	book >> FirstName >> LastName >> AreaCode >> PhoneNumber;


	Phonebook *array=NULL, objeto;
	array = new Phonebook[5];

	for (int i=0; i<= 5; i++)


}




I'm really stuck there. I have no clue how to read the file into a dynamic array of Phonebook objects and assign data to them.

I know firther questions will appear as I go deeper in it. SO I would really appreciate any help.

This is for next Monday.

Thanks a lot and have a nice/happy weekend!
  #2  
Old 29-Apr-2005, 11:56
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
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
Quote:
Originally Posted by marita
im really stuck there. i have no clue how to read the file into a dynamic array of Phonebook objects and assign data to them.

A few things. What are the errors/warnings your compiler is telling you about? I found a number of things that need to be done before you can even try to read your info in. For example after copying your code and compiling I got the following:

Code:
$ g++ -Wall phone_book.cpp -s -o PhoneBook In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.1/../../../../include/c++/3.4.1/backward/fstream.h:31, from phone_book.cpp:1: /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. phone_book.cpp:16: error: expected identifier before '&' token phone_book.cpp:16: error: ISO C++ forbids declaration of `PhoneNumber' with no type phone_book.cpp:17: error: `void Phonebook::operator=(const Phonebook&, const Phonebook&, const Phonebook&, con st Phonebook&)' must take exactly one argument phone_book.cpp:26: error: `main' must return `int' phone_book.cpp:26: error: return type for `main' changed to `int' phone_book.cpp: In function `int main(...)': phone_book.cpp:30: error: `ios' has not been declared phone_book.cpp:30: error: `in' undeclared (first use this function) phone_book.cpp:30: error: (Each undeclared identifier is reported only once for each function it appears in.) phone_book.cpp:32: error: `cout' undeclared (first use this function) phone_book.cpp:32: error: `endl' undeclared (first use this function) phone_book.cpp:33: error: `FirstName' undeclared (first use this function) phone_book.cpp:33: error: `LastName' undeclared (first use this function) phone_book.cpp:33: error: `AreaCode' undeclared (first use this function) phone_book.cpp:33: error: `PhoneNumber' undeclared (first use this function) phone_book.cpp:42: error: expected primary-expression before '}' token phone_book.cpp:42: error: expected `;' before '}' token

Now, a few of these just need to be addressed. You need to have your code even "see" things like ios, in, cout, endl, FirstName, LastName, AreaCode and PhoneNumber.

Computers are dumb, you need to help them. I will help with this little (very very little actually) bit of info.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>  // libraries
#include <ios>
using std::cout;
using std::fstream;
using std::ios;
using std::endl;

or, if you prefer:
Code:
#include <iostream> #include <fstream> // libraries #include <ios> using namespace std;

Now, the next section needs more info from you.
CPP / C++ / C Code:
book >> FirstName >> LastName >> AreaCode >> PhoneNumber;

What exactly do you want to do here? Would you like to get user input for each of the variables? Let us know and we may be able to help.

BTW, I was able with small amounts of effort get the compiler errors to...
Code:
$ g++ -Wall phone_book.cpp -s -o PhoneBook phone_book.cpp: In function `int main()': phone_book.cpp:38: error: `FirstName' undeclared (first use this function) phone_book.cpp:38: error: (Each undeclared identifier is reported only once for each function it appears in.) phone_book.cpp:38: error: `LastName' undeclared (first use this function) phone_book.cpp:38: error: `AreaCode' undeclared (first use this function) phone_book.cpp:38: error: `PhoneNumber' undeclared (first use this function)

Look your code over, make some changes and post your current state of affairs.

One last thing.
Quote:
Originally Posted by marita
This is for next Monday.

Guess what, don't care, don't care, don't care. I like to help but if you are after solutions without a learning process you came to the wrong place. If you are willing to do the work and learn along the way you are really really at the right place. Your deadlines have nothing to do with the voluntary help you can find here. Clean up your code a bit, and post your next set of questions. Myself or someone will most likely let you know how to take the next step.

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
  #3  
Old 02-May-2005, 08:06
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Hello,
Thank you so much....

Quote:
Now, the next section needs more info from you.
CPP / C++ / C Code:
book >> FirstName >> LastName >> AreaCode >> PhoneNumber;

What else does that needs?
i really have no clue.
This is giving me such a hard time...
  #4  
Old 02-May-2005, 08:18
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
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
Quote:
Originally Posted by marita
Hello,
Thank you so much....



What else does that needs?
i really have no clue.
This is giving me such a hard time...

Well, you made it here, asked questions and used code tags. I think you have at least some clue.

To start, were you able to correct some of the things I showed you to get your errors down to the undeclared variables. I see what you are trying to do (well I think I do anyhow) but you need to get to a point that you can begin compiling even if it has runtime problems.

So, post your new set of error codes and update your code. Post it here with a direct question/inquiry and we can move further on your problem with the program.

Mark

EDIT: one last thing. Can you post your phonebook.txt and your implementation file. It may be helpful.
__________________
"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
  #5  
Old 02-May-2005, 10:00
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Yeah, i think i might be able to do what you told me, i have a class now, so as soon as i get off there, i will come back here and hopefully, you'll help me.
And i can show how's my code and everything.
Thanks a lot!
Wait for me....
*haha*
  #6  
Old 03-May-2005, 07:47
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Hello there!
I was able to fix some errors but still have 7 errors!
What else do you think i need to do; here are the errors showing:

Code:
PhoneBook.cpp C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(26) : error C2059: syntax error : '&' C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(27) : error C2804: binary 'operator =' has too many parameters C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(43) : error C2065: 'FirstName' : undeclared identifier C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(43) : error C2065: 'LastName' : undeclared identifier C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(43) : error C2065: 'AreaCode' : undeclared identifier C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(43) : error C2065: 'PhoneNumber' : undeclared identifier C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(56) : error C2143: syntax error : missing ';' before '}' C:\Documents and Settings\ee181\Desktop\PhoneBook.cpp(56) : warning C4508: 'main' : function should return a value; 'void' return type assumed Error executing cl.exe. PhoneBook.obj - 7 error(s), 1 warning(s)

HELP! I'm desperate!
  #7  
Old 03-May-2005, 11:18
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
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
Most of the problem is due to you trying to get at a PhoneBook member with no object. When you have trouble, post changes to your code as well so we know what you are referring to. Here is a stripped down example of your code to get you in the right direction.

CPP / C++ / C Code:
#include <fstream>
#include <iostream>
#include <ios>
#include <string>

using std::string;
using std::cout;
using std::endl;
using std::fstream;
using std::ios;

class Phonebook
{
public:
  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
  {
    cout << "Here is your array" << endl << endl;

    cout << "Last" << '\t' << "First" << '\t' << "Phone number" << endl;
      cout << LastName << '\t' << FirstName << '\t'
           << AreaCode << "-" << PhoneNumber<<endl;

    cout << endl;
  };
};


int main()
{
  fstream book;
  Phonebook array[5];
  string input;

  book.open("phonebook.txt", ios::in);
  if (!book){
    cout << "ERROR! opening phonebook.txt" << endl;
    return -1; // so we don't print junk
  }else {
      for (int i=0; i< 5; i++){
        getline(book, input);
        strcpy(array[i].FirstName, input.c_str());
        getline(book, input);
        strcpy(array[i].LastName, input.c_str());
        getline(book, input);
        strcpy(array[i].AreaCode, input.c_str());
        getline(book, input);
        strcpy(array[i].PhoneNumber, input.c_str());
      }
  }
  array[0].print();
  array[1].print();
  array[2].print();
  array[3].print();
  array[4].print();
  
  book.close();

  return 0;
}

This is just a tossed together example to get you on your way. Once you can compile and run you can try to get the progam to work the way you would like it to. I had to make up my own textfile and just put a single entry on a line (then used getline). I just used strcpy because I am lazy and have to get back to work.

Mark

Please, ask very specific questions on things you don't understand. It will help someone here actually be helpful. I have reduced your class to just about nothing. Get the print funtion to work then you can worry about the operators later.

Fake phonebook.txt
Code:
First1 Last1 111 2222222 First2 Last2 222 3333333 First3 Last3 333 4444444 First4 Last4 444 5555555 First5 Last5 555 6666666
__________________
"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
  #8  
Old 03-May-2005, 13:22
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Hi!
I havent been able to runthe code you gave me because is gives me the following error:

phonebookhelp.cpp
C:\Documents and Settings\ee181\Desktop\phonebookhelp.cpp(52) : error C2065: 'getline' : undeclared identifier
Error executing cl.exe.

phonebookhelp.obj - 1 error(s), 0 warning(s)
What can i do to fix that?

Thanks...
  #9  
Old 03-May-2005, 13:27
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
CPP / C++ / C Code:
using std::getline;
  #10  
Old 03-May-2005, 13:27
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline
Junior Member
 
Join Date: Apr 2005
Location: Arizona
Posts: 35
Stack Overflow will become famous soon enough
Hello,

You could attempt to use a std:: namespace prefix on the getline() calls:

CPP / C++ / C Code:
std::getline(book, input);


- Stack Overflow
__________________
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [C] / [C++] tags. Your question may have been asked before, try the search facility.
 

Recent GIDBlogNARMY 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 20:58.


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