2,898 Posted Topics
Re: very simple: [CODE] int last_time_ms = some_time_function(); for(int year=0;year<MAX_YEAR;++year) { //perform your task.. int current_time_ms = some_time_function(); sleep(2000 - (current_time_ms - last_time_ms)); last_time_ms = some_time_function(); }; [/CODE] that's it. If you are looking for a good time function, I would suggest Boost Date-Time library, which would turn into: [CODE] #include … | |
Re: I think this issue is best illustrated by a simple example. Consider the following simple program: [CODE] const int four_factorial = 24; int some_function(int* c, int* d) { return c[0]*d[0] + c[1]*d[1] + four_factorial; }; int main() { int* a = new int[2]; int b[2]; int result = some_function(a,b); delete[] … | |
Re: Consider this simple example: [CODE] double twice(double d) { return 2.0*d; }; int main() { double result = twice(2.0); std::cout << "result is: " << result << std::endl; return 0; }; [/CODE] This would, most probably, with no optimization and no inlining, to something like this in pseudo-assembly code (I'm … | |
Re: Your question(s) is very broad. But I think that basically you are on the right path (at least in my opinion). Learning to be a skilled programmer does involve a lot of bits and pieces that you get acquainted to along the way. You said you already learned to work … | |
Re: make sure that the method "printSummary()" is also defined as const, like this: [CODE] class MutantType { //stuff here... public: void printSummary() const { //notice the const after the function prototype. //..print stuff but cannot change any data member value. }; }; [/CODE] otherwise, the compiler will throw an error … | |
Re: In my opinion, there are things that are important, others not so much. What I can think of, off the bat: Important stuff includes: - have clear names for classes and variables (identifiers), even if it makes them longer (IDE code-completion solves the extra typing issue, and it doesn't work … | |
Re: Well, think by representing the operation for small 2x2 and 3x3 matrices, you can see the pattern emerge: [CODE] //say vector<double> q is some 2x2 matrix then qt = {q[0],q[2],q[1],q[3]}; //say vector<double> q is some 3x3 matrix then qt = {q[0],q[3],q[6],q[1],q[4],q[7],q[2],q[5],q[8]}; [/CODE] For a (faster) limited-memory way, well there is … | |
Re: I think you would have better luck in a forum dedicated to Boost and/or Regex questions. One thing I can suggest is that the Boost Regex has to be linked to its binaries too (boost_regex.lib or libboost_regex.so/a), so make sure that is set up correctly (if not you should get … | |
Re: Ok.. here's the deal. When you hold a pointer within an object you need to clarify "who" owns the pointed-to memory. If the object owns the pointed-to memory, then you have to perform a deep-copy of the object if you assign it to another one and in this case there … | |
Re: First part: for the pow() function issue. I would suggest you just avoid it altogether by a simple multiplying accumulator: [CODE] int Poly::evaluate(int x) { int total = 0; int x_pow_term = 1; for(int i = 0; i <= degree; i++){ total += coefs[i]*x_pow_term; //coefs[] array is the array in … | |
Re: This is an issue that comes up once in a while on this forum and others. The first option that you can consider is the one that you are already trying to implement and with the link in evstevemd's post, it shouldn't be a problem to fix the syntax. However, … | |
Re: I'm not a security expert either, but here is a thought to add to the options already given. Write a little program that generates a module definition file (.def) for your dll with all the aliases (symbols) of the functions encrypted, so a user app won't be able to load … | |
Re: you only need to call srand(time(0)) once during the application's execution. So the function you have is not dependent on time and you can generate your random numbers as fast as you like (just take the srand() call out of your function and call it once at the start of … | |
Re: You are missing one star at line 4 of the second code snippet: [CODE] temp2=(char***)malloc (FIRST_DIMENSION * sizeof(char *[COLOR="red"]*[/COLOR])); [/CODE] For the rest, it is the correct way to do it. BTW sizeof(char) is always equal to 1, by definition from the standard. | |
Re: From the link you gave, first, these are recursion formulas, meaning that they are calculated as the data comes in, so all those calculations should be within that value-entering loop you have (or you can loop through the data in another function and apply the formulas). Since there aren't that … | |
Re: The compiler has to know that a function called "overDraft" exists before it gets to a line that calls that function. There are two options: 1) you can declare that the function exists by putting a "prototype" of the function before "withdraw". The prototype for overDraft has to exactly match … | |
Re: I'm sorry to say this, but there is something awfully wrong with your implementation. If you cast a pointer to CMap to a char pointer and save, in binary, sizeof(CMap) bytes, you will also save other "undesirable" things, especially the virtual table pointer (vptr) which is not going to be … | |
Re: >>Also, when writing in java, especially in eclipse, you have a project folder, a source folder, a package and then the class files. Is there a similar setup I can follow with C++? When you get into C++, you enter the land of the Free. One beautiful thing with C++ … | |
Re: Instructions to get the above to compile: 1) add #include <string> at the start of the Product.h file. 2) replace each use of type string with std::string in the Product.h file. 3) add "using namespace std;" just after the includes in the Product.cpp file 4) as said by firstPerson, include … | |
Re: Well, just uncomment the line 153 you have and it should work. Doesn't it? I would say that you need to implement a deep copy-constructor for class student_Univ, but, since your management of memory, pointers, and ownership is essentially non-existent in your code (which is very bad, especially when working … | |
Re: I have to say that I disapprove greatly of this code you posted. I cannot sanction the idea of getting a pointer to a pair that is used in a map or multimap object. This is because you don't know, and I don't know, how multimap implements it list of … | |
Re: I don't see anything wrong with your implementation except for this trivial piece missing in the main() to see the result: [CODE] vector<string> result = split(expression); cout << expression << " split into individual entities yields: "; for(vector<string>::iterator it = result.begin(); it != result.end(); ++it) cout << "'" << (*it) … | |
Re: You store the values if you need them later, you don't if you don't need them later. For the sum itself, you will only need a temporary variable to save the input but not a permanent array. To store all of them in a "single variable", that variable will have … | |
Re: This is a classic problem that comes up almost every day on this forum. The reason for this problem is that when you do a getline, it will get the string up to the first newline (enter stroke) and will delete that newline from the input buffer. But, when you … | |
Re: On a technical side, you have a problem with pointers / references / objects. A pointer is an address to an object of a class. An object is allocated for and created with new and destroyed and deallocated with delete via a pointer to that object (not a dereferenced pointer). … | |
Re: Well, I will do a little leap of faith and trust that you actually put some effort into it. Here is a very simple example that contains all that you need to do those two problems: [CODE] #include <iostream> using namespace std; int main() { double width = 0; double … | |
Re: This line: [CODE]foo * var[5];[/CODE] creates five pointers to a foo object, but that does not create five foo objects. var will hold 5 pointers to no-where or anywhere, which is the same. To initialize five foo objects for those pointers to point to, you need to do this: [CODE] … | |
Re: Line 15: the bound on the loop is wrong.. simple typo, you wrote i where it should be j: [CODE] for (j = 0; i < EVEN; j++) //should be: for (j = 0; j < EVEN; j++) [/CODE] | |
Re: I think the best way to look at this problem is by dividing the tasks properly. I think your Othello class seems to be ill-defined, as you said it does "almost everything" that's a bad sign that you can't even clearly say what it does or what it is responsible … | |
Re: Now, the problem is the difference between an object and a pointer to an object. Think of a class as any other type (like int or float or double), if you declare a variable with * it's a pointer to an object, if you declare with & it's a reference … | |
Re: Ok... the problem here is that you are confusing the idea of a class and an object. A class is a type that you define. In a class, you declare what data an object of that class holds and what functions (methods) can be used on that data. The object … | |
Re: What you have here is what is called "circular referencing" meaning that first.cpp needs what is in second.cpp to be defined and second.cpp needs what is in first.cpp to be defined (so what comes first, the egg or the chicken?). Luckily, C++ has a mechanism built-in for that exact purpose, … | |
Re: There are many more options available than the two you mentioned, but without details on what you are exactly trying to achieve, it's hard to tailor the answer. I don't know Qt at all, but I have done quite a bit of GUI, so let me give a few "generic" … | |
Re: >>how can I do it without pointers? Like this: [CODE] class Agent { private: struct AgentStruct { std::string agentName; double pID; double mID; AgentStruct *nextAgent; } AgentP; //notice no * star sign. public: Agent(std::string); void SetNextAgent(const AgentStruct&); //notice pass-by-reference Agent* GetNextAgent(); void SendMessage(); void ReceiveMessage(); }; //... /* * The … | |
Re: What has been said so far is all true. As Milton hinted to, what I would say is the main difference between struct and class is in the traditional use of them (or programmer conventions). Most programmers and most code that I have ever seen make use of struct almost … | |
Re: Just move this line 160: [CODE] LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);//the real winproc [/CODE] To the start of the code (after the includes) and all will be fine. The compiler has to know that there is a function (defined later) that is called WinProc and has the given prototype, before it gets … | |
Re: The reason for this problem is that array indexes in C/C++ start at 0 and end at N-1 (not N). So One[31] is not part of the array "long One[31];" it is actually one long beyond the last element of the array. The last element of the array is actually … | |
Re: I'm not an expert with this, but I have one idea / suggestion. This value of 4254684 looks a lot like a pointer value (converted to integer). Is it possible that you have a binary compatibility problem? Did you make sure that the static library in which the Array class … | |
Re: If I may suggest another solution that you might find more elegant and that could serve other purposes for this class or another: [CODE] class MyTime { ... public: //create formatting containers: class MilitaryTime { const MyTime& obj; public: explicit MilitaryTime(const MyTtime& aObj) : obj(aObj) { }; //implement ostream overload … | |
Re: You have to link the proper libraries for any OpenGL application. Your prof should have given instructions on how to compile. I'm not sure, it has been a while, but I think you need to link with: for Linux: libglut.a (add -lglut to the command line) and libopengl.a (-lopengl) for … | |
Re: The idea with the WinProc function is that the first parameter "hwnd" is the handle of the window whose message belongs to. So what you need to do is have a simple conditional inside the WinProc to redirect the message handling depending if the hwnd is for the first or … | |
Re: First, a critical section is not really the synchro you need for this, you want to use a mutex (may I recommend from [URL="http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html"]Boost Thread[/URL]). Second, you say you have a _list_ of structs. Is that a loose terminology or is it an actual std::list. If it is a std::list … | |
Re: Well this is not the first time on this forum that people have posted weird problems while using Windows Vista / 7 and Dev-C++ (a combination that has many known issues). I recommend you try another IDE, like Code::Blocks and see if the problem persists. | |
Re: When I look at this code, I remember why people don't program in C so much any more... This post probably belongs in the C programming forum instead. Anyhow, there is one definite problematic line, that is line 28. I found it, because that's always the first thing I look … | |
Re: wow, I think you have a couple of concepts mixed-up here. Hexadecimal numbers are just a notation for numbers in base 16 (like decimal is in base 10, binary in base 2, and octal in base 8). The computer stores only binary data, so any value is represented in binary. … | |
Re: POSIX is essentially for Linux or Unix systems. POSIX is just a specification for the API that the OS needs to expose and Linux, Unix and many others comply to those specifications. Windows does not, so windows has its own things, for example, via its Win32 API. Boost Thread library … | |
Re: I usually tackle these problems by looking at the boundaries. In your case, you have the basic scaling factor is "energy / max" which is at most 1 and at least 0. So in any logarithmic scale (I would assume you want base 10, but that doesn't really matter), these … | |
Re: At line 65, 66, and 67, you should have (index) instead of (index - 1). In the case you have index == 0, that would result in -1 and delete the element before the first one, which should result in the error you have got. | |
Re: First of all, after entering a single character, you usually want to call the ignore() function on cin to ignore the rest of the characters in the interval ]first character .. new-line (or enter)] (note the exclusive and inclusive brackets). Now, if you want a single character input in the … |
The End.