2,898 Posted Topics
Re: If all you want is an easy solution, here is the easiest of all: [CODE] #include <cstdlib> int main() { system("octave"); return 0; }; [/CODE] | |
Re: In this case, there are several options available to you. Here are two that I would recommend (btw, these are examples you can compile and run): 1) Polymorphism: This is a basic object-oriented implementation for this problem using an interface class for all class that could implement a response for … | |
Re: Again, polymorphism comes to the rescue. It is extremely typical, in fact I don't remember ever seeing any other solution to this, to use a "renderable" interface class and make all your objects that need to be rendered as derived from that interface: [CODE] //interface for all renderable objects class … | |
Re: indexing in C++ goes from 0 to N-1, so your code should be (in code tags) as: [CODE] #include<iostream> using namespace std; int main() //main always returns an int value. { int pro[100]; for(int a=99;a>=0;a--) { pro[a]=a+1; cout<<pro[a]<<"\t"; } return 0; } [/CODE] | |
Re: Ho, it took me a while to find this one.. here is your erroneous line 63: [CODE] char *buffer=new char(length+1); //these parenthesis should be square brackets. //like this: char *buffer=new char[length+1]; [/CODE] The effect of the normal parenthesis is to allocate space for only one character and initialize its value … | |
Re: I think it's a bit of a silly question. It is known that men and women have different and often complementary abilities (or at least inclinations for certain abilities). Since programming requires such a wide array of skills, I think you would probably find as good programmers in both genders, … | |
Re: NO, at least, it would be extremely surprising and weird. Just try and see. I bet you that if you have a proper compiler, it will issue a compilation error. Why? Well it does not make any sense to have a pure virtual destructor. Because virtual destructors are not like … | |
Re: You guys have looked over the main point here. You cannot do a template specialization of a function for only a templated return type. That's why Kanoisa had to put a dummy parameter to the function, otherwise the compiler will not allow the specialization and say, in this case, "explicit … | |
Re: The output of the first piece of code is 14 because the value of variable ival (referred to by ref) is set to the value of ival2 (which is 14). A reference can only be set once, upon construction (line 4). Afterwards, any operation on the reference is exactly as … | |
Re: I agree with sfuo, this is a matter of output formatting, not of the actual precision of the stored floating-point value. By default, the output format is somewhat automatic in the sense that if you have a number that is reasonably close to 0, it will be outputted in normal … | |
Re: Try and check if the SetCommMask is return true or false. And use GetLastError to see the error message, that's all I can think of. | |
Hi experts! I have the following code which I use for dynamically creating objects in one module (executable or shared object (.so or .dll)) and be able to delete it from any other module while enforcing the call to the delete operator in the original module where the object was … | |
Re: The reason for this to happen is that the compiler generates a default = operator for any class that doesn't provide one. So, in this case, the compiler generates a = operator for A<double> = A<double> that has a better match than the A<double> = A<X>. So you would have … | |
Re: If I may add a few things to the already good answers from Narue and Fbody. First, although Narue showed a case where explicitly calling a destructor, it's important to say that there is virtually no other reason where an explicit call to the destructor makes sense and the placement … | |
Re: I think the proper logic for the if-statements is this: [CODE] if(isalpha(textstring[i])) //if it is a letter (not a number or other crap) { letters_total++; //increment letter count. if(isupper(textstring[i])) //if upper-case, { textstring[i]=tolower(textstring[i]); //then make it lower case. } histogram_abs[(textstring[i] - 'a')]++; //at this point, only a lower-case letter could … | |
![]() | Re: As suggested by firsPerson, you need to add a seed for rand (srand) and closing the file is useless (it will be automatic when the program finishes and outfile is deleted). So, try this (and indent better in the future!): [CODE] #include "stdafx.h" #include <iostream> #include <fstream> #include <ctime> using … ![]() |
Re: From what you have posted, it doesn't really seem like you are really making what most people call a DataBase (which generally uses a database server using some SQL queries and such. Examples include MySQL, Oracle, etc.). In your case, this is what most programmers would call a memory serializer. … | |
Re: Well, you can use the std::sort algorithm in "#include <algorithm>". The only little problem is that it cannot be used with a list because [URL="http://www.cplusplus.com/reference/stl/list/"]list[/URL] provides only [URL="http://www.cplusplus.com/reference/std/iterator/BidirectionalIterator/"]bidirectional iterators[/URL], not [URL="http://www.cplusplus.com/reference/std/iterator/RandomAccessIterator/"]random access iterators[/URL], which is required for the [URL="http://www.cplusplus.com/reference/algorithm/sort/"]sort algorithm[/URL]. So you need to use a [URL="http://www.cplusplus.com/reference/stl/list/"]vector[/URL] instead. Also, the … | |
Re: In general it looks quite alright. Of course, I assume there will be future iterations on that problem in your book, because the instructions stated at the beginning will certainly be changed later to a more clever design. But I don't want to spoil it for you. One mistake that … | |
Re: First of all, your file is in binary, because all data is binary on a computer. Second, you use the sizeof operator incorrectly. sizeof gives you the size of the type at compile-time. In this case the type of buf is a pointer to char which, most likely, has either … | |
Re: Hardware programming on a PC is essentially I/O programming. This means you program drivers that communicate with hardware using some channel or port. So there is no general books about it. It's all about knowing the ports you work with, the OS calls necessary to open, configure and use the … | |
Re: For starters, I don't know how to do what you want. But I do know that the code you have there is completely wrong because of the fact that the handle that is outputted by the LoadLibrary function is not by any means a pointer to the memory where the … | |
Re: Your problem is that you declare the array of vertices as having 9 coordinates in total, which would only include the first 3 points, i.e. the first triangle. [CODE]//This is where that is set: glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 9, &vertex[0], GL_STATIC_DRAW); //This is what it should be: glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 24, … | |
Re: As Duki suggested, use the [URL="http://www.boost.org/doc/libs/1_44_0/doc/html/thread.html"]Boost Thread[/URL] library. You will have to install boost. This library in cross-platform and quite easy to use, and includes all the synchronization objects you can dream of. The C++0x standard will include a thread library, and that thread library will be directly taken from … | |
Re: Well, okteta reads binary data and prints it out in hex format (to be more readable than if you opened a binary file in a text editor). So what you need to write is binary data: [CODE] ofstream myfile; myfile.open ("output.midi",ios::binary); char buffer[44] = {0x4D,0x54,0x68,0x64,0x00,0x00,0x00,0x06,0x00,0x01,0x00,0x01,0x00,0x80,0x4D,0x54,0x72,0x6B,0x00,0x00,0x00,0x16,0x80,0x00,0x90,0x3C,0x60,0x81,0x00,0x3E,0x60,0x81,0x00,0x40,0x60,0x81,0x00,0xB0,0x7B,0x00,0x00,0xFF,0x2F,0x00}; myfile.write(buffer,44); myfile.close(); [/CODE] | |
Re: [URL="http://linux.die.net/man/3/system"]system()[/URL] | |
Re: I will advocate for Boost again... Boost has this Date-Time library which implements a microseconds clock (even a nanoseconds clock). It will run on any "normal" operating system. Of course, it will only really be as precise as your system can provide, but it will output the results in microseconds … | |
Re: 4) Use are reference of the Parent type to call the overload: [CODE] #include <iostream> class Parent { public: virtual int Add(int a, int b) = 0; int Add(int a) { return this->Add(a,a); } }; class Child1 : public Parent { public: int Add(int a, int b) { return a+b; … | |
Re: This is because the function cin.get() will take a char* parameter and the size of the array of chars, and the compiler doesn't find the function that has only a char* parameter. You should change the "make" variable to type std::string (in #include <string>) and use getline() on the cin … ![]() | |
Re: szString1 is actually a pointer (or address) to the start of that chunk of memory with all the characters of the string (terminated by NULL or 0 or '\0', it's all the same). So when you pass szString1 as szTarget to the function, then szTarget also points to the start … | |
Re: No no, no need for pointers here. the original solution posted is fine (without the const_cast non-sense). Just do this: [CODE] int& get_tp() { return tp; }; .. int Activate(int& tp, int weaponDamage, int attack ); [/CODE] That's totally fine, as long as it is acceptable in your design that … | |
Re: Some compilers don't like it when the source file does not end with at least one empty line. See [URL="http://msdn.microsoft.com/en-us/library/4exw7xyc%28VS.71%29.aspx"]MSDN[/URL]. | |
Re: Well, I just tested a similar code on my Linux machine (gcc 4.4, with only default options), and it's working properly. Maybe, just maybe, in your compiler / std-libraries, there is an additional [URL="http://www.cplusplus.com/reference/string/char_traits/"]char_traits template specialization[/URL] for a static array of chars. It would be very weird and non-standard, but … | |
Re: In linux, the distributions (Ubuntu, Kubuntu, Fedora, Gnome, ..) and their families (Debian, ..) really mostly differ in where you find things in the computer (e.g. the different services and stuff your software links with). So that's why there are different "packages" for different distributions (usually by family). But really, … | |
Re: @firstPerson: the point is not if the members make sense or not for the classes, it's about how to best implement the I/O for base and derived classes. @gerard: Here is what you should be doing, in my opinion (and this is what I actually do in my own code): … | |
Re: I don't know any good books from the beginning, but I'm sure you can't go wrong with Stroustrup, like ganesh suggests. I say, once you get through a C++ book that starts from the beginning and ends in somewhat advanced Object-Oriented Programming topics. Then you should definitely get project(s) going … | |
Re: Working with such a big array is going to be very slow and troublesome. Even if you make it work on your computer, will it work on another? I would suggest you think about the algorithm you have which uses this array and see if it is not possible to … | |
Re: I'm guessing the [URL="http://www.cplusplus.com/reference/stl/bitset/to_ulong/"]to_ulong()[/URL] function is not good enough, i.e., you have too many bits in the bitset. If you can use it, then you can always memmove() the resulting unsigned long into an array: char[sizeof(unsigned long)]. If the bitset is bigger, consider splitting it up. Otherwise, I don't know, … | |
Re: Please post the code between code tags, and indent it properly.. it's horrible to look at code that has no indentation: [CODE] #include <stdio.h> #include <conio.h> int main() { char charPtr[7]; int smallest, largest, middle, intNo; int dividend=0; int modolus=0; int valid; do { printf("Enter a seven digit number:\n"); scanf("%s", … | |
Re: There is _maybe_ a tricky way to do this. Since the == operator is not required to be inside the class definition of pair, you could implement it outside of it. Now the problem will arise with the two conflicting functions: the original (somewhere in the std namespace) and your … | |
Re: Did you make sure that SDL_LoadBitmap() has a valid output. In other words, those the bitmap load properly? Because the tutorial gives a Unix-style file path for the image, maybe it is wrong? | |
Re: The idea in your second solution is the right one, you want to copy the memory from the string that is passed to a newly allocated string. That's exactly correct and proper. The only problem you have is that you used sizeof() to get the length of the string. This … | |
Re: First, is the external DLL file working? Second, maybe you should open the file with ios::binary flag as well. Maybe, I'm completely wrong though, I'm not expert on file systems. I can only suspect it is some sort of file header information Third, are those file sizes obtained from the … | |
Re: This loop is wrong: [CODE] while (!fin.eof()) { while (getline(fin, line)) { } } [/CODE] First, the outer condition is good, it means you want to read until the end of the file. The problem it that inside, you are reading all the lines in a loop and always overwriting … | |
Re: goto is evil! You would do good to take it out of your programming vocabulary! Now, how on earth is this code supposed to jump to rec, sq, or cir? You never test the ch that is input. Since your choices are numbers, you should input a number, not a … | |
Re: The other use of break is to "break" out of a loop. For example, the following three loops have essentially the same behavior (of course the first one is the simplest, best and most efficient): [CODE] const int N = 20; //upper-bound for(int i=0; i<N; ++i) { //.. do something … | |
Re: Maybe the precision is not good enough. Is it running in any sort of time that you know for sure is more than 1ms? If it does definitely take more than 1ms, then, as david said, try to inspect the tick count values directly to see if the GetTickCount() and … | |
Re: You have been looking at the wrong swap function. He means the swap function in the <algorithm> header. [URL="http://www.cplusplus.com/reference/algorithm/swap/"]see this page on it[/URL]. If you don't understand the template syntax, just assume it works for any type "T", and leave it at that for now. You would use it as, … | |
Re: [URL="http://www.cprogramming.com/tutorial/initialization-lists-c++.html"]Here[/URL] is a good article on initialization lists. >>How much code can go in an initializer list? As much as you can while keeping the code neat. This means, don't go overboard trying to fit everything in the list, make sure it stays understandable and readable. Aside from that, there … | |
Re: [URL="http://en.wikipedia.org/wiki/Static_scoping#Dynamic_scoping"]Wikipedia[/URL] I never heard of dynamic scoping, and to no surprise after reading this wiki. There is no dynamic scoping in C++, and frankly, I'm happy there is none because it seems more hurtful than helpful. |
The End.