466 Posted Topics
Re: Seems like the task could be to use a *bitshift* operation: int i = 1; std::cout << i << std::endl; i << 1; // Shift bits to the left std::cout << i << std::endl; i << 1; // Shift bits to the left std::cout << i << std::endl; This should … | |
Re: This task is all about *operator overloading*. Search for "C++ operator overloading" in your favourite search engine and you'll get lots of examples. You first need to make a `TrashCan` class though, have you done that? | |
Re: You have a few choices. 1. Make an array that's sufficiently big to meet the likely amount of data 2. Dynamically allocate the memory at run-time, using `new` 3. Use `std::vector` to manage the memory allocation for you I'd say that you probably need to use option 2 for this … | |
Re: You can use a [icode]stringstream[/icode] for this. Try something like [code] #include <iostream> #include <string> #include <sstream> int main() { for ( int i = 0; i < 10; ++i ){ std::string filenameRoot("file_"); std::stringstream ss; ss << filenameRoot << i << ".txt"; std::cout << ss.str() << std::endl; } return 0; … | |
Re: You can modify your function to take a function as one of the arguments. I find it easiest to add another template parameter to do this: template< typename value_t, typename op_t > value_t reduceArray( value_t myArray[], size_t size, value_t initial, op_t op) { value_t out = initial; for ( size_t … | |
Re: Is the array that you pass in to this function as `dynamicArray` allocated using `new[]`? If not this would cause a crash. If you could, it might help if you post a fragment of the code that calls this function. This kind of thing is one reason why you should … | |
Re: > ptr is a pointer to first position in array and arr is pointing to where number 33 exists in array. so (ptr-arr) is the offset from begining of the array where 33 exists (probably equal to 2). Almost. You have `ptr` and `arr` the wrong way round in this … | |
Re: Making parts of your program into function calls is easy: // Function prototype/declaration // * Needs to be before the function call. // * Ends in a semi-colon // * The 'int' at the start is the "return-type", it can be any type // * the things in the brackets … | |
Re: Since you mentioned Boost, have you looked at [Boost program_options](http://www.boost.org/doc/libs/1_51_0/doc/html/program_options.html)? It has a nice syntax for parseing these kind of files. Lots of the tutorials on the site are about using it to parse command line arguments, but the is a section about parsing config file [here](http://www.boost.org/doc/libs/1_51_0/doc/html/program_options/overview.html#id2549206). The only thing … | |
Re: If you're OK using external libraries, then boost has `boost::lexical_cast`, which can be used to do these conversions: std::string s1( "123" ); int i = boost::lexical_cast< int >( s1 ); std::string s2 = boost::lexical_cast< std::string >( i ); If it can't do the conversion, `lexical_cast` throws a `boost::bad_lexical_cast` exception. I … | |
Re: The maximum value that `rand()` will return is determined by a variable in `cstdlib` called `RAND_MAX`. I think it's compiler/system-dependent, but should be at least 32767. What is it on your system? | |
Re: On the static variable part: you have to initialise a static variable to something, so just doing this won't work: class MyClass { public : void AddOne(){ ++myStaticVar; } float Get() const { return myStaticVar; } private : float myStaticVar; }; You will get a compilation error. The variable must … | |
Re: I think that the question intends that you use a loop to read in and print out the information about the bills. For instance, the bits like this: cout << "Enter the number of One Hundred Dollar Bills you have: "; cin >> bill; cout << '\n'; bills.push_back( bill ); … | |
Re: Just a note. Your algorithm isn't particularly efficient. You can see from your loops that this algorithm requires `n1 * n2` iterations. You should consider if you *need* to loop over the whole of `string2` for each element in `string1`? For instance, you can take advantage of the fact that … | |
Re: It sounds like you need to pass the serial comms object (as a reference or pointer) into the functions that need to use it. | |
Re: You could `new` a `JointDetails` object in `main` before you make your other classes, then add a constructor `Analyzer` that takes a pointer to a `JointDetails` and constructors to `Test1` and `Test2` that also take pointers to `JointDetails`. The `Test1` and `Test2` constructors would pass the pointers to `JointDetails` that … | |
Re: You should be careful not to leak memory. The `Car` pointer that you `new` on line 99 goes out of scope on line 108 without being deleted. This means that you leak a `Car` every time you go round the while loop. In C++ there are so-called *smart pointers* that … | |
Re: Have you checked how long it takes to just run through the array and add 1 to all the values? I suspect it's not that long. I was recently profiling some code that was doing arithmetic on arrays like this and the compiler-optimised, release code was running over each element … | |
Re: I don't know about decoding it, but you can easily see that there is some structure in this data. If you insert line-breaks in the right places you can see that there are two main sections to the data that feature different repeating motifs: 4B 50 7B F1 F4 F5 … | |
Re: One way that I can immediately see is that you only need to look for factors up to `number / 2`, instead of `number` like you currently do. That should give you a factor of 2 speed up in total run-time Another thing would be to make your search multi-threaded. ![]() | |
Re: You need to take that call to `print_pdb` out of the loop that it's in (move it after line 59). `print_pdb` trys to loop over `simbox_TotalNparticles` elements and print them out, but they don't all exist yet. You allocate your array of pointers to doubles on line 29. You then … | |
Re: From Phorce's original example: vector<string> names(2); // Define a vector with a size of 2 elements names.push_back("Phorce"); names.push_back("Bob"); names.push_back("Jimmy"); /* Note how I have 3 elements, in an array, it would force a memory error if not handled. */ This doesn't do what is stated. When you do `vector< string … | |
Re: I think you should re-evaluate your approach to parsing the equation. It would be greatly simplified and more understandable if you take a more object-oriented approach. If you don't know how to use classes in C++, look it up; it's really useful :) So, a polynomial is a collection of … | |
Re: You could use `boost::lexical_cast` to try and convert the string to a number and catch the `boost::bad_lexical_cast` exception that's thrown if the conversion can't be done. This feels a bit weird though, as I generally try not to use exceptions for flow control. I guess you should wrap it in … | |
Re: From this line: `./oemsort(_Z11printToFilePiiPc+0x5b)[0x40be51]` It looks like the problem is in the function printToFile. Looking at line 212 of your code, you pass in `argv[3]` as the file name. If you check the command line that you invoked the program with: `mpirun -n 5 ./oemsort 2 unsorted.txt sorted.txt` Then, `argv[3]` … | |
Re: >Does #include <wingdi.h> not include those libraries? No, only the function declarations are generally in the header file. The declarations are enough for the compiler to compile the code that is calling the function. However, when the linker (that's what the `ld` part of the error is referring to) tries … | |
Re: You need two things to do this. Firstly, you need to make a `for` loop that runs over all the numbers. Secondly, you need to use the `%` operator on each. `%` gives you the "remainder" of an integer division. If you divide an integer by 2 and look at … | |
Re: This isn't really an answer to your question, but if I were you, I'd just read in the whole line in one go and then have a separate piece of code to split the line into its individual fields. If you think about it, they are logically separate operations: 1. … | |
Re: What do you mean "configure"? What are you trying to do? | |
Re: In gereral it's a really bad idea to use macros with very short names, like `n`. It removes the posibility of ever using that symbol as a variable name in your code. I think it's generally a good idea to try and use longish upper-case names for macros. Additionally, if … | |
Re: To make a simple periodic-boundary-condition-like access to an array, you can do size_t size = 5; double array[ size ]; // ... Fill the array with data std::cout << "enter index:" << std::endl; int index; std::cin >> index; double value = array[ ( size + ( index % size ) … | |
Re: This is not directly related to your question, but it will make things easier for you. From your code, it looks like you're representing your polynomial in the order that it's typically written down, e.g. a*x^3 + b*x^2 + c*x + d This is fine, but it's easier to work … | |
Re: Some answers (not to all the questions though)... SnipRobotDlg.cpp: Line 12: You should always try and initialise variables to a known value. In this case, after `get_body` has returned, you should check `pElementBody != NULL`. Line 22: You should check that "Snip" is actually found in the string. Line 34: … | |
![]() | Re: You don't need to use a `goto` here. You should remove it from your program. I think, with a bit of tweaking, you can replace the `goto` with a `while` loop. I think that labels have the same kind of scoping rules as other variables and functions in C++, so … |
Re: As mike_2000_17's comprehensive reply said, anything in the Arduino family of things is pretty easy. I've used freeduino in the past. You get the board for about $30. There's some soldering to do, but not too much and all of the surface-mounted componented are done for you, so you don't … | |
Re: I know this isn't an answer to your question, but why do you need to do all this adding of spaces and such? It sounds like it could be a bit of a hack? If you don't mind, what is it that you're actually trying to accomplish with the `find_keyword` … | |
Re: I've never used it to flush the input stream, but I have used it to flush the output stream. Say you want to produce output like: Doing something... done! Then I'd have code like this: std::cout << "Doing something... "; fflush( std::cout ); /* Some code that takes some time … | |
Re: This code builds fine for me. Which compiler/platform are you using? There are a couple of changes that I'd make though. For example, You don't need to do find the length of the number in the way that you do on line 32. In fact, I don't think that there's … | |
Re: You probably don't need the debug libaries, unless you actually expect to be debugging Qt itself. If you just want to use Qt in your program, then you can use the non-debug version of Qt. You'll still be able to debug your program, it's just that you won't be able … | |
Re: Er, this looks *very* similar to the [this example](http://www.cplusplus.com/reference/clibrary/cstdio/remove/) on cplusplus.com | |
Re: You could set up some enums like this: enum EDirection { eNone = 0x0000, eNorth = 0x0001, eSouth = 0x0010, eEast = 0x0100, eWest = 0x1000, eNorthEast = eNorth | eEast, eNorthWest = eNorth | eWest, eSouthEast = eSouth | eEast, eSouthWest = eSouth | eWest }; And the, check … | |
Re: One way you could do it would be to make a class that stores the details, something like: class CPersonDetails { public : CPersonDetails( const std::string& name, unsigned class, std::string number ); private : std::string m_name; unsigned m_class; std::string m_phoneNumber; }; You could then overload `operator<` for this class and … | |
Re: this isn't doing what i think you think it's doing. `array[i]` is an array of 5 strings, not an array of characters. There's no default way to insert arrays of things into a streaam, so the compiler will complain. | |
Re: Visual studio has a special file called `autoexp.dat` that tells the debugger how to format things when it's presenting them to you. You can add your own definitions to the file, so you can have a custom presentation of pretty much any structure in the debugger. It's one of the … | |
Re: Classes generally have two aspects to them: Things they *know* and things they *do*. The first is generally taken care of by the class *member variables*, while the second is the job of the class's *member functions* (sometimes called *class methods*). The task you posted already states that you're going … | |
Re: It looks like you're used to programming in C a bit more than C++. As Deceptikon mentioned, you should write some constructors to handle copying and other object instantiation events. This will greatly improve the level of *encapsulation* in your program. In general, it's desirable for an object to handle … | |
Re: [QUOTE=WaltP;1745749]Add another [iCODE]getline()[/iCODE] just before the [I]return[/I] in main....[/QUOTE] To add to what WaltP said, I like to make a small function called something like [icode]WaitForExit()[/icode] which encapsulates this: [code] #include <iostream> void WaitForExit() { std::cout << "Press ENTER key to exit...." << std::endl; std::cin.get(); } int main() { WaitForExit(); … | |
Re: Er, I don't think that any of these examples do what you want them to do. If you take Lucaci Andrew's code for example. If you have a file in the format shown in the example, then the program will try to open a file called "1," and then fail, … | |
Re: Er, do you use MS Outlook for your email? If so it has built in features to allow you to do this kind of thing with templates, see [here](http://office.microsoft.com/en-us/outlook-help/create-an-e-mail-template-HA001086500.aspx) | |
Re: Just a small point but, in C++, you don't need to put the `struct` keyword before the typename when you're using your structs. Also, I don't think that you need to do all the casting that you're doing. I think that this would be fine: struct AIstruc_word { // Set … |
The End.