Thread: ok im stuck...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    ok im stuck...

    Hi, I have been studying the C++ fundamentals for about a year now... off and on... and I just recently started again.
    Currently, before I stopped reading on C++ I wrote a simple project that was a class test type program that used my personal Phonebook as a class, and each person in my phonebook as the objects.
    Now, after completing this successfully, I wanted to make a program that either reads in the data file "Phonebook.txt" from the projects directory to display the current entries in the phonebook, or if there is no file there, be able to write a formatted data file with entries that are created in the program. My brain hasn't been focused recently....haha
    here is the code so far... I dont have all the functions that I will be using... I just want to know if I am handling a file correctly... and if there isnt a file... if i am creating it correctly.
    Code:
    #include <fstream.h>
    #include "PBEntry.h"
    
    const string UniversalFileName = "PhoneBook.txt";
    
    void WriteEntries(PBEntry *Entry, ofstream *oFile)
    {
         oFile<<"\t\tListing:\t\t"<<Entry.getNum()<<endl;
         oFile<<"\t\tName:\t\t\t"<<Entry.getName()<<endl;
         oFile<<"\t\tHome Phone:\t\t"<<Entry.getHPhone()<<endl;
         oFile<<"\t\tCell Phone:\t\t"<<Entry.getCPhone()<<endl;
         oFile<<"\t\tAddress:\t\t"<<Entry.getAddress()<<endl<<endl;
    }
    
    void HandleFile()
    {
         ifstream *File;
         File.open(UniversalFileName);
         
         if(!File)
         {
               cout<<"ERROR: Data File \"PhoneBook.txt\" could not be opened or found!"<<endl;
               cout<<"Create a new file named Phonebook.txt?(Y or N): ";
               char answer;
               cin>>answer;
               cout<<"\n";
               if(answer=='y' || answer=='Y')
               {
                              ofstream nFile(UniversalFileName);
                                       if(!nFile.is_open())
                                       {
                                          cout<<"File could not be created! Press Enter to exit the program...."<<endl;
                                          cint.get();
                                          return 0;
                                       }
                              nFile.close(); // File Created....
               }
               else if(answer=='n' || answer=='N')
               {
                    cout<<"\nThis program can go no further without a file. Press Enter to end the program..."<<endl;
                    cin.get();
                    return 0;
               }                                   
         }
         
         string *fEntryNum;
         string *fEntryName;
         string *fEntryHPhone;
         string *fEntryCPhone;
         string *fEntryAddress; 
         
         while(!File.eof())
         {
                          getline(File, &fEntryNum, '\t'); 
                          getline(File, &fEntryName, '\t');
                          getline(File, &fEntryHPhone, '\t');
                          getline(File, &fEntryCPhone, '\t');
                          getline(File, &fEntryAddress);
         }
    
    int main()
    {
        
    }
    if i am doing this incorrectly... please let me know... i havnt even finished the handle file function cause im stuck...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> #include <fstream.h>
    The first thing to change is this. <fstream.h> is outdated and non-standard. Switch to <fstream>. You'll have to specify the std namespace for the standard library names you use, like ifstream, ofstream, cout, cin, etc. I prefer to put std:: in front of each name, but an easier way would be to put using namespace std; at the top of the file under the includes.

    You should also be including <string> and <iostream> since you use those in this file (and any other necessary headers) even if they are included elsewhere.

    >> getline(File, &fEntryNum, '\t');
    getline takes a string, not a strig pointer or address of a string pointer. Make all your strings plain strings and pass them and not their addresses to the getline function.

    You are going to need some sort of data structure to hold each entry. This will take a little bit of thought. I'd recommend making a struct out of all your strings and then using a vector to hold instances of that struct.

    Those are just a few issues I see at first glance.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You're learning "old" C++, which uses char* for strings, and <iostream.h> which somehow got into the C++ library in the first place. I think you should go re-learn some parts of the C++ library again. from more up-to-date sources, so you can use the cstdlib better.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    oo sorry... i should have included the "PBEntry.h" in the post
    thank you for your replies... any criticism is appreciated.

    Here is my new coded .cpp file after thinking about it. Not done yet cause I still gotta add code that counts how many entries are stored in the "Phonebook.txt", if any, and then I still have to make a menu function for the program.

    PBEntry.cpp

    Code:
    #include <fstream.h>
    #include "PBEntry.h"
    
    const string UniversalFileName = "PhoneBook.txt";
    
    void CreateEntry(int *EntryNum,string *EntryName,string *EntryHPhone,string *EntryCPhone,string *EntryAddress)
    {
         PBEntry Entry = new PBEntry(&EntryNum,&EntryName,&EntryHPhone,&EntryCPhone,&EntryAddress);
    }
    
    void ReadFileContents(ifstream *iFile)
    {
         int i=0;
         PBEntry Entries[];
         string fEntryNum;
         string fEntryName;
         string fEntryHPhone;
         string fEntryCPhone;
         string fEntryAddress;
         
         while(getline(&iFile, fEntryAddress) != NULL)
         {
                          cout<<"\nCurrently on Entry "<<i+1<<"."<<endl;  
                          getline(&iFile, fEntryNum, '\t'); 
                          getline(&iFile, fEntryName, '\t');
                          getline(&iFile, fEntryHPhone, '\t');
                          getline(&iFile, fEntryCPhone, '\t');
                          getline(&iFile, fEntryAddress);
                          
                          Entries[i++]=CreateEntry(fEntryNum,fEntryName,fEntryHPhone,
                                      fEntryCPhone,fEntryAddress);
         }
    }
    
    void WriteEntries(PBEntry *Entry, ofstream *oFile)
    {
         oFile<<Entry.getNum()<<"\t"<<endl;
         oFile<<Entry.getName()<<"\t"<<endl;
         oFile<<Entry.getHPhone()<<"\t"<<endl;
         oFile<<Entry.getCPhone()<<"\t"<<endl;
         oFile<<Entry.getAddress()<<endl<<endl;
    }
    
    void CreateOutFile()
    {
                     ofstream nFile(UniversalFileName);      // Im creating a file if there is not one currently available
                             if(!nFile.is_open())
                             {
                                cout<<"File could not be created! Press Enter to exit the program...."<<endl;
                                cint.get();
                                return 0;
                             }
                     nFile.close(); // File Created....
    }
    
    void HandleFile(ifstream *iFile)
    {
         
         if(!iFile)
         {
               cout<<"ERROR: Data File \"PhoneBook.txt\" could not be opened or found!"<<endl;
               cout<<"Create a new file named Phonebook.txt?(Y or N): ";
               char answer;
               cin>>answer;
               cout<<"\n";
               if(answer=='y' || answer=='Y')
               {
                    CreateOutFile();
               }
               else if(answer=='n' || answer=='N')
               {
                    cout<<"\nThis program can go no further without a file. Press Enter to end the program..."<<endl;
                    cin.get();
                    return 0;
               }                                   
         }
         ReadFileContents(&iFile);
    }
         
    
    
    int main()
    {
         ifstream *iDataFile;
         ofstream *oDataFile;
         iDataFile.open(UniversalFileName);
         HandleFile(&iDataFile);
    }
    PBEntry.h

    Code:
    #include <iostream>
    using namespace std;
    
    class PBEntry
    {
          public:
                 
                 PBEntry(int eNum, string eName, string eHPhone,
                         string eCPhone, string eAddress)
                 {
                         EntryNum      = eNum;
                         EntryName     = eName;
                         EntryPhone[0] = eHPhone;
                         EntryPhone[1] = eCPhone;
                         EntryAddress  = eAddress;
                 };
                 ~PBEntry() {};
                 
                 int getNum() {return EntryNum;}
                 string getName() {return EntryName;}
                 string getHPhone() {return EntryPhone[0];}
                 string getCPhone() {return EntryPhone[1];}
                 string getAddress() {return EntryAddress;}
                 
          protected:
                    
                    int EntryNum;
                    string EntryName;
                    string EntryPhone[2];
                    string EntryAddress;
    };

    Quote from Daved:
    You are going to need some sort of data structure to hold each entry. This will take a little bit of thought. I'd recommend making a struct out of all your strings and then using a vector to hold instances of that struct.
    Would this be easier since I have no derived classes from the original class structure "PBEntry"?

    And jafet, the link you provided is broken, and I was learning from the book "C++ Programming in Easy Steps: Conforms to ANSI and ISO Standards". If this reference book is not up to date, I was thinking about getting a C++ Primer book. I am not familiar with the editions though..which one would meet my requirements for being a new programmer?...Havnt really checked in on it.

    And Salem, the book I was learning from uses while(!file.eof()). Is this not good practice? I think I understand the link reference. Use the return value of getline to see if the end of the file has been reached?
    Ex.
    Code:
         while(getline(&iFile, fEntryAddress) != NULL)
         {
                          cout<<"\nCurrently on Entry "<<i+1<<"."<<endl;  
                          getline(&iFile, fEntryNum, '\t'); 
                          getline(&iFile, fEntryName, '\t');
                          getline(&iFile, fEntryHPhone, '\t');
                          getline(&iFile, fEntryCPhone, '\t');
                          getline(&iFile, fEntryAddress);
                          
                          Entries[i++]=CreateEntry(fEntryNum,fEntryName,fEntryHPhone,
                                      fEntryCPhone,fEntryAddress);
         }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> C++ Primer book
    Get the 4th edition if you get this book (by Lippman, Lajoie and Moo). Accelerated C++ might also be better for you because it teaches modern C++ and you are already knowledgable enough to handle its accelerated nature.

    >> the book I was learning from uses while(!file.eof()). Is this not good practice?
    It probably doesn't make a difference in this specific case (because the last call to getline will reach the end-of-file before the loop continues), but it is not good practice in other cases because eof() might return false even when there is no more data left in the file. Putting the getline into the while control is the right idea, but you should put the part that reads in the fEntryNum since that is the first part of each record. It will be stored properly if the read is successful.

    >> PBEntry Entries[];
    That doesn't do what you think it does. This is why I would use a vector. You don't know at compile time how many entries you will read in. You don't even know how many they'll be while you're reading them in, so by using a vector you can just keep adding them on to the end of the container one at a time until you are done. If you use a C style array, you have to make sure you allocate enough space to fit all of the entries in the file.

    >> &iFile
    If iFile is a pointer, then you should be using *iFile to dereference that pointer and pass it to getline.

    You should really be going slower and compiling as you go. You have a lot of compile errors and logic errors that will be difficult to fix if you wait until the end. If you just start with the PBEntry class and a simple main function that writes out one entry or reads in one entry or gets the information for one entry from the user, then you can test just those parts to make sure they work before moving on.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    wow... thank you man...
    I think I'll just discard the above code right now, and start a little simpler and slower.
    I wasn't to sure what I was doing when I started the code, lol, not good practice... i know...
    Well you will see me posting... in the near future.
    Thanks again.
    And with the vectors, I'm not to sure how to use them.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You just have to find a good book or reference to know how to use vectors. In general, you should use them instead of arrays in C++, and in your case (where you don't know the size beforehand) they are even more useful. If you do get C++ Primer (by the authors I mentioned) or Accelerated C++, they will have the information you need to get used to them. Overall, they are actually easier to use than C style arrays.

    Keep a copy of the code you have just for reference, but I think starting again a little slower is a great idea. Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM