Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 24 Apr 2010
Offline Last Active Private

#5271984 Advice on coding style

Posted by Khatharr on 20 January 2016 - 05:53 AM

Is there a function call to purge / clear an error in the input stream? cin.clear() didn't work.

Yes. It's in the code I showed you earlier. Look at the steps I used to clean up the stream after the comment.

You're clearing the error flags, but not removing the bad data from the stream.

Also, be advised that using 0 at the beginning of a number is the notation for octal. In the case of 00 it doesn't matter because zero is the same in any base, but 010 is 8, not 10, so watch out for that.

#5271967 Pathfinding and Databases

Posted by Khatharr on 20 January 2016 - 02:03 AM


#5271930 Advice on coding style

Posted by Khatharr on 19 January 2016 - 08:31 PM

I'm not sure what you're asking. At first I thought you were running into the stream in issue with spaces, but from what you said it sounds like you're expecting it get several names and then get the grades after that?


But the code you posted tells it to get one name and then enter a grade input loop, then when the grade loop ends it should ask for the next name.


Could you explain what exact behavior you want to happen, and give an example of the input you're providing it?

#5271604 C++ exceptions

Posted by Khatharr on 17 January 2016 - 04:27 PM

"Sprite::Sprite() - Could not open file "chubu.png" for loading." saves me a nice bit of time compared to, "The program was terminated unexpectedly."


Especially since I can ifdef out the try/catch stuff in the debugger and VC will halt the program at the site of the throw while I'm debugging.

#5271603 static variable in function

Posted by Khatharr on 17 January 2016 - 04:14 PM

Bar& Foo() {
00BC70E0  push        ebp  
00BC70E1  mov         ebp,esp  
00BC70E3  sub         esp,0C0h  
00BC70E9  push        ebx  
00BC70EA  push        esi  
00BC70EB  push        edi  
00BC70EC  lea         edi,[ebp-0C0h]  
00BC70F2  mov         ecx,30h  
00BC70F7  mov         eax,0CCCCCCCCh  
00BC70FC  rep stos    dword ptr es:[edi]  
  static Bar bar;
00BC70FE  mov         eax,dword ptr [_tls_index (0C6A708h)]  
00BC7103  mov         ecx,dword ptr fs:[2Ch]  
00BC710A  mov         edx,dword ptr [ecx+eax*4]  
00BC710D  mov         eax,dword ptr ds:[00C6A1E0h]  
00BC7112  cmp         eax,dword ptr [edx+104h]  //if it's already instantiated
00BC7118  jle         Foo+67h (0BC7147h)  //jump to return
00BC711A  push        0C6A1E0h  
00BC711F  call        __Init_thread_header (0BB7B54h)  
00BC7124  add         esp,4  
00BC7127  cmp         dword ptr ds:[0C6A1E0h],0FFFFFFFFh  
00BC712E  jne         Foo+67h (0BC7147h)  
00BC7130  mov         ecx,0C6A1DCh  
00BC7135  call        Bar::Bar (0BB7A91h)  
00BC713A  push        0C6A1E0h  
00BC713F  call        __Init_thread_footer (0BB88D3h)  
00BC7144  add         esp,4  
  return bar;
00BC7147  mov         eax,0C6A1DCh  
00BC714C  pop         edi  
00BC714D  pop         esi  
00BC714E  pop         ebx  
00BC714F  add         esp,0C0h  
00BC7155  cmp         ebp,esp  
00BC7157  call        __RTC_CheckEsp (0BB91BBh)  
00BC715C  mov         esp,ebp  
00BC715E  pop         ebp  
00BC715F  ret  

#5271602 [SFML] My vector array is only drawing one sprite

Posted by Khatharr on 17 January 2016 - 04:04 PM

Rather than using the temporary and pushing it, you can do:

    enemyVector.emplace_back(new Troll);
    enemyVector.back()->setPosition(std::rand() % 600 + 1, std::rand() % 600 + 1);

#5271540 [SFML] My vector array is only drawing one sprite

Posted by Khatharr on 17 January 2016 - 07:44 AM

If you only put one thing in the container then there will only be one thing in the container. (Show the full code body, not just the parts you like.)

#5271533 C++ console game

Posted by Khatharr on 17 January 2016 - 03:39 AM

Please use code tags when posting code. you can do this manually with [ code] [ /code] or use the < > button in the editor.
The immediate issue is that you declare 'slots' with size 2 but use slots[0], slots[1], and slots[2]. If you want three then declare:
int slots[3];
std::endl places a newline character on the stream and also flushes the stream. If you just want a newline then you can use "\n". For example:
cout << "Your bank roll is " << money << "\n\nPlay again?(y/n): ";
Why cin.get() after using cin >> thing ?

srand() should be called only once for the lifetime of the thread (unless you intentionally want to reseed, which is not the case here).

Move the srand() call to the beginning of main().

#5271529 C++ exceptions

Posted by Khatharr on 17 January 2016 - 03:19 AM

superior alternatives



#5271480 C++ exceptions

Posted by Khatharr on 16 January 2016 - 06:18 PM

The world has come a long way since exceptions were originally conceived; they're an unfortunate anachronism that are best brushed under the rug of history.


Except that STL and even core language features throw exceptions to communicate errors. There are many, many cases where exceptions are a massive PITA, but there are also cases where they work better than return values. Constructor errors are a problem, and requiring a return value to make room for an "error" value is not ideal. The new operator throwing by default instead of returning nullptr has always struck me as an incredibly dick move, but not nearly as dick as the syntax for non-throwing new..


If you plan to recover from any of this shit then writing exception safe code isn't really optional. That said, throwing and catching isn't really the problem here. Those are both pretty easy to do, and throwing is even downright convenient if you want to include an error message.


Fortunately, for game development recovery is usually not necessary. If you plan to write code for a mission critical server or something then you're stuck, but if you're just writing a game then most errors can CTD with an error message. That said, it would make sense to catch in main() and print e.what() or even just const char* in a dialogue, then throw descriptive error messages from wherever you want.


Of course, that's ignoring overhead, and in any case it might just be far more convenient to have a core language feature that could do the same job without the overhead or explicit catch.

#5270784 Vertex Dynamic Memory (malloc and free)

Posted by Khatharr on 12 January 2016 - 07:15 PM

Instead of doing all of these individual allocations and reads why not just create a structure that matches the format of the data in the file and read the whole thing in one call?

#5270572 Criticism of C++

Posted by Khatharr on 11 January 2016 - 01:49 PM

Has anyone mentioned properties yet? I want automatic-ish properties.

#5270312 Advice on coding style

Posted by Khatharr on 09 January 2016 - 04:41 PM

Prohibitions without adequate explanation are counter-productive. Some people will react by rejecting your advice and others will accept it pathologically and develop (and often spread) alternative bad habits. I'm guilty of doing the same thing sometimes though.

If your compiler supports C++11 or greater then you should too. Replace that enum with enum class, future code readers will thank you later

Can you make (or link) a compelling case for this?

Just say no to pulling in the std namespace, being a game programmer, you will probably end up writing a vector class at some point

I think I understand what you mean, but could you explain it so that someone who doesn't can get why it's a problem?

Probably want to just "inline" the date class's functions.
Same with Employee


FUTURE BUG: employee::showEmp() you can get that switch statement into a bad way. Think about adding a default case to avoid people from doing heinous things later on.

Such as?

Float suck, don't use them unless you have a good reason


Switch statements in main, default case....just waiting for bad user input

What input is "bad" and why? What will happen and how can it be avoided?

"\nDate of employment (mm/dd/yyyy) format: ", There are several ways to do this, 1 of many, http://www.cplusplus.com/reference/ctime/strftime/
Apply the above to date::showDate()

Avoid stdlib in C++. If you want to promote a standardized method then C++ uses <chrono>.

int number, year, month, day; There exists a data structure to represent time already, no need to reinvent

What data structure?

Lots of issues between pass by value vs reference

This probably implies a lack of understanding. Can you explain the correct usage and why it's superior?

#5269724 need suggestion for IDE/Engine

Posted by Khatharr on 06 January 2016 - 07:32 PM

Unity and MonoGame are recommended for C#.


Unity has a massive amount of support data (huge reference library, concept tutorial videos, etc) and community. It's a full IDE, so it may be a little overwhelming at first, but intro videos can very quickly get you situated with the workflow.


MonoGame is pretty easy to work with and also has a decent community. MonoGame is an API rather than an IDE, so it doesn't involve learning a new environment.


You should probably take a look at the introductory materials for both and see which one is closer to meeting your needs.

#5269533 Advice on coding style

Posted by Khatharr on 05 January 2016 - 08:08 PM

That's exactly (part of) what const correctness does for you. It catches subtle errors like that which are otherwise valid code but don't do what you really want.


Good luck. Let us know if you have more questions. smile.png