|
You need to read up on Winsock. There are some articles here. As far as discovering other servers for a game environment etc. It depends on how each server is set up. For example, if the server is always listening for a connection on a particular port, it may be as simple as broadcasting a message to all IPs on that port and see which ones respond. Of course, there should already be an established protocol on how a game running on a client computer would do this.
But, sending a broadcast message is a connectionless datagram protocol such as UDP. Usually, you would send the message to IP broadcast address of 255.255.255.255 or your IP's subnet as xxx.xxx.xxx.255. If you read up on how to send a UDP network message, its simply a matter of putting the broadcast address as your destination address (the PC you're sending the message to).
|
|
|
|
|
If I use UDP to broadcast, Do I need to use the "setsockopt" function to set the "SO_BROADCAST" flag?
Aidman » over and out
|
|
|
|
|
Yes, you do. Here is a sample program I wrote to perform broadcast. It works on both Linux, Posix (for our embedded system), and Windows. For Windows, you need to #define WINMACHINE:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#ifdef WINMACHINE // for Windows
#include <windows.h>
#include <winsock.h>
#include <wincon.h>
#include <conio.h>
#else // for unix/POSIX
#include <machine/mpc823.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <pthread.h>
#include <netinet/udp.h>
#endif
#define TXOUTBUFSZ 10000 // Output buffer size
#define RADAR_PORT 44068 // IP/UDP Port number for trans/rcv
#ifdef WINMACHINE // for Windows
WSADATA wsaData;
SOCKET sock;
SOCKADDR_IN dst_addr, src_addr;
int dst_addr_len = sizeof(dst_addr);
#else // for unix/POSIX
int sock;
struct sockaddr_in dst_addr, src_addr;
#endif
int rc;
int main()
{
char sampleMsg[132];
int optval = 1;
int optlen = sizeof(int);
dst_addr.sin_family = AF_INET;
dst_addr.sin_port = htons(RADAR_PORT);
dst_addr.sin_addr.s_addr = INADDR_BROADCAST;
strcpy(sampleMsg, "This is a test sample msg from the box. ");
#ifdef WINMACHINE // for Windows
rc = WSAStartup (MAKEWORD (1, 0), &wsaData);
if(rc != 0)
{
printf("Error starting WinSock\n");
goto error;
}
sock = socket(AF_INET,SOCK_DGRAM, 0);
if(sock == INVALID_SOCKET)
{
printf("Error creating receive socket\n");
goto error;
}
rc = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&optval, optlen);
if(rc == SOCKET_ERROR)
{
printf("Error setting up receive buffer\n");
goto error;
}
while(!kbhit())
{
printf("Sending data: %s Any key to quit.\n", sampleMsg);
rc = sendto(sock, sampleMsg, strlen(sampleMsg) + 1, 0, (struct sockaddr*) &dst_addr, sizeof(dst_addr));
if(rc == SOCKET_ERROR)
{
printf("Couldn't send data.\n");
goto error;
}
Sleep(1000);
}
closesocket(sock);
goto finish;
#else // for unix/POSIX
sysInit();
startNetwork();
if (setuid(0)== -1)
printf("setuid not eq root");
if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("Network socket initialization failed.\n");
fflush(stdout);
goto error;
}
rc = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&optval, optlen);
if(rc)
{
printf("Error setting up receive buffer\n");
goto error;
}
src_addr.sin_family = AF_INET;
src_addr.sin_port = htons(RADAR_PORT);
src_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock,(struct sockaddr*) &src_addr,sizeof (src_addr)) == -1)
printf("bind failed \n");
while(1)
{
printf("Sending data: %s Any key to quit.\n", sampleMsg);
if(sendto(sock, sampleMsg, strlen(sampleMsg) + 1, 0, (struct sockaddr*) &dst_addr, sizeof(dst_addr)) < 0)
{
printf("Error: could not send data. \n");
fflush(stdout);
goto error;
}
sleep(1);
}
goto finish;
#endif
finish:
printf("Done.\n");
return 0;
error:
#ifdef WINMACHINE
printf("\nsock Error in main(): %ld Hit any key\n", WSAGetLastError());
#else
printf("\nsock Error in main(): %ld Hit any key\n", errno);
#endif
getchar();
return 1;
}
|
|
|
|
|
GREAT THANKS!!!
really cool code =)
Aidman » over and out
|
|
|
|
|
Sorry, just one more question...
Can I use the same socket to receive broadcast messages?
Aidman » over and out
|
|
|
|
|
Yep. Set up a in_addr structure and a receive buffer to receive data from the specified port and set the IP address you're receiving from to INADDR_ANY. When you do a receive you have to do a bind() first (during the initialization). Should be plenty of examples around.
|
|
|
|
|
Ok thanks very much!
I am very sorry, could I ask a last another question?
How do you create a no blocking/waiting socket for recveiving?
So that if there is no incoming data available for the socket at the receiving function "recvfrom" it would continue instead of waiting for incoming data. Can you make the socket nonblocking or must you make the recievning function nonblocking? Is it possible to do this without having multithreads or network events?
Aidman » over and out
|
|
|
|
|
The short answer: Yes, you can make a socket blocking or non-blocking. Better answer: Get this book, "Network Programming for Microsoft Windows", Second Edition, Anthony Jones & Jim Ohlund. I got it off of half.com for $15 or $20 bucks. Well worth it especially since it retails for $59.00
Or, read up more on Winsock and sockets.
|
|
|
|
|
Is it possible to change the image size (higth/width) the the Image Class?
If so what is the best way to do so?
|
|
|
|
|
where did you find this "Image Class" ?
|
|
|
|
|
This is the link to where i found the Image class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/GDIPlusReference/Classes/ImageClass/ImageConstructors/Image_28stream_useEmbeddedColorManagement.asp
|
|
|
|
|
It is possible using CImage class in MFC !!!!
|
|
|
|
|
Dear all,
The only reason I posted the whole code was to give one the chance to run the program and see in practice the problem.
But I guess that is too much.
Anyway,
The implementation of the two functions is:
[code]
//It returns the member.
Member* AssociationList<book,member>::get_member(Book* book)
{
Member* member=0;
bool searching=true;
int index=0;
while(searching)
{
if (this->association_list[index])
if (this->association_list[index]->linked_book()==book)
{
member=this->association_list[index]->linked_member();
searching=false;
}
else
index++;
else
index++;
if (searching && (index == LIST_SIZE))
{
searching=false;
}
}
return member;
}
[/code]
[code]
//it returns the book
template<class book,class="" member="">
Book* AssociationList<book,member>::get_book(Member* member)
{
Book* book=0;
bool searching=true;
int index=0;
while(searching)
{
if (this->association_list[index])
if (this->association_list[index]->linked_member()==member)
{
book=this->association_list[index]->linked_book();
searching=false;
}
else
index++;
else
index++;
if (searching && (index == LIST_SIZE))
{
searching = false;
}
}
return book;
}
[/code]
The template file that I use to display the books is:
[code]
template<class object="">
void List<object>::displayElement(char* type)
{
if (num_elements == 0)
cout<<"No "<<type<<" is="" found="" in="" the="" "<<type<<"="" array.\n";
="" else
="" for(int="" element="0;" element<this-="">num_elements; element++)
{
cout<<'\n';
this->element_list[element]->display(association_list.get_member(element_list[element]));
}
}
[/code]
But it does not work for Members. I've tried to use the recommendations you've told me but it seems not to work.
The program works ok, when I call the functions:
book1.addElement("Books"); and book1.displayElement("Books");
to add a book and to display them respectively.
But when I use the :
member1.addElement("Member"); and member1.displayElement("Member"); it dispalys the problem:
The compiler complains at the point:
this->element_list[element]->display(association_list.get_member(element_list[element])); saying that : get_member: cannot convert parameter one from Class Member* to class Book*.
Can you correct any mistakes or suggest any solutions?
Please if you make any suggestion adjust them to my code, if possible.
Best Regards,
grscot
|
|
|
|
|
Below the message editing area you will see a Formatting bar. You need to select all of your code and then click on "pre" and "code" to make it readable. Untill you start doing that your code examples are useless.
Also short to the point snippets of code are far better than long winded examples.
Neville Franks, Author of ED for Windows. www.getsoft.com
Make money with our new Affilate program
|
|
|
|
|
...however, you will have to turn your less than and greater than into HTML escapes, and avoid any smiley combination...
unless things changed since I last tried to post code
"Der Geist des Kriegers ist erwacht / Ich hab die Macht" StS
sighist | Agile Programming | doxygen
|
|
|
|
|
peterchen wrote:
...however, you will have to turn your less than and greater than into HTML escapes, and avoid any smiley combination...
unless things changed since I last tried to post code
Yes you're right. The Formatting bar has options for this too. ie. < and >
Neville Franks, Author of ED for Windows. www.getsoft.com
Make money with our new Affilate program
|
|
|
|
|
I am using the GetCurrentImage function from the IBasicVideo Interface in DirectX like in the example below.
char *buffer = new char[size];
hr = pBV->GetCurrentImage(&size, (long*)buffer);
the buffer is meant to contain the image data from the video stream.
How Can I place these in the IStream Interface, so that i can then pass the IStream in the Image class from GDI+
Any help would be great
|
|
|
|
|
Hello All.
I currently have a need to add in a color palette (for picking custom colors) in my application. I have a simple color palette working, but I know there must be a much better way of doing it, in addition, a much nicer looking one.
Anyone have any pointers on where to look for this kind of information?
Thanks.
-stv
|
|
|
|
|
http://www.codeproject.com/miscctrl/
Trust in the code Luke. Yea right!
|
|
|
|
|
Dear all,
The concept behind the code is:
There is an associationlist that holds association pointers. Each association pointer has two pointers, that point to a book and to a member object respectively.
Now what I'm trying to do is:
To have the association pointer to use its two pointers to point to an array of books(booklist, up to 10 books) and to an array of members (memberlist, up to 10) respectively.
If you copy and paste the code the program works ok only for books(adding a new book to the booklist(up to 3 for test purposes) and displaying the books).
If one wants to add a member and display the members, they have to use the function: get_book(); in the file:
template<class Object>
void List<Object>::displayElement(char* type) of the List.cpp, and also to put in comments the //book1.displayElement("Books"); of the main.cpp file.
If I don't use the comments in the main.cpp file and use the get_book() function then an error will occur: " 'get_book':cannot convert parameter 1 from 'class Book*' to 'class Member*' "
Could someone suggest a solution using a single template function, for displaying members and books?
The code of each file is:
<br />
#ifndef _ASSOCIATION<br />
#define _ASSOCIATION<br />
<br />
template<class Book,class Member><br />
class Association<br />
{<br />
public:<br />
Association(Book* book, Member* member);<br />
<br />
Book* linked_book(){return this->book;}<br />
<br />
Member* linked_member(){return this->member;}<br />
<br />
private:<br />
Book* book;<br />
Member* member;<br />
};<br />
#endif<br />
<br />
#ifndef _ASSOCIATIONCPP<br />
#define _ASSOCIATIONCPP<br />
#include "Association.h"<br />
<br />
template<class Book,class Member><br />
Association<Book,Member>::Association(Book* book, Member* member)<br />
{<br />
this->book=book;<br />
this->member=member;<br />
}<br />
#endif<br />
<br />
#ifndef _ASSOCIATIONLIST<br />
#define _ASSOCIATIONLIST<br />
#include "Association.h"<br />
<br />
const int LIST_SIZE=100;<br />
<br />
template<class Book,class Member><br />
class AssociationList<br />
{<br />
public:<br />
AssociationList();<br />
<br />
<br />
Member* get_member(Book* book);<br />
<br />
<br />
Book* get_book(Member* member);<br />
<br />
template<class R,class P><br />
R* get_member(P* pArg);<br />
<br />
<br />
bool link(Book* book,Member* member);<br />
<br />
<br />
bool unlink(Book* book,Member* member);<br />
private:<br />
Association<Book,Member>* association_list[LIST_SIZE];<br />
};<br />
#endif<br />
<br />
#ifndef _ASSOCIATIONLISTCPP<br />
#define _ASSOCIATIONLISTCPP<br />
#include <iostream><br />
#include "Association.h"<br />
#include "Association.cpp"<br />
#include "AssociationList.h"<br />
#include "Book.h"<br />
#include "Member.h"<br />
using namespace std;<br />
<br />
template<class Book,class Member><br />
AssociationList<Book,Member>::AssociationList()<br />
{<br />
cout<<"AssociationList constructor called\n";<br />
int index;<br />
<br />
for(index=0; index<LIST_SIZE; index++)<br />
this->association_list[index]=0;<br />
}<br />
<br />
<br />
<br />
<br />
template<class Book,class Member><br />
<br />
Member* AssociationList<Book,Member>::get_member(Book* book)<br />
{<br />
Member* member=0;<br />
bool searching=true;<br />
int index=0;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_book()==book)<br />
{<br />
member=this->association_list[index]->linked_member();<br />
searching=false;<br />
}<br />
else<br />
index++;<br />
else<br />
index++;<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching=false;<br />
}<br />
}<br />
return member;<br />
}<br />
<br />
template<class Book,class Member><br />
Book* AssociationList<Book,Member>::get_book(Member* member)<br />
{<br />
Book* book=0;<br />
bool searching=true;<br />
int index=0;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_member()==member)<br />
{<br />
book=this->association_list[index]->linked_book();<br />
searching=false;<br />
}<br />
else<br />
index++;<br />
else<br />
index++;<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching = false;<br />
}<br />
}<br />
return book;<br />
}<br />
<br />
template<class Book,class Member><br />
bool AssociationList<Book,Member>::link(Book* book,Member* member)<br />
{<br />
bool searching=true;<br />
bool success=true;<br />
int index=0;<br />
int free_slot;<br />
<br />
while(searching)<br />
{<br />
if(this->association_list[index])<br />
if((this->association_list[index]->linked_book()==book ||<br />
(this->association_list[index]->linked_member()==member)))<br />
{<br />
searching=false;<br />
success=false;<br />
}<br />
else<br />
index++;<br />
else<br />
{<br />
free_slot=index;<br />
index++;<br />
}<br />
if(searching&&(index == LIST_SIZE))<br />
searching=false;<br />
}<br />
if (success)<br />
this->association_list[free_slot]=new Association(book,member);<br />
return success;<br />
}<br />
<br />
template<class Book,class Member><br />
bool AssociationList<Book,Member>::unlink(Book* book,Member* member)<br />
{<br />
bool searching=true;<br />
bool success=true;<br />
int index=0;<br />
int link_slot;<br />
<br />
while(searching)<br />
{<br />
if (this->association_list[index])<br />
if (this->association_list[index]->linked_book()==book &&<br />
this->association_list[index]->linked_member()==member)<br />
{<br />
searching=false;<br />
link_slot=index;<br />
}<br />
else<br />
index++;<br />
else<br />
{<br />
index++;<br />
}<br />
if (searching && (index == LIST_SIZE))<br />
{<br />
searching=false;<br />
success=false;<br />
}<br />
}<br />
if (success)<br />
delete this->association_list[link_slot];<br />
return success;<br />
}<br />
#endif<br />
<br />
#include<iostream><br />
#include"Book.h"<br />
#include"Member.h"<br />
using namespace std;<br />
<br />
Book::Book()<br />
{<br />
cout<<"Book constructor called called\n";<br />
this->bookDetails=get_string_ver2("input book title and author separated by a comma and a space character: ");<br />
}<br />
<br />
Book::~Book()<br />
{<br />
if (this->bookDetails)<br />
delete [] this->bookDetails;<br />
}<br />
<br />
void Book::display(Member* borrower)<br />
{<br />
cout<<"Book title and author are: "<<this->bookDetails<<"."<<endl;<br />
if (borrower)<br />
cout<<"The member's name is: "<<borrower->get_name()<<"."<<endl;<br />
else<br />
cout<<"No member has borrowed a book."<<endl;<br />
}<br />
<br />
#ifndef _BOOK<br />
#define _BOOK<br />
#include "utility.h"<br />
class Member;
class Book<br />
{<br />
public:<br />
Book();<br />
<br />
~Book();<br />
<br />
char* get_bookDetails(){return this->bookDetails;}<br />
<br />
void display(Member* borrower);<br />
private:<br />
Member* borrower;<br />
char* bookDetails;<br />
};<br />
#endif<br />
<br />
#include<iostream><br />
#include"Book.h"<br />
#include"Member.h"<br />
using namespace std;<br />
<br />
Member::Member()<br />
{<br />
cout<<"Member constructor called\n";<br />
this->name=get_string("Input member's surname and forename respectively, separated by a comma and a space character: <br />
<br />
");<br />
}<br />
<br />
Member::~Member()<br />
{<br />
if (this->name)<br />
delete [] this->name;<br />
}<br />
<br />
void Member::display(Book* book)<br />
{<br />
cout<<"Member's name is: "<<this->name<<"."<<endl;<br />
if (book)<br />
cout<<"Book title and author are: "<<book->get_bookDetails()<<"."<<endl;<br />
else<br />
cout<<this->name<<" has not borrowed any book."<<endl;<br />
}<br />
<br />
#ifndef _MEMBER<br />
#define _MEMBER<br />
#include "utility.h"<br />
class Book;
class Member<br />
{<br />
public:<br />
Member();<br />
<br />
~Member();<br />
<br />
char* get_name(){return this->name;}<br />
<br />
void display(Book* book);<br />
private:<br />
Book* book;<br />
char* name;<br />
};<br />
#endif<br />
<br />
#ifndef _LISTCPP<br />
#define _LISTCPP<br />
#include <iostream><br />
#include "AssociationList.h"<br />
#include "List.h"<br />
<br />
using namespace std;<br />
<br />
template<class Object><br />
List<Object>::List()<br />
{<br />
this->num_elements=0;<br />
}<br />
<br />
template<class Object><br />
List<Object>::~List()<br />
{<br />
for(int element=0; element<this->num_elements; element++)<br />
delete this->element_list[element];<br />
}<br />
<br />
template<class Object><br />
void List<Object>::addElement(char* type)<br />
{<br />
if (this->num_elements == MAX_ELEMENTS)<br />
{<br />
cout<<"No more room in the "<<type<<" array.\n";<br />
cout<<"The maximum number of "<<type<<" is "<<MAX_ELEMENTS<<"."<<endl;<br />
}<br />
else<br />
{<br />
this->element_list[num_elements]=new Object;<br />
(this->num_elements)++;<br />
}<br />
}<br />
<br />
template<class Object><br />
void List<Object>::displayElement(char* type)<br />
{<br />
if (num_elements == 0)<br />
cout<<"No "<<type<<" is found in the "<<type<<" array.\n";<br />
else<br />
for(int element=0; element<this->num_elements; element++)<br />
{<br />
cout<<'\n';<br />
this->element_list[element]->display(association_list.get_member(element_list[element]));<br />
}<br />
}<br />
<br />
<br />
template<class Object><br />
void List<Object>::borrowElement()<br />
{<br />
cout<<"Borrow book function invoked\n";<br />
}<br />
<br />
template<class Object><br />
void List<Object>::returnElement()<br />
{<br />
cout<<"Return book function invoked\n";<br />
}<br />
<br />
<br />
<br />
#endif<br />
<br />
#ifndef _LIST<br />
#define _LIST<br />
#include "AssociationList.h"<br />
#include "Book.h"<br />
#include "Member.h"<br />
const int MAX_ELEMENTS=3;<br />
template<class Object><br />
class List<br />
{<br />
public:<br />
List();<br />
~List();<br />
void addElement(char* type);<br />
void removeElement();<br />
void displayElement(char* type);<br />
void borrowElement();<br />
void returnElement();<br />
private:<br />
AssociationList<Book,Member>association_list;<br />
Object* element_list[MAX_ELEMENTS];<br />
int num_elements;<br />
};<br />
#endif<br />
<br />
#include <iostream><br />
#include <cstring><br />
#include <ctype.h><br />
using namespace std;<br />
#define cinFlush cin.seekg(ios::end)<br />
<br />
char* get_string (char* prompt)<br />
{<br />
const int MAX_SIZE=100;<br />
char string[MAX_SIZE];<br />
char* out_string;<br />
char *substring=", ";<br />
int position=0;<br />
bool result=false;<br />
do<br />
{<br />
cout<<prompt; cinFlush;<br />
cin.getline(string,MAX_SIZE);<br />
if (strstr(string,substring) == NULL)<br />
{<br />
cout<<"Invalid Name(substring ', ' not found into the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
position=strstr(string,substring) - string;<br />
if (position == 0)<br />
{<br />
cout<<"Invalid Name(substring ', ' found in the beginning of the string)"<<endl;<br />
result=false;<br />
}<br />
else if (toascii(string[position + 1]) == 32 && string[position + 2] == '\0')<br />
{<br />
cout<<"Invalid Name(substring ', ' found at the end of the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
for(int j=0; j<position; j++)<br />
if ((toascii(string[j]) >= 0 && toascii(string[j]) <= 64)<br />
|| (toascii(string[j]) >= 91 && toascii(string[j]) <= 96)<br />
|| (toascii(string[j]) >= 123 && toascii(string[j]) <= 127))<br />
{<br />
cout<<"Invalid character(s) found before the substring ', '"<<endl;<br />
result=false;<br />
break;<br />
}<br />
else<br />
{<br />
for(int m=position + 2; m<strlen(string); m++)<br />
if ((toascii(string[m]) >= 0 && toascii(string[m]) <= 64)<br />
|| (toascii(string[m]) >= 91 && toascii(string[m]) <= 96)<br />
|| (toascii(string[m]) >= 123 && toascii(string[m]) <= 127))<br />
{<br />
result=false;<br />
break;<br />
}<br />
else<br />
result=true;<br />
}<br />
<br />
}<br />
}<br />
}while(!result && !cin.eof());<br />
out_string=new char [strlen(string)+1];<br />
if (out_string)<br />
strcpy(out_string,string);<br />
return out_string;<br />
}<br />
<br />
<br />
char* get_string_ver2 (char* prompt)<br />
{<br />
const int MAX_SIZE=100;<br />
char string[MAX_SIZE];<br />
char* out_string;<br />
char *substring=", ";
int position=0;
bool result=false;
do
{
cout<<prompt; cinFlush;<br />
cin.getline(string,MAX_SIZE);<br />
if (strstr(string,substring) == NULL)<br />
{<br />
cout<<"Invalid String(substring ', ' not found into the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
position=strstr(string,substring) - string;<br />
if (position == 0)<br />
{<br />
cout<<"Invalid String(substring ', ' found in the beginning of the string)"<<endl;<br />
result=false;<br />
}<br />
else if (toascii(string[position + 1]) == 32 && string[position + 2] == '\0')<br />
{<br />
cout<<"Invalid String(substring ', ' found at the end of the string)"<<endl;<br />
result=false;<br />
}<br />
else<br />
{<br />
for(int z=0;z<position; z++)<br />
if (!(( toascii(string[z]) > 64 && toascii(string[z]) < 91 ) <br />
|| ( toascii(string[z]) > 96 && toascii(string[z]) < 123)<br />
|| ( toascii(string[z]) == 32)))<br />
{<br />
cout<<"Invalid character(s) found before the substring"<<endl;<br />
result=false;<br />
break;<br />
}<br />
else<br />
{<br />
for(int m=position + 2; m<strlen(string); m++)<br />
if (!(( toascii(string[z]) > 64 && toascii(string[z]) < 91 ) <br />
|| ( toascii(string[z]) > 96 && toascii(string[z]) < 123)<br />
|| ( toascii(string[z]) == 32)))<br />
{<br />
result=false;<br />
break;<br />
}<br />
else<br />
result=true;<br />
}<br />
<br />
}<br />
}<br />
}while(!result);<br />
out_string=new char [strlen(string)+1];<br />
if (out_string)<br />
strcpy(out_string,string);<br />
return out_string;<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
void hold_screen()<br />
{<br />
char ch;<br />
<br />
cout << "hit enter to continue" << endl;<br />
cin.get(ch);<br />
}<br />
<br />
void prompt_to_quit()<br />
{<br />
char ch;<br />
<br />
cout << "hit enter to quit" << endl;<br />
cin.get(ch); <br />
}<br />
<br />
#ifndef _UTILITY<br />
#define _UTILITY<br />
<br />
char* get_string (char* prompt);<br />
<br />
<br />
char* get_string_ver2(char* prompt);<br />
<br />
<br />
void hold_screen();<br />
<br />
<br />
void prompt_to_quit();<br />
<br />
<br />
<br />
#endif<br />
and finally
<br />
#include <iostream><br />
#include "List.cpp"<br />
#include "Association.cpp"<br />
#include "AssociationList.h"<br />
#include "AssociationList.cpp"<br />
#include "Book.h"<br />
#include <conio.h><br />
#include "Member.h"<br />
#include "utility.h"<br />
#define cinFlush cin.seekg(ios::end)<br />
using namespace std;<br />
<br />
void main()<br />
{<br />
List<Member>member1;<br />
List<Book>book1;<br />
char menuChoice;<br />
do<br />
{<br />
cout<<'\n';<br />
cout<<'\n';<br />
cout<<"**** Main Menu ****"<<endl;<br />
cout<<'\n';<br />
cout<<"1: Add Book "<<endl;<br />
cout<<'\n';<br />
cout<<"2: Remove Book "<<endl;<br />
cout<<'\n';<br />
cout<<"3: Display Books "<<endl;<br />
cout<<'\n';<br />
cout<<"4: Add Member "<<endl;<br />
cout<<'\n';<br />
cout<<"5: Remove Member "<<endl;<br />
cout<<'\n';<br />
cout<<"6: Display Members "<<endl;<br />
cout<<'\n';<br />
cout<<"7: Borrow Book "<<endl;<br />
cout<<'\n';<br />
cout<<"8: Return Book "<<endl;<br />
cout<<'\n';<br />
cout<<"0: Quit "<<endl;<br />
cout<<endl;<br />
cout<<"Enter one of the above choices: ";cinFlush;<br />
cin>>menuChoice;<br />
switch(menuChoice)<br />
{<br />
case '1': cout<<'\n';<br />
book1.addElement("Books");<br />
break;<br />
case '2': cout<<'\n';<br />
<br />
break;<br />
case '3': cout<<'\n';<br />
book1.displayElement("Books");<br />
break;<br />
case '4': cout<<'\n';<br />
break;<br />
case '5': cout<<'\n';<br />
<br />
break;<br />
case '6': cout<<'\n';<br />
break;<br />
case '7': cout<<'\n';<br />
break;<br />
case '8': cout<<'\n';<br />
break;<br />
default: cout<<'\n';<br />
cout<<"Invalid Selection\n";<br />
}<br />
}while(menuChoice != '0' && !cin.eof());<br />
prompt_to_quit();<br />
}<br />
What I want to do is to use one function for displaying members and books (to use a template).
What needs to be changed to use only one function for displaying members and books?
Regards,
grscot
|
|
|
|
|
Could you cut down your code example and format it better? Do you expect someone to read all that code?
|
|
|
|
|
//structure.cpp
#include <iostream>
using namespace std;
struct item
{
char name[35];
int price;
};
int main()
{
item ksoh =
{
"kings sword of haste",
25,000
};
item gpow =
{
"godly plate of the whale",
278,000
};
item demonspike =
{
"legit demonspike",
492,000
};
cout << "garth's equipment shoppe\n";
cout << "how about a nice " << ksoh.name << "for a mere ";
cout << ksoh.price <<"?\n";
cout << "or\n";
cout << "a high ac " << demonspike.name;
cout << " for only " << demonspike.price;
cout << " could really liven things up when laz is in the house. ";
cout << " be sure to take special note of our 2fer special today: ";
cout << "a " << ksoh.name << " and a burly ";
cout << gpow.name << " would most certainly be essential ";
cout << "if you plan on doing anything significant beyond level 12.";
cout << " on sale today only, a 2fer special of ";
cout << ksoh.price + gpow.price cout << "!!\n ";
}
|
|
|
|
|
|
preceding is some code I can't get to work. It's a simple structure
I've created and there doesn't seem to be any difference at all with
it and the one in the book I'm studying other than i've got 3
structure variables and the book's example has 2. My only clue is the
book mentions that external structure declaration is necessary to
allow all functions that follow to use it. I did that to start with
and tried placing it inside main as well; coming up with the same
error each time. ms visual studio.net is what I'm using the error I'm
getting is "structure error LNK2019: unresolved external symbol _main
referenced in function _mainCRTStartup" the help on this error isn't
much help to me, it's just too cryptic for me to apply to my problem.
Sorry if this is a really easy problem, it seems like it should be,
but I can't get it to work...
|
|
|
|
|
dont u have to do it like
struct item ksoh{
"kings sword of haste",
25,000
};
struct item gpow =
{
"godly plate of the whale",
278,000
};
struct item demonspike =
{
"legit demonspike",
492,000
};
|
|
|
|
|