|
-
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)
-
Jun 16th, 2002, 03:02 AM
#2
Monday Morning Lunatic
As the linker has told you, there's no implementation for the function "intro", referenced in main(). I couldn't see where the code for it was defined, did you forget to copy it over?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 16th, 2002, 11:40 AM
#3
Thread Starter
Registered User
wee.
Thanks parksie! That was it, foolish me! I was wondering, do you happen to know a way, to keep the new line character from going to the next input stream? for instance...
int age;
char type[30];
char health[100];
char sex;
char descript[100];
char location[100];
char other[100];
I wrote this for the user input:
Code:
cout <<"Type: ";
cin.getline(type,31);
cout <<"health: ";
cin.getline(health,101);
cout <<"Age: ";
cin >> age;
cout <<"Sex: ";
cin >> sex;
cout <<"Description: ";
cin.getline(descript,101);
cout <<"Location: " << endl;
cin.getline(location,101);
cout << endl;
cout <<"Other: " << endl;;
cin.getline(other,101);
and what this does in teh console window is as fallows:
Type: Health: //user input [enter]
age: //user input[enter]
sex: //user input[enter]
description: location:
cursor is now blinking below these feilds, //user input[enter]
[blank space]
Other:
curser is now blinking below these feilds, //user input[enter]
What i don't get, is why is it doing that? There was no overflow, the user never entered more than was allowed. and from the console output, it looks as if, the new line character is spilling out all over the place! '\n';
-
Jun 16th, 2002, 11:42 AM
#4
Monday Morning Lunatic
If you use getline that comes with the strings you should have no trouble:
Code:
string s;
getline(cin, s);
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 16th, 2002, 11:57 AM
#5
Thread Starter
Registered User
thats what everyone is telling me, but its not working, i have to put cin.ignore() everywhere somtimes before input somtimes after input for it to work...
they all told me to use <string> but when i try and #include <string.h> it doesn' accepted it, it says missing a ; before
string location;
-
Jun 16th, 2002, 11:58 AM
#6
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 16th, 2002, 12:09 PM
#7
Thread Starter
Registered User
for it to work, i had to use
std::string type;
std::string location;
std::etc...
that was in the declaration file, so i #included <string> there
but now in the defition file
the getline isn't working...
Code:
//animal.cpp
//definition
#include "animal.h"
#include <fstream.h>
void Animal::addRecord()
{
ofstream outfile("inventory.txt",ios::ate);
if(!outfile)
{
cout<<"Unable to write to file\n";
}
else
{
cout <<"Type: ";
//cin.ignore();
//cin.getline(type,31);
std::getline(type,31);
cout <<"health: ";
//cin.ignore();
//cin.getline(health,101);
std::getline(health,101);
cout <<"Age: ";
cin >> age;
cout <<"Sex: ";
cin >> sex;
// cin.ignore();
cout <<"Description: ";
// cin.getline(descript,101);
std::getline(descript,101);
cout <<"Location: " << endl;
// cin.getline(location,101);
std::getline(location,101);
cout <<"Other: " << endl;;
// cin.getline(other,101);
std::getline(other,101);
//output to file
outfile <<"Type: " << type
<<" Health: " << health
<<" Age: " << age
<< " Sex: " << sex
<<" Discription: " << descript
<<" Location: " << location
<<" Other: " << other << "\n\n";
}
outfile.close();
}
--------------------Configuration: Animal Project - Win32 Debug--------------------
Compiling...
animal.cpp
C:\unzipped\Animal Project\animal.cpp(37) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
C:\unzipped\Animal Project\animal.cpp(37) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_ist
ream<_E,_Tr> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [30]'
C:\unzipped\Animal Project\animal.cpp(43) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
C:\unzipped\Animal Project\animal.cpp(43) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_ist
ream<_E,_Tr> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [100]'
C:\unzipped\Animal Project\animal.cpp(55) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
C:\unzipped\Animal Project\animal.cpp(55) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_ist
ream<_E,_Tr> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [100]'
C:\unzipped\Animal Project\animal.cpp(60) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
C:\unzipped\Animal Project\animal.cpp(60) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_ist
ream<_E,_Tr> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [100]'
C:\unzipped\Animal Project\animal.cpp(64) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
C:\unzipped\Animal Project\animal.cpp(64) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_ist
ream<_E,_Tr> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [100]'
Error executing cl.exe.
Animal Project.exe - 10 error(s), 0 warning(s)
-
Jun 16th, 2002, 12:11 PM
#8
Monday Morning Lunatic
It looks like you're using the old iostream.h headers - no extension on that either.
You can put a "using namespace std;" into the source file after all the includes (but don't use it in a header).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jun 16th, 2002, 01:16 PM
#9
Thread Starter
Registered User
I tried to use using namespace std; in every file, like not at the same time, but i tried it in the animal.h, the main.cpp and the animal.cpp, what do u mean not in the header file? i tried after all the includes and i get 48 somthing errors, saying cout isn't defined and what not
-
Jun 16th, 2002, 01:19 PM
#10
Monday Morning Lunatic
Inside the individual function, you might be able to put the using namespace std;, but it depends on the compiler, I can't remember if VC6/7 support it properly.
Alternatively, you can do what they usually suggest which is std::cout << whatever << std::endl;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
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
|