Thread: Help with this program !

  1. #1

    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
    // the 'entry' class used for each entry
    // in the address book
    class entry
                    entry(char Name[100], int Home, int Mobile, char filename[100]);                ~entry();
                    char name[100]; // name
                    int home;   // home number
                    int mobile; // mobile number
    }; // end entry class
    // 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;
            // not too much
    addressbook.cpp // main dude
    // 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'
    	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;
    		case 1:
    		case 2:
    			cout << "Error. Chose a number from the menu...\n";
    }// 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
    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:

    class 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.

    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
    thanks I did all that but where do I define Entries so it will be in scope when used in CreateNew() ?


  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    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
    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:
    entry* Entries = new entry[*entries];

  6. #6
    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.


  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Seattle, WA
    you might want to return entries from StartStuff() (change it to an int type first) and then declare Entries in main
     entry* Entries = new entry[StartStuff()];
    actually not sure if that will work, you could just do something like :
    int x=StartStuff();
    entry* Entries = new entry[x];
    i think you could then pass Entries by doing something like
    //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."

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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM