|
Ok to start things off i am a complete noob at programming, been trying to learn most of it myslef. Recently i made a property managment Database for the properties i own. I want to try and upgrade this program with implementing classes and Vectors. now im not sure what to do and how to transform it, i think the struct would go in the class in a .h file and the fuctions would go in the .cpp can someone please review the code below and give me a rough indercation of what to do.
----------------------------------------------------------------
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct propertyInfo
{
string address;
string suburb;
int price;
};
void displayProperty (propertyInfo property);
void getProperty(propertyInfo &property, int &);
void displayLibrary(propertyInfo properties[], int &);
void displayFile(propertyInfo properties[], int &);
void search(propertyInfo properties[], int &);
void saveproperty(propertyInfo properties[], int &numProperties);
int displayMenu();
const int MAXPROPERTIES = 100;
int main()
{
int numProperties = 0;
propertyInfo properties[MAXPROPERTIES];
propertyInfo property = {};
displayFile(properties, numProperties);
int choice = displayMenu();
while (choice!=0)
{
switch (choice)
{
case 1: search(properties,numProperties);
break;
case 2: saveproperty(properties, numProperties);
break;
case 3: displayLibrary(properties, numProperties);
break;
case 4: getProperty(properties[numProperties], numProperties);
break;
case 5: return 0;
}
choice = displayMenu();
}
system("pause");
return 0;
}
void getProperty(propertyInfo &property, int & numProperties)
{
cout << "\nEnter address: " ;
getline(cin, property.address);
cout << "Enter suburb: ";
getline(cin, property.suburb);
cout << "Enter price: ";
cin >> property.price;
cin.ignore();
cout <<"property: " << property.address << ", " << property.suburb << ", " << property.price << " added";
numProperties++;
}
void displayProperty (propertyInfo property)
{
cout << property.price << " " << property.address << " " << property.suburb ;
}
void displayLibrary(propertyInfo properties[], int &numProperties)
{
cout << endl;
for(int t = 0; t < numProperties; t++)
{
displayProperty(properties[t]);
cout << endl;
}
}
void displayFile(propertyInfo properties[], int &numProperties)
{
string FileName = "";
string Extension = ".txt";
cout << "Enter property filename: ";
cin >> FileName;
FileName = FileName + Extension;
//Convert the sting into a char pointer in order to pass it to open()
const char * TheFile = FileName.c_str();
ifstream fin;
fin.open(TheFile, ios::in);
if(fin.is_open())
{
while (numProperties < MAXPROPERTIES && !fin.eof())
{
getline(fin, properties[numProperties].address);
getline(fin, properties[numProperties].suburb);
fin >> properties[numProperties].price;
fin.ignore(); // ignore new line character left after reading int
numProperties++;
}
fin.close();
}
else
cout << "Could not open filename: " << FileName << endl;
}
void search(propertyInfo properties[], int &numProperties)
{
int searchamount = 0;
cout << "Enter Search amount ";
cin >> searchamount;
cout << endl;
for (int z=0; z < numProperties; z++)
{
if (properties[z].price < searchamount)
{
displayProperty(properties[z]);
cout << endl;
}
}
}
int displayMenu()
{
int choice =0;
cout << "\nMenu:\n";
cout << "1 - Property Search" <<endl;
="" cout="" <<="" "2="" -="" save="" property="" list"="" <<endl;="" "3="" list="" all="" properties"="" "4="" add="" a="" property"="" <<endl;
="" "5="" exit"=""
="" cout<<="" "enter="" menu="" choice="" ";="" cin="">> choice;
cin.ignore(1);
return choice;
}
void saveproperty(propertyInfo properties[], int &numProperties)
{
string fileName = "";
string Extension = ".txt";
cout << "Enter filename: ";
cin >> fileName;
cout << "Property details saved to the file: " << fileName <
|
|
|
|
|
Hi.
You'll probably find this program more useful in the long run if you move it to a windows app instead of a console app. As for moving it to have classes, as it stands, it only needs one, the struct you've already defined, unless you want to move data access into a class perhaps.
I'd also suggest looking into using MSDE or something similar to store your data instead of a flat text file.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
what im trying to do is get rid of the struct and turn it into a class.
|
|
|
|
|
replace
struct
{
with
class
{
public:
a class and a struct are the same thing, the only difference is that a class has private as default, and a struct has public. You can add constructors and methods to a struct, like I say, there is no real difference. Of course, if you create a class in a .h and .cpp file, then as your struct has no methods, you'd put it all in the .h file, the .cpp file would be empty except the #include of the header file. If you add methods, they would be declared in the header, and their body would be in the cpp file.
I recommend getting the build in wizards to build a class and add some methods, so you can see how it all gets laid out. Basically you put the class name and a :: before the name of the method in the .cpp file, to differentiate from global methods.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
if any has the time can they please rewrite the program with the .h file and .cpp file seperate that would help heaps
|
|
|
|
|
just post the code here or send it to my email thanks
|
|
|
|
|
You mean to make the struct a class ? Or you want to seperate it further ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Yes i want to turn the struct into a class in a .h file could someone show me hows it's done its a small simple program would be very happy if someone could do it and show me the finished .h file and .cpp
|
|
|
|
|
It's exactly as I explained it yesterday
the header file:
class propertyInfo
{
public:
string address;
string suburb;
int price;
};
the .cpp file:
#include PropertyInfo.h
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I have a dialog with a start button and I want to add a listen function on it. Below is the code that I try:
void CTCPTestDlg::OnBtnstart(LPVOID lParam)
{
// TODO: Add your control notification handler code here
}
When I compile, ot occur a error:
error C2511: 'OnBtnstart' : overloaded member function 'void (void *)' not found in 'CTCPTestDlg'
I don't know how to solve it. can somebody help me
|
|
|
|
|
check your declaration in the .h file
most probably you have a different declaration there.
void CTCPTestDlg::OnBtnstart(LPVOID lParam) in .cpp
afx_msg void OnBtnstart(LPVOID lParam) in .h (if function is auto-generated)
void OnBtnstart(LPVOID lParam) in .h (if function is manually generated)
|
|
|
|
|
Hi..
how can i select excel cell by index not by name using automation? e.g. cell(1,2) for B1 cell. in VB i can use cells(1, 2) coz it's treated as an array of cells. but in VC++, i got error if i use cells[1][2] to access the cell. There is no such cells( 1, 2) method either. The method available is only Range("B2") which is using cell's name not cell's index. Help.. urgent..
Many Thanks..
mu23ye
|
|
|
|
|
I wanted to make a vb application which would dial a telephone number using
the modem. When the person on the other end lifts up the telephone (answers
the phone), the application should start playing an audio file(eg. .wav
file).
The approach I took was using the Tapi. As my modem is connected to the
serial port on my computer I send AT commands to make the phone call . This
is working fine and the call is being placed.
I wanted to know whether this is possible at all? Whether I am approaching
the wrong way? If it is possible, then please give some suggestions or
samples(vb or vc). If it is not possible , then what is the best way to do
so
|
|
|
|
|
I have no idea, but google[^] did.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
How to solve this error?
Error C2065 : 'ReadPortNo' : undeclared identifier
I got a lot of this same error but not all is ReadPortNo, there are different. I just declare it outside the function by int ReadPortNo; I don't know whether it is right or not. Can help me..
|
|
|
|
|
Where did you declare it ? It's obviously not visible to the code that tries to use it. Post some code so we can see what's going on
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
int SOCKADDR_IN; <---this is I declare when it occur the error :'SOCKADDR_IN' undeclare identifier
UINT ListenThread(LPVOID lParam)
{
HANDLE hThread;
SOCKADDR_IN SktAddr;
char szMessage[300];
wsprintf(szMessage, "(ListenThread) - Starting TCP/IP Initialisations.");
WriteSysLog(szMessage);
return 0;
}
|
|
|
|
|
1983ttj wrote:
int SOCKADDR_IN;
Did you mean to put typedef in front of this ? Or perhaps #define SOCKADDR_IN int ?
As it stands, you have a global int called SOCKADDR_IN, and you're trying to use a variable name as if it were a type.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I am a straight-up newbie to Visual C++ .net. The problem I have today is that I have typed in some code. It runs it fine. Then I make some changes, and it just continues to compile and execute the old code - even when I erase every single visible peace of code. It's driving me nuts. It just started doing this today. Is this some glitch or did I mistakenly tell it to just run the first code I compile no matter what I type. Any (and I mean any) help you all can provide would be appreciated, especially since I am very, very frustrated right now.
Thanks in advance for your help.
Sincerely,
BP
It's just a matter of time!
|
|
|
|
|
BlitzPackage wrote:
It's driving me nuts. It just started doing this today.
Try 'clean' ( which erases your old compilation files ) and then 'rebuild all'. This shouldn't happen, but that's how you fix it. If that doesn't work, go into the directory you're building in, and delete everything by hand. Then the compiler has no choice but to recreate it.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Why is it not seeing the IActiveDesktop Interface in shlobj.h. I tried using other interfaces in there, and they work fine, but the IActiveDesktop interfaces are the only ones that dont work.
When I try to compile the following code I get an error:
error C2065: 'IActiveDesktop' : undeclared identifier
#include <shlobj.h>
#include <string>
#include <wininet.h>
#include <windows.h>
#using <system.dll>
#using <mscorlib.dll>
using namespace System;
using namespace std;
int main()
{
HRESULT hr;
IActiveDesktop *pActiveDesktop;
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**)&pActiveDesktop);
...
|
|
|
|
|
Include <wininet.h> before including <shlobj.h> and make sure _WIN32_IE is defined to a value greater or equal than 0x400
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Thanks
now unfortunately the hr function
hr = CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER,
IID_IActiveDesktop, (void**)&pActiveDesktop);
is not setting up the ActiveDesktop and hr is returning failed,
how do i initialize it so that i can use pActiveDesktop?
|
|
|
|
|
Did you call CoInitialize ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Always look at the HRESULT value. Then do a look it up in the system header file called winerror.h . The header files are documented.
|
|
|
|