[QUOTE=thf;32315878]What's your reason not to let it just implicitly return? Just curious here :v:[/QUOTE]
Purely OCD. I like consistency.
It's more of an aesthetic decision than a practical decision.
[QUOTE=Werem00se;32303760]Okay, I can do that. Well, basically it's a simple simulation rpg about being a slave. And I decided to have fun with. I don't want him to die per say, but it is going to be a possibility. It's a reference to that movie ROOTS.
See, when I program I have to get myself interested in doing something, then I'll do it and learn on the way. That's how I thought myself a good bit of Java, but anyway it's really racist and I apologize. I'm pretty sure I'm not racist. Anyway when it's done I'll you guys for suggestions on how to improve the code. I learn more that way. But yeah, that was pretty simple.. :v:
Oh, and I want to know why it outputs another one of 'your name is toby, got that' even though When it hits zero, it should stop.[/QUOTE]
It's not that weird, you get beaten, you call the takeDamage function which lowers your health and then inside that function you call checkHealth and it outputs that you're dead. Then you come back to the while loop and under the takeDamage function you do another "Your name is Toby" print.
You could make the takeDamage function return a bool, true if you're dead, false if you're not. So if it returns true then you break from the loop.
Value code readability over functionality 99% of the time.
[QUOTE=Chris220;32306915]Well, the latter brings everything from inside the std namespace into your global namespace, so name collisions and stuff are more likely to happen. You can just use std::cout << "whatever" << std::endl; like that anyway, it's not much extra typing to be honest.
[editline]15th September 2011[/editline]
Assuming you put "using namespace std;" in the global namespace, at least.[/QUOTE]
When using other libraries, they might have nested namespaces or longer names. In that case I also often do namespace short = longnamespace::nested1::nested2.
But generally there's usually nothing wrong when doing stuff like that inside implementation files or scopes. You should not do that in the top-level of header-files though.
[editline]16th September 2011[/editline]
[QUOTE=Bambo.;32307467]Hmm? can you help me out? I'm really new to C++ and SFML haha, i have no idea how to store the sf::Input reference in the player-class, also am i leaking memory because i didn't destroy the player class pointer?
[editline]15th September 2011[/editline]
also can you suggest better ways of doing this?[/QUOTE]
[cpp]class Player
{
//...
void acceptInput(const sf::Input& input);
//...
};
//....
void Player::acceptInput(const sf::Input& input)
{
if(input.isKeyDown(sf::Key::A))
//...
}
//...
player->acceptInput(input);
[/cpp]
As for the player being a pointer, as it was mentioned, since you allocated it with new you must release the memory with delete.
Another option would be just having it on the stack like
[cpp]Player player;[/cpp]
[editline]16th September 2011[/editline]
It is probably more comfortable to listen to a key-event, accept a sf::Key::Code in acceptInput and use a switch-statement to check each possible key.
Since you probably want to be able to re-bind the keys, perhaps look how a std::map can help you there ;)
You could then also return a boolean indicating if the key-event was handled by the player.
[QUOTE=Anonim;32315853]Strings in Java are not primitive types like int, char, byte, boolean, float, double, long and short.
Therefore, when you're doing this comparison:
[code]pSex.toUpperCase() == "M"[/code]
You're actually comparing two pointers to separate objects when you ought to be comparing their contents.
Luckily, the String class has a built-in method for this:
[code]pSex.toUpperCase().equals("M")[/code]
This should work.[/QUOTE]
It worked, but at the mean time when it comments the gender choice, it also prints out the else and then stops program.
Also how would I make it to:
If user enters m/M further in text use for example: " He was going there even though he didn't want to..." (so that it replaces places with he) while if user enters f/F it says: "She was going there even though she didn't want to..."
help?
[QUOTE=ZeekyHBomb;32316223]
[cpp]class Player
{
//...
void acceptInput(const sf::Input& input);
//...
};
//....
void Player::acceptInput(const sf::Input& input)
{
if(input.isKeyDown(sf::Key::A))
//...
}
//...
player->acceptInput(input);
[/cpp]
As for the player being a pointer, as it was mentioned, since you allocated it with new you must release the memory with delete.
Another option would be just having it on the stack like
[cpp]Player player;[/cpp]
[editline]16th September 2011[/editline]
It is probably more comfortable to listen to a key-event, accept a sf::Key::Code in acceptInput and use a switch-statement to check each possible key.
Since you probably want to be able to re-bind the keys, perhaps look how a std::map can help you there ;)
You could then also return a boolean indicating if the key-event was handled by the player.[/QUOTE]
Thanks alot, i think i understand this alot better now!
[QUOTE=ROBO_DONUT;32315895]Purely OCD. I like consistency.
It's more of an aesthetic decision than a practical decision.[/QUOTE]
Half of programming is probably OCD
[editline]16th September 2011[/editline]
The other half is swearing
[QUOTE=ZeekyHBomb;32316223]When using other libraries, they might have nested namespaces or longer names. In that case I also often do namespace short = longnamespace::nested1::nested2.
But generally there's usually nothing wrong when doing stuff like that inside implementation files or scopes. You should not do that in the top-level of header-files though.
[editline]16th September 2011[/editline]
[cpp]class Player
{
//...
void acceptInput(const sf::Input& input);
//...
};
//....
void Player::acceptInput(const sf::Input& input)
{
if(input.isKeyDown(sf::Key::A))
//...
}
//...
player->acceptInput(input);
[/cpp]
As for the player being a pointer, as it was mentioned, since you allocated it with new you must release the memory with delete.
Another option would be just having it on the stack like
[cpp]Player player;[/cpp]
[editline]16th September 2011[/editline]
It is probably more comfortable to listen to a key-event, accept a sf::Key::Code in acceptInput and use a switch-statement to check each possible key.
Since you probably want to be able to re-bind the keys, perhaps look how a std::map can help you there ;)
You could then also return a boolean indicating if the key-event was handled by the player.[/QUOTE]
I'd use the stack for something like the player, and if making an entity system, probably store them in shared_ptrs.
I was just wondering what's the best way of including a .cpp file in a class. eg.
I haz one class in one file and one class in another and i want to use them both together in my main()
[QUOTE=KevinThePirate;32323158]I was just wondering what's the best way of including a .cpp file in a class. eg.
I haz one class in one file and one class in another and i want to use them both together in my main()[/QUOTE]
You don't include .cpp files.
[QUOTE=T3hGamerDK;32324732]You don't include .cpp files.[/QUOTE]
You could try being useful and telling him how he [I]should[/I] do it ya know...
That is, you have to declare your class in a header file, and implement it in your CPP file. Then you can include the header using #include "my_header.h" in the CPP file you wish to use the class in.
This article explains it quite well: [url]http://www.learncpp.com/cpp-tutorial/19-header-files/[/url]
I'm trying to translate a number range of 255 to a range of 28, however the range doesn't follow any actual sequence, so I'm unable to make some sort of formula for it. Is there a better way to translate the range instead of using if else statements?
Here's a snippet of what the range is so you can get an idea:
[code]
255-170 -> 3
169-98 -> 4
97-62 -> 5
61-44 -> 6
...
[/code]
uh what?
-snip cant read-
I'm basically trying to optimise this mess:
[cpp]
if(AG >= 170) //170+
counter = 3;
else if(AG >= 98) //169-98
counter = 4;
else if(AG >= 62) //97-62
counter = 5;
else if(AG >= 44) //61-44
counter = 6;
//etc...
[/cpp]
This ladder goes on for about another 15 statements and I'm wondering if there's a better way.
[QUOTE=Niteshifter;32329852]I'm basically trying to optimise this mess:
[cpp]
if(AG >= 170) //170+
counter = 3;
else if(AG >= 98) //169-98
counter = 4;
else if(AG >= 62) //97-62
counter = 5;
else if(AG >= 44) //61-44
counter = 6;
//etc...
[/cpp]
This ladder goes on for about another 15 statements and I'm wondering if there's a better way.[/QUOTE]
Since you already said that there's no pattern, that is probably the best way there is, without knowing anything else about what you're doing or the numbers.
How do I add an include folder and a lib forlder when using nMake?
I dont want to put the libs/headers in the VC folder
How do you know which range the number is in if there's no pattern?
[QUOTE=Jookia;32330877]How do you know which range the number is in if there's no pattern?[/QUOTE]
I was given a chart with the list of numbers.
I just found out that it's an exponential growth function and the chart capped it at 255 (the next number in the series would have been around 340). Such a dumb mistake that threw me off. Unfortunately, the numbers are floored, so I have to test a few equations, but I've got the structure now.
I'm trying to create a pipe using Winsock and I've come up with the following code:
[cpp]// Headers
#include <iostream>
#include <WinSock2.h>
// Entry
int main( int argc, const char* argv[] )
{
// Initialize winsock
WSADATA wsaData;
WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
// Start listening for connections
SOCKET listenSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
SOCKADDR_IN listenInfo;
listenInfo.sin_family = AF_INET;
listenInfo.sin_addr.s_addr = INADDR_ANY;
listenInfo.sin_port = htons( 1337 );
bind( listenSocket, (SOCKADDR*)&listenInfo, sizeof( listenInfo ) );
listen( listenSocket, 10 );
// Wait for client
SOCKET clientSocket = accept( listenSocket, NULL, NULL );
// Connect to endpoint
SOCKET serverSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
HOSTENT* hostEntry = gethostbyname( "www.google.com" );
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr = *( (LPIN_ADDR)*hostEntry->h_addr_list );
serverInfo.sin_port = htons( 80 );
connect( serverSocket, (SOCKADDR*)&serverInfo, sizeof( serverInfo ) );
fd_set readSockets;
readSockets.fd_count = 2;
readSockets.fd_array[0] = clientSocket;
readSockets.fd_array[1] = serverSocket;
/*char buf[1024];
int l = recv( clientSocket, buf, 1024, 0 );
send( serverSocket, buf, l, 0 );*/
SOCKET in, out;
char buffer[1024];
int len;
while ( true )
{
select( 0, &readSockets, 0, 0, 0 );
if ( FD_ISSET( clientSocket, &readSockets ) ) { in = clientSocket; out = serverSocket; std::cout << "Reading from client!" << std::endl; }
else { in = serverSocket; out = clientSocket; std::cout << "Reading from server!" << std::endl; }
len = recv( in, buffer, 1024, 0 );
if ( len > 0 )
send( out, buffer, len, 0 );
else
break;
}
closesocket( serverSocket );
closesocket( clientSocket );
// Clean up
closesocket( listenSocket );
WSACleanup();
return 0;
}[/cpp]
Unfortunately the server fails to respond after the initial message from the client. If I uncomment the code that forces the initial transfer, the server does respond. I verified that the send function does not fail in both cases. So for some reason the select only sticks to the first socket that responds??
[editline]shit[/editline]
Oh right, I forgot select modifies the fd_set :')
[QUOTE=Overv;32335914]Winsock stuff.[/QUOTE]
You should use getaddrinfo instead, gethostbyname is deprecated.
I'm working on a Server <---> Clients application and I have a bit of trouble understanding what a socket is. Is it a unique identifier for a Server <---> Client connection?
Can I do something like
[code]
struct client{
Socket socket;
int id;
};
[/code]
to handle multiple clients?
I'm working in C++ and I need to have a static map in a class;
[code]
class TextureRepository {
static map<string, int> textures;
static int foo;
};
[/code]
I of course also do this thing in the cpp
[code]
map<string, int> TextureRepository::textures= map<string, int>();
int TextureRepository::foo= 0;
[/code]
However, when I use the "textures" map, I get access violation errors. The "foo" variable works fine however.
[editline]17th September 2011[/editline]
Apparently it had something to do with a
TextureReposiory *texture_repository= new TextureRepository();
I had declared in some old code in main.
I heard that some compiler can make a "map" file for your sources, so when a program crashes, it looks at the stack and checks the address against the map, giving you the exact line where an error happened.
I'm using MinGW and I'd like to know how can I do something to a similar effect in my project.
That's what you get when you compile in debug mode; with gcc it's the "-g" option.
Really shitty newbie question: How do I call something when a variable is changed in C#?
[QUOTE=Mr. Smartass;32350448]Really shitty newbie question: How do I call something when a variable is changed in C#?[/QUOTE]
Take a look at [URL="http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.80%29.aspx"]properties[/URL]
[QUOTE=Fox-Face;32350571]Take a look at [URL="http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=vs.80%29.aspx"]properties[/URL][/QUOTE]
Thanks man, I had no idea where to start looking.
I've been looking for a good C# sound library for a while now, and there are plenty of them out there, but none of them allow commercial use without having to buy a license (I'm going to use it for Froid 2, and since I'm not using SFML.NET anymore, I can't use SFML's audio stuff anymore... and Froid 2 might be Steam-worthy, who knows?). The ones I found so far are irrKlang, FMOD.NET and BASS.NET, but as I said, they are free for non-commercial use and require a license for commercial use. Anyone knows a C# sound library that allows free commercial use?
(the hobbyist license for irrKlang is only €65, but still, I'd obviously rather go for a free alternative)
[QUOTE=Dlaor-guy;32352423]I've been looking for a good C# sound library for a while now, and there are plenty of them out there, but none of them allow commercial use without having to buy a license (I'm going to use it for Froid 2, and since I'm not using SFML.NET anymore, I can't use SFML's audio stuff anymore... and Froid 2 might be Steam-worthy, who knows?). The ones I found so far are irrKlang, FMOD.NET and BASS.NET, but as I said, they are free for non-commercial use and require a license for commercial use. Anyone knows a C# sound library that allows free commercial use?
(the hobbyist license for irrKlang is only €65, but still, I'd obviously rather go for a free alternative)[/QUOTE]
Couldn't you use only the audio part of SFML.NET?
Sorry, you need to Log In to post a reply to this thread.