Thread: Help with this program !

  1. #1
    gar_19
    Guest

    Help with this program !

    Hello can someone help me with my errors? I cant figure them out. There were alot more but i fixed those. (im on mandrake 9.1 using gcc)

    first here are the errors
    addressbook.cpp: In function `void StartStuff()':
    addressbook.cpp:52: invalid type argument of `unary *'
    addressbook.cpp: In function `void CreateNew()':
    addressbook.cpp:106: `Entries' undeclared (first use this function)
    addressbook.cpp:106: (Each undeclared identifier is reported only once for each
    function it appears in.)
    entry.cpp:9: parse error before `char'
    entry.cpp:12: ISO C++ forbids declaration of `home' with no type
    entry.cpp:12: `Home' was not declared in this scope
    entry.cpp:13: ISO C++ forbids declaration of `mobile' with no type
    entry.cpp:13: `Mobile' was not declared in this scope
    entry.cpp:15: `filename' was not declared in this scope
    entry.cpp:17: syntax error before `<<' token
    entry.cpp:18: syntax error before `<<' token
    entry.cpp:19: syntax error before `<<' token
    entry.cpp:21: syntax error before `.' token
    entry.cpp:25: destructors must be member functions
    entry.h
    Code:
    // the 'entry' class used for each entry
    // in the address book
    //
                                                                                    
    class entry
    {
            public:
                                                                                    
                    entry(char Name[100], int Home, int Mobile, char filename[100]);                ~entry();
                                                                                    
            protected:
                    char name[100]; // name
                    int home;   // home number
                    int mobile; // mobile number
                                                                                    
    }; // end entry class
    entry.cpp
    Code:
    // entry class constructor/destructor
    //
                                                                                                                                 
    #include "entry.h"
    #include <fstream>
                                                                                                                                 
    using namespace std; // for lame fstream include *sigh*
                                                                                                                                 
    entry(char Name[100], int Home, int Mobile, char filename[100])
    {
            name = Name;
            home = Home;
            mobile = Mobile;
                                                                                                                                 
            ofstream outfile(filename);
                                                                                                                                 
            outfile << name << endl;
            outfile << home << endl;
            outfile << mobile << endl;
                                                                                                                                 
            outfile.close();
    }
                                                                                                                                 
    ~entry()
    {
            // not too much
    }
    addressbook.cpp // main dude
    Code:
    // address book program: stores name, home and mobile phone numbers
    //
    
    
    // includes
    #include "entry.h" // isolated cause its cool
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    
    // prototypes
    void StartStuff();
    void Menu();
    void CreateNew();
    void Search();
    
    // globals
    int entries;       // number of database entries (extracted from the index.dat file)
    char *entries_str; // string version of entries integer
    
    // 
    using namespace std;
    
    // enter main dude
    int main()
    {
    	cout << "Address Book testing dude\n\n";
    	
    	StartStuff(); // get everyhting read to use the prog
    	
    	Menu(); // show menu; bla bla
    
    	cout << endl;
    	
    	return 0;
    }// end main()
    
    // start everything and get it ready
    void StartStuff()
    {
    	ifstream infile("index.dat");
    
    	infile >> entries_str;
    	entries = atoi(entries_str); // change to int + store it in 'entries'
    
    	infile.close();
    
    	entry* Entries = new entry[*entries];
    		
    }// end StartStuff()
    
    // for user to select what to do in the program
    void Menu()
    {
    	int choice = 0;
    	cout << "1. Create new entry\n"
    	     << "2. Search by name\n\n";
    
    	cout << "> ";
    	cin >> choice;
    
    	switch(choice)
    	{
    		case 1:
    			CreateNew();
    			break;
    
    		case 2:
    			Search();
    			break;
    
    		default:
    			cout << "Error. Chose a number from the menu...\n";
    			break;
    	}
    
    }// end Menu()
    
    
    // create new entry in database; this is where the magic happens
    void CreateNew()
    {
    	char filename[100];
    	char tempName[100];
    	int tempHome = 0;
    	int tempMobile = 0;
    	
    	strcpy(filename, entries_str);
    	strcat(filename, ".dat");	
    	
    	ofstream newFile(filename);
    
    	cout << "Enter the person's name: ";
    	cin >> tempName;
    	cout << "\nEnter the person's home phone number: ";
    	cin >> tempHome;
    	cout << "\nEnter the person's mobile phone numnber: ";
    	cin >> tempMobile;
    
    	entry newEntry(tempName, tempHome, tempMobile, filename); // new entry being created of class type 'entry'
    
    	Entries[entries + 1] = newEntry;
    
    }// end CreateNew()

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Addressing the first few, the syntax for dynamic allocation is:

    type* array = new type[size];

    Entries is never defined in CreateNew(), and Entries from StartStuff() is out of scope at that point.

    Class declarations are as follows:

    Code:
    class name
    {
    public:
       name();
       void f();
    };
    
    name::name() { }
    
    void name::f() { }
    That oughta give you a start, though I recommend you read up some more on implementing and working with classes.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    gar_19
    Guest
    thanks I did all that but where do I define Entries so it will be in scope when used in CreateNew() ?

    Thanks

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Could pass it as an argument to the function. Also might want to pass entries and entries_str as references to StartStuff(), because they too are not in scope in the function call.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    gar_19
    Guest
    can you show me how please? I cant seem to get it. Also can you show me how to properly write this line (nameley how to reference to 'entries' in the brackets of the array:
    Code:
    entry* Entries = new entry[*entries];
    Thanks

  6. #6
    gar_19
    Guest
    Ok, now I just need to be shown how to declare Entries/pass it as an argument so it wont come up as indefined - i fixed all the other stuff.

    Thanks

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    you might want to return entries from StartStuff() (change it to an int type first) and then declare Entries in main
    Code:
     entry* Entries = new entry[StartStuff()];
    actually not sure if that will work, you could just do something like :
    Code:
    int x=StartStuff();
    entry* Entries = new entry[x];
    i think you could then pass Entries by doing something like
    Code:
    //in Main:
     CreateNew(entry *Entries)
    
    //now the function
    CreateNew(entry &Entries)
    but I would have to test it first (heh, so I'm just a novice )
    remember to change the prototype
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM