Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

142 Neutral

About deathknight2005

  • Rank
  1. deathknight2005

    School assignment using while loops

    I see what you are doing. You are writing the sum to the console in each loop. That won't work. You have to add counter to the sum with each loop, and at the very end of the loop, print it. If you print it during each loop it will, like you said, only print increasing numbers each time. I think you misunderstood what I said when I told you to print sum. I meant after the loop. This will not work: while(counterR >= firstNum) { cout << (sum+=counterR) << " "; counterR -= 2; } Because look at where you are printing. Every single loop will print something, but you only want to print the total sum. You have to do this: sum = 0; while (counter >= firstNum) { sum += counter; counter -= 2; } cout << sum << endl; If you do that, sum will continually add the counter variable to itself, and, when the loop is over, print the value to the console. EDIT: Oh, it looks like you figured it out yourself!
  2. For one thing, I would close the file after using it. infile.close(); Also, it is safer to check and see if the file is actually open before you begin using it. What happens if you accidentally enter the wrong filename and the compiler looks to open a file that doesn't exist? You can use if(infile.is_open()) { .... } to make sure of that. Make sure you do everything with the file after that. For instance... if(infile.is_open()) { while(!infile.eof()) { //Reading the file in here... } infile.close(); } infile.eof() checks to see if you are at the end of the file. In order to read the file, you have to loop through the file until infile.eof() returns true. Right now you are just printing to the console window (which is wrong anyways, unless you have overloaded the << operator). The file is open, but it is doing nothing in the code. To read from the file, you use the >> operator, but with the filename instead of cin. For instance: char c; infile >> c; Which will read a character from the file. So what you have to do is read from the file into your matrix. As for the problems you are getting, it looks like you are redefining the apmatrix class which is usually caused by using #include on the same header file more than once. The best way to prevent that is by using an include guard on the class (which we haven't seen yet). This is done by: #ifndef APMATRIX_H #define APMATRIX_H //Class header goes here #endif That prevents the class from being redefined.
  3. deathknight2005

    School assignment using while loops

    This problem is actually very similar to the problem WanMaster helped you with, except in this case it involves even numbers and instead of printing the numbers, you must get the sum of them all. I would have simply summed all of the even numbers and printed the odd numbers in the same loop, however, because of the way the program is structured so far, and because it is a project that demonstrates the use of loops, you can just use another loop for this problem. That way, you can use the same counter variable as well. In this case, you must make sure sum = 0 at the very beginning of your code (otherwise it is an uninitialized variable). Actually, a simple way to do it is to do the exact opposite of WanMaster's code (so that it makes sure the numbers are even, not odd). if (secondNum % 2 == 1) counter = secondNum - 1; // odd, but we want even else counter = secondNum - 2; // even So let's take a few numbers for example: 15: Since the remainder of 15/2 is 1 (15 % 2 == 1), counter will begin at 15 - 1 which is 14, an even number. 14: Since the remainder of 14/2 is 0 (14 % 2 == 0), counter will begin at 14 - 2 which is 12, again an even number. Next, you can go about it the same way as the other problem. By looping through and decrementing the counter variable by 2, until it is >= to firstNum. The only difference in this case is that with each loop you want to add the counter to sum. sum = 0; while (counter >= firstNum) { sum += counter; counter -= 2; } See, it is very similar to the code for the other problem, with some small differences. Once that is done, all you do is display the sum. I hope that helped!
  4. deathknight2005

    School assignment using while loops

    Exactly. Your code will not work because counter will not be equal to secondNum because the assignment is done before secondNum itself has a value! int firstNum, secondNum, oddNum, sum, evenNum, betweenNum, squareNum, counter; counter = secondNum; //This is your problem right here If you look at the code, you will see that you have declared all of those variables, however the user hasn't entered the two numbers yet, so counter will not be equal to the value of secondNum later on. Just think about where you should move the assignment so that counter has the proper value of secondNum after the user has entered it. For #2, you can see whether or not a number is odd by using the % operator, which returns the remainder between two numbers. Knowing that any even number divided by 2 has a remainder of zero, it is a simple task to see whether or not a number is odd or not. All in the same loop, you can output all of the odd numbers by seeing if the remainer between the number and 2 is not 0, and if it is zero, then it is an even number, and for problem #3, you can add it to the sum variable (remember to initialize that variable at 0!). Knowing that, I am sure you can figure out problem #4. You can either implement your own function for squaring numbers (as there is no operator for squaring a number in C++) or you can use the function in the math library.
  5. deathknight2005

    Looking for a free engine for top-down shooter

    It really depends on which programming language you use. If you are planning on programming in C++, I would suggest using Allegro, as it is easy to understand and nice to use. It is good to look up the different engines and their features, and differences, to decide which to use. There are many of them out there.
  6. deathknight2005

    [C++] template base class problems

    No problem ;-)
  7. deathknight2005

    [C++] template base class problems

    Is your class split into two different files? Is there a header (.h) file and the implementation? Because, with template classes, the implementation must be in the same file as the declaration of the class. So you must move the implementation code of the class, which you did not post, to the same file as the declarations. So you would move them to the header file. This does make it difficult if you are making a very large project, especially if you have a lot of code in the implementation, but it is necessary when using templates. Here, have a look at this article that explains the reason for this: Templates Go to the bottom of that tutorial and it explains why you must keep the implementation and header of a template class in the same file. I hope that helped with your problem!
  8. deathknight2005

    Clarification needed...

    Structs are different from enums. In fact, they are more like classes than enums. See, a struct can contain many different types of variables (they can also contain constructors etc...). An enum is a data type that has a number of identifiers. An enum is used to identify different variable types and the value will always be a number. A struct, however, has many data types that can be different. For example, you could have two int variables and a char and a bool in one struct. There is no way of doing this with an enum, because an enum is an abstract data type rather than an object that can contain many variables. I hope that cleared things up (instead of confused you even more!)
  9. deathknight2005

    2D Collision Implementation Problem

    In order to detect collision, you check each frame to see if it has happened and if it has, then you take action (I'm sure you read about that in the actual article, though). So you check in the game loop because if you checked outside of the game loop, it would not work because it would only check it once and never again. I am not sure what sort of example you would like because, judging by your post, you already understand how to actually check for collision. Also, if you are already trying to check collision you must have an understanding of the structure of a game and the game loop etc..
  10. deathknight2005

    Printing a variable using sdl

    Yes there is. You have to create a char buffer and use sprintf. Then, when you want to draw the text, you use that string of characters (the buffer).... I don't think I explained that very well so here is an example: (I am assuming you understand how to draw text because you read the tutorials): char buf[50]; sprintf(buf, "This is the variable: %d", someVariable); TTF_RenderText_Solid(font, buf, color); In this case, buf is the string of chars and someVariable is the variable you want to see. %d is used if you want to show a signed decimal integer, there are more of these specifiers for different types. Check out this site for them and to get a full understanding of sprintf: http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html
  11. deathknight2005

    can't get images to work with direct3d

    Hmm... I can't track down an error right now from your source code but maybe if you sent me the full source I could compile it and find the problem. P.S: You should still minimize the code in between Lock and Unlock
  12. deathknight2005

    can't get images to work with direct3d

    I noticed that you were handling rotation and writing to the file in between Lock() and Unlock(). You should try to keep the code in between as concise as possible because in between those two functions, your program is stalling so that nothing accesses the vertex data. Also, in this section: rect_dest->left -= centre_x; rect_dest->right -= centre_x; rect_dest->top -= centre_y; rect_dest->bottom -= centre_y; Are you trying to pinpoint the centre? If so, the left and top values probably shouldn't subtract the centre_x/centre_y variables from themselves because that won't give you the centre (unless there is a reason for it somewhere else in code that you haven't posted) EDIT: As a side note, you should probably look up D3DXSPRITE because it is useful if you don't want to use too many transformations for a 2D game or program. It could greatly simplify your code while keeping the functionality (after all, it is a 2D program).
  13. deathknight2005

    New to game development, need tips

    Have you ever programmed before? If not, you should learn a language before even attempting game programming. Personally, I use C++, but I do not recommend that to you as a first language. Python, C# or Java would be better options for a first language. If you have programmed before and you know your language well, and you are just starting out in game programming, you should download the Windows SDK and learn how to program windows at least to understand how it works, even if you do end up using an alternative game library. You should probably pick a game library or API you want to use. I use SDL and Allegro but I have also learned API's such as DirectX and OpenGL. DirectX and OpenGL are more difficult to understand and master than Game Librarie's such as SDL and Allegro but they are useful when you start getting into 3D programming. Google is useful getting into Game Programming, you should look up what you want to learn. This link is useful when you are starting, on this site: http://www.gamedev.net/reference/start_here/
  14. deathknight2005

    (c#) what should i do?

    Yes, alexmullins is correct. Except for, you do not even have to write: string name = System.Console.ReadLine(); Instead, you can simply put: string name = Console.ReadLine() This is because you are "using System" at the beginning of your code. So through your code, you are using the namespace System so that you don't have to type it out every time.
  15. deathknight2005

    dev c++ compiler error

    I know what the problem is. First of all, do not comment out END_OF_MAIN, but there is something you need to change in main. Allegro uses the Main() parameters so your code should look like this: #include <allegro.h> int main(int argc, char *argv[]) { //This is the only difference allegro_init(); install_keyboard(); set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); readkey(); return 0; } END_OF_MAIN(); As you can see, I added "int argc, char *argv[]" as parameters to main, they must be used with Allegro.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!