|
-
Jun 16th, 2002, 01:59 AM
#1
Thread Starter
Registered User
Very newbish question! proper coding style!
I just noticed i have very very bad coding styles, infact I usually just have 2 files, .cpp and a .h. I also don't use std! I have no idea how to use that, i've read 3 books, 1 which was a software engineering book, and not one used using namespace std, or std:: prefix. But anyways, I had this program working just fine, compiled with no errors, when i just had 2 files! but i wanted to break it down into 3 differnet files, animal.h, animal.cpp and main.cpp. And now the program isn't working at all, and its becuase of my #include's, so i'm going to post the whole program, its not that big, and i just need to know where to put the #includes so evertyhing links together properly, If you have to use std to get this working right, then just add it and i'll try and figure out what you did! here it is!
Code:
//animal.cpp
//definition
#include "animal.h"
#include <string.h>
#include <fstream.h>
void Animal::createDB()
{
ofstream outfile("inventory.txt",ios::out);
if(!outfile)
{
cout<<"Unable to create file\n";
}
else
{
cout<<"File successfully created\n";
outfile.close();
}
}
void Animal::addRecord()
{
ofstream outfile("inventory.txt",ios::ate);
if(!outfile)
{
cout<<"Unable to write to file\n";
}
else
{
cout <<"Type: ";
cin >> type;
cout <<"health: ";
cin >> health;
cout <<"Age: " << endl;
cin >> age;
cout <<"Sex: " << endl;
cin >> sex;
cout <<"Discription: " << endl;
cin >> discript;
cout <<"Location: " << endl;
cin >> location;
cout <<"Other: " << endl;;
cin >> other;
//output to file
outfile <<"Type: " << type
<<" Health: " << health
<<" Age: " << age
<< " Sex: " << sex
<<" Discription: " << discript
<<" Location: " << location
<<" Other: " << other << "\n\n";
}
outfile.close();
}
int Animal::menu()
{
int choice;
cout<<".::--------------------Animal Menu---------------------::.\n";
cout<<"(1) Create a Database\n";
cout<<"(2) Add a new record\n";
cout<<"(3) Delete a record\n";
cout<<"(4) Search\n";
cout<<"(5) Return to Main Menu\n";
cout<<".::----------------------------------------------------::.\n";
cin >> choice;
switch(choice)
{
case 1: createDB();
break;
case 2: addRecord();
break;
case 3: delRecord();
break;
case 4: search();
break;
case 5: return 0;
break;
default: cout <<"Error. Please enter a choice, 1-4\n";
}
menu();
return 0;
}
//*******Main Menu*****
void mainMenu()
{
Animal animal;
int choice;
cout<<".::---------------------Main Menu----------------------::.\n";
cout<<"(1) Create a Database\n";
cout<<"(2) Add a new record\n";
cout<<"(3) Delete a record\n";
cout<<"(4) Search\n";
cout<<"(5) Quit\n";
cout<<".::----------------------------------------------------::.\n";
cin >> choice;
switch(choice)
{
case 1: animal.menu();
mainMenu();
break;
case 2: animal.menu();
mainMenu();
break;
case 3: animal.menu();
mainMenu();
break;
case 4: animal.menu();
mainMenu();
break;
case 5: cout<<"thank you for using my program\n";
exit(1);
break;
default: cout <<"Error. Please enter a choice, 1-4\n";
}
mainMenu();
}
Code:
//animal.h
//declaration
#include <iostream.h>
#include <stdlib.h>
class Animal
{
public:
int menu();
void createDB();
void addRecord();
void delRecord() { cout<<"delete record function\n"; } //working on
void search() { cout <<"search record function\n"; } //working on
void quit() { exit(1);}
private:
int age;
char type; //should be char array
char health;
char sex;
char discript; //not used yet
char location; //not used yet
char other; //not used yet
};
Code:
//main.cpp
#include "animal.h"
//function prototypes
void intro();
void mainMenu();
int main()
{
intro();
mainMenu();
return 0;
}
errors:
--------------------Configuration: Animal Project - Win32 Debug--------------------
Linking...
main.obj : error LNK2001: unresolved external symbol "void __cdecl intro(void)" (?intro@@YAXXZ)
Debug/Animal Project.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Animal Project.exe - 2 error(s), 0 warning(s)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|