147 Posted Topics
![]() | Re: It's true. You can build games using C. But you can build better using C++. Object oriented design helps a lot when you model the game world, entities and entity interactions. Plus, there are very good libraries available to help. As for your code request, I guess there are a … |
Re: [QUOTE=learningcpp]Can you please explain why is this working?[/QUOTE] Why shouldn't it work? I do it all the time :P In this case, the definition acts as a declaration too. [QUOTE=learningcpp]if I take of the friend from infront of the method name, it fails compiling[/QUOTE] Well, operator << is a binary … | |
Re: It really isn't easy to say without seeing the contents of "user.hpp"... If I were you, I'd put cout statements in key places of the code in order to isolate the problem. | |
Re: What you need is called dynamic memory allocation: [CODE]#include <iostream> using namespace std; struct Node {}; int main() { int num_n; cout << "Enter number of nodes:\n"; cin >> num_n; // Allocate memory dynamically Node * my_nodes = new Node[num_n]; for (int i=0; i<num_n; i++) { // Set values of … | |
Re: What's wrong with this? [CODE]#include <iostream> using namespace std; int main() { double number1, number2; char operation; while (true) { cin >> number1; cin >> operation; cin >> number2; if (!cin) break; switch(operation) { case '+': cout << number1 + number2 << endl; break; case '-': cout << number1 - … | |
Re: As mentioned above, the main use of constructors is object initialization. You may think that this is not an important job, but consider this -> You want to create a vector of 10 vectors of 10 ints and you want to assign the value 25 to all of them (the … | |
Re: Check these out too: [url]http://en.wikipedia.org/wiki/INI_file#Accessing_INI_files[/url] [url]http://msdn.microsoft.com/en-us/library/ms724353.aspx[/url] [url]http://msdn.microsoft.com/en-us/library/ms724345.aspx[/url] EDIT: Though, your file doesn't seem to have the correct format... You may have to change it. | |
Re: [QUOTE=Narue]I challenge you to input a character with the value of 0 using cin. It's not as simple as you might think.[/QUOTE] Assuming I have a char variable declared, I can do this in one statement. | |
Re: The problem is that your compiler doesn't know how to add/subtract complex numbers using operators +/- A simple solution is to define two functions for these operations... [CODE]//... cmplxNumber add_cmplx(const cmplxNumber & c1, const cmplxNumber & c2) { //... } cmplxNumber sub_cmplx(const cmplxNumber & c1, const cmplxNumber & c2) { … | |
Re: Piece of cake! Use SetForegroundWindow -> [url]http://msdn.microsoft.com/en-us/library/ms633539(v=vs.85).aspx[/url] [CODE]#define _WIN32_WINNT 0x0500 #include <windows.h> int main() { HWND hwnd = GetConsoleWindow(); int timer_1 = 0; int timer_2 = 0; int delay = 50; while (true) { if (timer_1 > 100) { if (GetAsyncKeyState(VK_ESCAPE) >> 15) break; timer_1 = 0; } if (timer_2 … | |
Re: It's an interesting topic, so I decided to investigate a bit. The program you wrote [I]does[/I] work with video games. However, don't forget that in a video game the whole window is redrawn many times a second. This means your rectangle is visible merely for some milliseconds. If you try … | |
Re: Don't use a list. Use a vector or a deque and std::sort. Useful links: [url]http://cplusplus.com/reference/stl/vector/[/url] [url]http://cplusplus.com/reference/stl/deque/[/url] [url]http://cplusplus.com/reference/algorithm/sort/[/url] | |
Re: Why don't you pass the array as an argument to the function instead? [CODE]//... void test(string a[5]) { a[0] = "Hello"; //... } int main() { string b[5]; test(b); //... }[/CODE] | |
Re: [QUOTE=triumphost]How would I go about calling java in c++ or invoking it..[/QUOTE] I don't think you need to do that. Usually, a bot doesn't have access to the game's code. It only interacts with the game the same way a human would interact with it. That is, by getting visual … | |
Re: Does grab return a reference? | |
Re: Please, don't double post -> [url]http://cplusplus.com/forum/general/45559/[/url] | |
Re: Excuse me, this is not homework, right? Why not do it the STL way? [CODE]#include <iostream> #include <string> #include <bitset> using namespace std; string atobcd(const string & ascii) { string ret; string::const_iterator cur_it = ascii.begin(); string::const_iterator end_it = ascii.end(); for ( ; cur_it != end_it; ++cur_it) ret += bitset<8>(*cur_it - … | |
Re: [QUOTE=arshi9464]how can i access a particular memory area using pointers?[/QUOTE] Why do you want to do this? What kind of memory area is this? Maybe there are OS API functions that do exactly what you want. For example, if you want to write a cheat engine on windows, you can … | |
Re: This is called padding and it is done by your compiler to improve access speed of the struct members. Useful link -> [url]http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding[/url] | |
Re: You shouldn't delete your buttons in your constructor. Do it in your destructor, using a for loop. The for loop in your constructor should look like this: [CODE]//... for(int bt_index = 0; bt_index < NrButtons; bt_index++) { File >> mask; //rading the mask File >> imag; //normal image File >> … | |
Re: I may be wrong, but I think the OP wants to demonstrate that the value of the pointer is increased by 4 (or whatever sizeof(int) is in his machine) when he uses operator ++ on it. This should do it: [CODE]#include<stdio.h> int main() { int * p = (int *) … | |
Re: Yes, I believe the above will work. As for the other question... [QUOTE]Is there any way to write it like [ICODE]while( style!={"contain","defensive","counter"} ) {/*...*/}[/ICODE][/QUOTE] Yes, there is a way to do it like this: [CODE]#include <algorithm> #include <iostream> #include <string> using namespace std; const string allowed_styles[7] = { "contain", "defensive", … | |
Re: To add to the above, if you don't want to create a window, you can also use this -> [url]http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx[/url] | |
Re: Are you sure the file exists and is in the correct folder? What do you mean by saying they do not work? Are you getting compilation errors, or is it something else? It would really help if you posted your code. | |
Re: Here's another one: [CODE]#include <iostream> struct B { virtual void print(const char * msg = "wtf? -.-") { std::cout << msg << std::endl; } virtual ~B() {} }; struct D : B { void print(const char * msg = "Hello, World!") { std::cout << msg << std::endl; } }; int … | |
Re: You can try opening it for reading and check if it's open. If it's not open, it doesn't exist. [CODE]//... #include <fstream> #include <string> //... std::string filename; //... std::ifstream fin(filename.c_str()); if (fin.is_open()) { /* file exists */ } else { /* file does not exist */ } //...[/CODE] Useful link … | |
Re: I recently stumbled upon an interesting pdf explaining how you can do OOP in C -> [url]http://www.planetpdf.com/codecuts/pdfs/ooc.pdf[/url] It uses structures like this one to hold the type information in a compact way: [CODE]struct Class { size_t size; void * (* ctor) (void * self, va_list * app); void * (* … ![]() | |
Re: You are very close. You should set current to zero every time i changes, not just when it's big enough to become the current mode count. EDIT: You were closer than I initially thought... | |
Re: Check these out: [url]http://cplusplus.com/reference/clibrary/ctime/localtime/[/url] [url]http://cplusplus.com/reference/clibrary/ctime/tm/[/url] | |
Re: [QUOTE=your teacher]The list will be piped in from the command line and terminated with end-of-stream (^Z).[/QUOTE] This means that you can simply do it like this: [CODE]#include <iostream> #include <vector> int main() { std::vector<int> my_vector; int temp; while (std::cin >> temp) my_vector.push_back(temp); //... }[/CODE] When you reach the end-of-stream (CTRL-Z), … | |
Re: Is using a vector a requirement? You could use a (multi)set and its upper_bound member function. If the data on the file is already sorted, you can use the global upper_bound function with a vector. Useful links: [url]http://www.cplusplus.com/reference/stl/set/[/url] [url]http://www.cplusplus.com/reference/stl/set/upper_bound/[/url] [url]http://www.cplusplus.com/reference/stl/multiset/[/url] [url]http://www.cplusplus.com/reference/stl/multiset/upper_bound/[/url] [url]http://cplusplus.com/reference/algorithm/upper_bound/[/url] | |
Re: If you don't modify your initial vector after populating the second, I guess it's ok. Otherwise, both approaches (pointers and iterators) are problematic. BTW, there are ready solutions to this -> [url]http://www.boost.org/doc/libs/1_46_1/libs/multi_index/doc/index.html[/url] EDIT: Also, if you use decide to use pointers, you probably need pointers to myClass, not to int … | |
Re: There are two problems: (1) m must terminate before n can use its output as input. (2) n doesn't terminate. These should work ok: [B]m.cpp[/B] [CODE]#include <iostream> using namespace std; int main() { int a=34, b=40; int count = 0; while(1) { if (++count > 10) break; cout << a … | |
Re: Well, in order to inline a function pointed to by a function pointer, the compiler must know (at compile time) which function the pointer points at when it's used. The compiler doesn't know that. Therefore, this code... [CODE]#include <cstdio> class CTest { public: inline static void StaticMethod(void) { printf("lalalalalalala\n"); } … | |
Re: Yes, but getting them in separate strings, the way they appear in the file, and then concatenating them the way you want is easier. EDIT: Too slow... | |
Re: If you're getting a stack overflow error it means that your array is too big to be allocated on the stack. It has nothing to do with passing it to the function. Try using a dynamically allocated array instead. A std::vector<std::vector<double> > is a good alternative too. Also, this... [CODE]class … | |
Re: The problem is here -> [ICODE]cin >> record[i].course;[/ICODE]. It should be [ICODE]getline(cin, record[i].course);[/ICODE] | |
I have two code snippets that involve recursive manipulation of std::strings. [B]code snippet 1[/B] [CODE]#include <iostream> #include <string> void print_stuff(int min_n, int max_n) { struct local { static void build_stuff( int min_n, int max_n, int cur_n, int cur_c, int max_c, bool inc, std::string & ret) { if (cur_c == max_c) … | |
Re: Maybe not the best way to do it, but it most certainly is an option: [CODE]#include <iostream> #include <set> template <class T> struct SetMaker { std::set<T> data; SetMaker & operator << (const T & element) { data.insert(element); return *this; } SetMaker & operator , (const T & element) { data.insert(element); … | |
Re: The test should be [ICODE]if(argc == 1)[/ICODE]. [ICODE]argc[/ICODE] always is at least 1, as the first argument always is the command that invoked the program. | |
Re: [QUOTE]I know I should pass it as a pointer, but for some reason it still doesn't seem to be doing anything.[/QUOTE] How do you do this? Could you post an example? Mind that you may have to pass the pointer by reference (or by pointer). Check this out: [CODE]#include <iostream> … | |
Re: [B]Q1[/B] The semicolon (;) just indicates the end of a statement. It has nothing to do with the end of the string. When you use double quotes (") around text, your compiler kindly puts a null character at the end. [B]Q2[/B] phrase is an array of 13 chars. This -> … | |
| |
Re: It's an interesting approach, but the scoped color idea is a bit awkward... I would do something like this: [CODE]#include <iostream> #include <iomanip> #include <windows.h> struct Color { int color; Color(int color_): color(color_) {} Color operator + (const Color & other) const { return Color(this->color | other.color); } }; #define … | |
Re: Try [ICODE]class Block * blocks[8] = nullptr;[/ICODE] in Cube.h and/or [ICODE]class Cube *cube;[/ICODE] in Block.h | |
Re: You can't do that with PlaySound. If you want that kind of functionality you need a library like OpenAL or SDL or SFML etc... | |
Re: Here's an example of how you can split the key in bytes and then use them to calculate the hash value, provided that the key is a primitive or a POD: [CODE]#include <iostream> template <class T> uint32_t hash(const T & key, uint32_t key_size, uint32_t table_size) { const uint8_t * bytes … |
The End.